Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models/graph/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Graph
Schema = GraphQL::Schema.define do
query Graph::Types::Query
resolve_type ->(obj, ctx) do
Graph::Schema.types.values.find { |type| type.name == obj.class.name }
end
end
end
9 changes: 9 additions & 0 deletions app/models/graph/types/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ module Types
field :starships, types[Graph::Types::Starship] do
resolve ->(_, _, _) { ::Starship.all }
end

field :vehicle, Graph::Types::Vehicle do
argument :id, types.ID
resolve ->(_, args, _) { ::Vehicle.find(args['id']) }
end

field :vehicles, types[Graph::Types::Vehicle] do
resolve ->(_, _, _) { ::Vehicle.all }
end
end
end
end
36 changes: 21 additions & 15 deletions app/models/graph/types/starship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,17 @@ module Types
name "Starship"
description "A single transport craft that has hyperdrive capability."

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

field :name, types.String, "The name of this starship. The common name, such as \"Death Star\"."
field :model, types.String, "The model or official name of this starship. Such as \"T-65 X-wing\" or \"DS-1 Orbital Battle Station\"."
field :id, types.ID, "The ID of this starship."

# Starship Specific Fields

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

field :manufacturer, types.String, "The manufacturer of this starship."
field :costInCredits, types.Float, "The cost of this starship new, in galactic credits", property: :cost_in_credits
field :length, types.Float, "The length of this starship in meters."
field :crew, types.String, "The number of personnel needed to run or pilot this starship."
field :passengers, types.String, "The number of non-essential people this starship can transport."

field :maxAtmospheringSpeed, types.Int do
description "The maximum speed of this starship in atmosphere. null
if this starship is incapable of atmosphering flight."
property :max_atmosphering_speed
end

field :hyperdriveRating, types.Float, "The class of this starships hyperdrive.", property: :hyperdrive_rating

field :MGLT, types.Int do
Expand All @@ -39,6 +28,23 @@ module Types
property :mglt
end

# Transport Interface Fields

field :name, types.String, "The name of this starship. The common name, such as \"Death Star\"."
field :model, types.String, "The model or official name of this starship. Such as \"T-65 X-wing\" or \"DS-1 Orbital Battle Station\"."

field :manufacturer, types.String, "The manufacturer of this starship."
field :costInCredits, types.Float, "The cost of this starship new, in galactic credits", property: :cost_in_credits
field :length, types.Float, "The length of this starship in meters."
field :crew, types.String, "The number of personnel needed to run or pilot this starship."
field :passengers, types.String, "The number of non-essential people this starship can transport."

field :maxAtmospheringSpeed, types.Int do
description "The maximum speed of this starship in atmosphere. null
if this starship is incapable of atmosphering flight."
property :max_atmosphering_speed
end

field :cargoCapacity, types.Float do
description "The maximum number of kilograms that this starship can transport."
property :cargo_capacity
Expand Down
20 changes: 20 additions & 0 deletions app/models/graph/types/transport_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Graph
module Types
TransportInterface = GraphQL::InterfaceType.define do
name "Transport"
description "A single transport craft"

field :name, types.String
field :model, types.String
field :manufacturer, types.String
field :costInCredits, types.Float
field :length, types.Float
field :crew, types.String
field :passengers, types.String
field :maxAtmospheringSpeed, types.Int
field :cargoCapacity, types.Float
field :consumables, types.String
field :pilots, types[Graph::Types::Person]
end
end
end
46 changes: 46 additions & 0 deletions app/models/graph/types/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Graph
module Types
Vehicle = GraphQL::ObjectType.define do
name "Vehicle"
description "A single transport craft that has hyperdrive capability."

interfaces [Graph::Types::TransportInterface]

field :id, types.ID, "The ID of this vehicle."

# Vehicle Specific Fields

field :vehicleClass, types.String do
description "The class of this vehicle, such as \"Wheeled\" or \"Repulsorcraft\"."
property :vehicle_class
end

# Transport Interface Fields
field :name, types.String, "The name of this vehicle. The common name, such as \"Sand Crawler\" or \"Speeder bike\""
field :model, types.String, "The model or official name of this vehicle. Such as \"All-Terrain Attack Transport\"."

field :manufacturer, types.String, "The manufacturer of this vehicle."
field :costInCredits, types.Float, "The cost of this vehicle new, in galactic credits", property: :cost_in_credits
field :length, types.Float, "The length of this vehicle in meters."
field :crew, types.String, "The number of personnel needed to run or pilot this vehicle."
field :passengers, types.String, "The number of non-essential people this vehicle can transport."

field :maxAtmospheringSpeed, types.Int do
description "The maximum speed of this vehicle in atmosphere. null
if this vehicle is incapable of atmosphering flight."
property :max_atmosphering_speed
end

field :cargoCapacity, types.Float do
description "The maximum number of kilograms that this vehicle can transport."
property :cargo_capacity
end

field :consumables, types.String, "The maximum length of time that this vehicle can provide consumables for its entire crew without having to resupply."
field :pilots, types[Graph::Types::Person], "The pilots on this vehicle."

field :created_at, types.String, "The ISO 8601 date format of the time that this resource was created."
field :updated_at, types.String, "The ISO 8601 date format of the time that this resource was updated."
end
end
end
43 changes: 41 additions & 2 deletions test/controllers/graphql_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
{ "name" => "Nien Nunb" }
],
"starshipClass" => "Light freighter"
},
"vehicle" => {
"id" => "82948950",
"name" => "Snowspeeder",
"model" => "t-47 airspeeder",
"cargoCapacity" => 10.0,
"consumables" => "none",
"costInCredits" => nil,
"created_at" => "2014-12-15 12:22:12 UTC",
"crew" => "2",
"length" => 4.5,
"manufacturer" => "Incom corporation",
"maxAtmospheringSpeed" => 650,
"passengers" => "0",
"pilots" => [
{ "name" => "Luke Skywalker" },
{ "name" => "Wedge Antilles" },
{ "name" => "Luke Skywalker" },
{ "name" => "Wedge Antilles" },
],
"vehicleClass"=>"airspeeder"
}
}
}
Expand All @@ -88,7 +109,7 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest

def full_graphql_query
"
query Full($personID: ID, $filmID: ID, $planetID: ID, $starshipID: ID, $speciesID: ID) {
query Full($personID: ID, $filmID: ID, $planetID: ID, $starshipID: ID, $speciesID: ID, $vehicleID: ID) {
person(id: $personID) {
birthYear
eyeColor
Expand Down Expand Up @@ -152,6 +173,22 @@ def full_graphql_query
pilots { name }
starshipClass
}
vehicle(id: $vehicleID) {
id
name
model
cargoCapacity
consumables
costInCredits
created_at
crew
length
manufacturer
maxAtmospheringSpeed
passengers
pilots { name }
vehicleClass
}
}
"
end
Expand All @@ -162,13 +199,15 @@ def default_variables
film = films(:"a-new-hope")
planet = planets(:alderaan)
species = species(:hutt)
vehicle = vehicles(:snowspeeder)

{
"starshipID" => starship.id,
"personID" => person.id,
"filmID" => film.id,
"planetID" => planet.id,
"speciesID" => species.id
"speciesID" => species.id,
"vehicleID" => vehicle.id
}
end
end