Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit e0ce8c4

Browse files
authored
Merge pull request #17 from GraphQLAcademy/add-vehi
Vehicle type and transport interface
2 parents b4a9163 + efd6553 commit e0ce8c4

File tree

6 files changed

+140
-17
lines changed

6 files changed

+140
-17
lines changed

app/models/graph/schema.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module Graph
22
Schema = GraphQL::Schema.define do
33
query Graph::Types::Query
4+
resolve_type ->(obj, ctx) do
5+
Graph::Schema.types.values.find { |type| type.name == obj.class.name }
6+
end
47
end
58
end

app/models/graph/types/query.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ module Types
4848
field :starships, types[Graph::Types::Starship] do
4949
resolve ->(_, _, _) { ::Starship.all }
5050
end
51+
52+
field :vehicle, Graph::Types::Vehicle do
53+
argument :id, types.ID
54+
resolve ->(_, args, _) { ::Vehicle.find(args['id']) }
55+
end
56+
57+
field :vehicles, types[Graph::Types::Vehicle] do
58+
resolve ->(_, _, _) { ::Vehicle.all }
59+
end
5160
end
5261
end
5362
end

app/models/graph/types/starship.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,17 @@ module Types
44
name "Starship"
55
description "A single transport craft that has hyperdrive capability."
66

7-
field :id, types.ID, "The ID of this species."
7+
interfaces [Graph::Types::TransportInterface]
88

9-
field :name, types.String, "The name of this starship. The common name, such as \"Death Star\"."
10-
field :model, types.String, "The model or official name of this starship. Such as \"T-65 X-wing\" or \"DS-1 Orbital Battle Station\"."
9+
field :id, types.ID, "The ID of this starship."
10+
11+
# Starship Specific Fields
1112

1213
field :starshipClass, types.String do
1314
description "The class of this starship, such as \"Starfighter\" or \"Deep Space Mobile Battlestation\""
1415
property :starship_class
1516
end
1617

17-
field :manufacturer, types.String, "The manufacturer of this starship."
18-
field :costInCredits, types.Float, "The cost of this starship new, in galactic credits", property: :cost_in_credits
19-
field :length, types.Float, "The length of this starship in meters."
20-
field :crew, types.String, "The number of personnel needed to run or pilot this starship."
21-
field :passengers, types.String, "The number of non-essential people this starship can transport."
22-
23-
field :maxAtmospheringSpeed, types.Int do
24-
description "The maximum speed of this starship in atmosphere. null
25-
if this starship is incapable of atmosphering flight."
26-
property :max_atmosphering_speed
27-
end
28-
2918
field :hyperdriveRating, types.Float, "The class of this starships hyperdrive.", property: :hyperdrive_rating
3019

3120
field :MGLT, types.Int do
@@ -39,6 +28,23 @@ module Types
3928
property :mglt
4029
end
4130

31+
# Transport Interface Fields
32+
33+
field :name, types.String, "The name of this starship. The common name, such as \"Death Star\"."
34+
field :model, types.String, "The model or official name of this starship. Such as \"T-65 X-wing\" or \"DS-1 Orbital Battle Station\"."
35+
36+
field :manufacturer, types.String, "The manufacturer of this starship."
37+
field :costInCredits, types.Float, "The cost of this starship new, in galactic credits", property: :cost_in_credits
38+
field :length, types.Float, "The length of this starship in meters."
39+
field :crew, types.String, "The number of personnel needed to run or pilot this starship."
40+
field :passengers, types.String, "The number of non-essential people this starship can transport."
41+
42+
field :maxAtmospheringSpeed, types.Int do
43+
description "The maximum speed of this starship in atmosphere. null
44+
if this starship is incapable of atmosphering flight."
45+
property :max_atmosphering_speed
46+
end
47+
4248
field :cargoCapacity, types.Float do
4349
description "The maximum number of kilograms that this starship can transport."
4450
property :cargo_capacity
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Graph
2+
module Types
3+
TransportInterface = GraphQL::InterfaceType.define do
4+
name "Transport"
5+
description "A single transport craft"
6+
7+
field :name, types.String
8+
field :model, types.String
9+
field :manufacturer, types.String
10+
field :costInCredits, types.Float
11+
field :length, types.Float
12+
field :crew, types.String
13+
field :passengers, types.String
14+
field :maxAtmospheringSpeed, types.Int
15+
field :cargoCapacity, types.Float
16+
field :consumables, types.String
17+
field :pilots, types[Graph::Types::Person]
18+
end
19+
end
20+
end

app/models/graph/types/vehicle.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Graph
2+
module Types
3+
Vehicle = GraphQL::ObjectType.define do
4+
name "Vehicle"
5+
description "A single transport craft that has hyperdrive capability."
6+
7+
interfaces [Graph::Types::TransportInterface]
8+
9+
field :id, types.ID, "The ID of this vehicle."
10+
11+
# Vehicle Specific Fields
12+
13+
field :vehicleClass, types.String do
14+
description "The class of this vehicle, such as \"Wheeled\" or \"Repulsorcraft\"."
15+
property :vehicle_class
16+
end
17+
18+
# Transport Interface Fields
19+
field :name, types.String, "The name of this vehicle. The common name, such as \"Sand Crawler\" or \"Speeder bike\""
20+
field :model, types.String, "The model or official name of this vehicle. Such as \"All-Terrain Attack Transport\"."
21+
22+
field :manufacturer, types.String, "The manufacturer of this vehicle."
23+
field :costInCredits, types.Float, "The cost of this vehicle new, in galactic credits", property: :cost_in_credits
24+
field :length, types.Float, "The length of this vehicle in meters."
25+
field :crew, types.String, "The number of personnel needed to run or pilot this vehicle."
26+
field :passengers, types.String, "The number of non-essential people this vehicle can transport."
27+
28+
field :maxAtmospheringSpeed, types.Int do
29+
description "The maximum speed of this vehicle in atmosphere. null
30+
if this vehicle is incapable of atmosphering flight."
31+
property :max_atmosphering_speed
32+
end
33+
34+
field :cargoCapacity, types.Float do
35+
description "The maximum number of kilograms that this vehicle can transport."
36+
property :cargo_capacity
37+
end
38+
39+
field :consumables, types.String, "The maximum length of time that this vehicle can provide consumables for its entire crew without having to resupply."
40+
field :pilots, types[Graph::Types::Person], "The pilots on this vehicle."
41+
42+
field :created_at, types.String, "The ISO 8601 date format of the time that this resource was created."
43+
field :updated_at, types.String, "The ISO 8601 date format of the time that this resource was updated."
44+
end
45+
end
46+
end

test/controllers/graphql_controller_test.rb

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
7777
{ "name" => "Nien Nunb" }
7878
],
7979
"starshipClass" => "Light freighter"
80+
},
81+
"vehicle" => {
82+
"id" => "82948950",
83+
"name" => "Snowspeeder",
84+
"model" => "t-47 airspeeder",
85+
"cargoCapacity" => 10.0,
86+
"consumables" => "none",
87+
"costInCredits" => nil,
88+
"created_at" => "2014-12-15 12:22:12 UTC",
89+
"crew" => "2",
90+
"length" => 4.5,
91+
"manufacturer" => "Incom corporation",
92+
"maxAtmospheringSpeed" => 650,
93+
"passengers" => "0",
94+
"pilots" => [
95+
{ "name" => "Luke Skywalker" },
96+
{ "name" => "Wedge Antilles" },
97+
{ "name" => "Luke Skywalker" },
98+
{ "name" => "Wedge Antilles" },
99+
],
100+
"vehicleClass"=>"airspeeder"
80101
}
81102
}
82103
}
@@ -88,7 +109,7 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
88109

89110
def full_graphql_query
90111
"
91-
query Full($personID: ID, $filmID: ID, $planetID: ID, $starshipID: ID, $speciesID: ID) {
112+
query Full($personID: ID, $filmID: ID, $planetID: ID, $starshipID: ID, $speciesID: ID, $vehicleID: ID) {
92113
person(id: $personID) {
93114
birthYear
94115
eyeColor
@@ -152,6 +173,22 @@ def full_graphql_query
152173
pilots { name }
153174
starshipClass
154175
}
176+
vehicle(id: $vehicleID) {
177+
id
178+
name
179+
model
180+
cargoCapacity
181+
consumables
182+
costInCredits
183+
created_at
184+
crew
185+
length
186+
manufacturer
187+
maxAtmospheringSpeed
188+
passengers
189+
pilots { name }
190+
vehicleClass
191+
}
155192
}
156193
"
157194
end
@@ -162,13 +199,15 @@ def default_variables
162199
film = films(:"a-new-hope")
163200
planet = planets(:alderaan)
164201
species = species(:hutt)
202+
vehicle = vehicles(:snowspeeder)
165203

166204
{
167205
"starshipID" => starship.id,
168206
"personID" => person.id,
169207
"filmID" => film.id,
170208
"planetID" => planet.id,
171-
"speciesID" => species.id
209+
"speciesID" => species.id,
210+
"vehicleID" => vehicle.id
172211
}
173212
end
174213
end

0 commit comments

Comments
 (0)