From 5a59768e44dd64391d53af7e503e8dd81354062c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sat, 11 Feb 2017 13:42:44 -0500 Subject: [PATCH 1/2] Vehicle type and transport interface --- app/models/graph/schema.rb | 3 ++ app/models/graph/types/query.rb | 9 ++++ app/models/graph/types/starship.rb | 36 +++++++++------ app/models/graph/types/transport_interface.rb | 20 ++++++++ app/models/graph/types/vehicle.rb | 46 +++++++++++++++++++ test/controllers/graphql_controller_test.rb | 43 ++++++++++++++++- 6 files changed, 140 insertions(+), 17 deletions(-) create mode 100644 app/models/graph/types/transport_interface.rb create mode 100644 app/models/graph/types/vehicle.rb diff --git a/app/models/graph/schema.rb b/app/models/graph/schema.rb index 9d5aed4..a8a066b 100644 --- a/app/models/graph/schema.rb +++ b/app/models/graph/schema.rb @@ -1,5 +1,8 @@ module Graph Schema = GraphQL::Schema.define do query Graph::Types::Query + resolve_type ->(obj, ctx) do + MySchema.types.values.find { |type| type.name == obj.class.name } + end end end diff --git a/app/models/graph/types/query.rb b/app/models/graph/types/query.rb index 006a471..b282721 100644 --- a/app/models/graph/types/query.rb +++ b/app/models/graph/types/query.rb @@ -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 diff --git a/app/models/graph/types/starship.rb b/app/models/graph/types/starship.rb index cb6a1da..0b96e3e 100644 --- a/app/models/graph/types/starship.rb +++ b/app/models/graph/types/starship.rb @@ -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 @@ -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 diff --git a/app/models/graph/types/transport_interface.rb b/app/models/graph/types/transport_interface.rb new file mode 100644 index 0000000..04048a1 --- /dev/null +++ b/app/models/graph/types/transport_interface.rb @@ -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 diff --git a/app/models/graph/types/vehicle.rb b/app/models/graph/types/vehicle.rb new file mode 100644 index 0000000..1557bb9 --- /dev/null +++ b/app/models/graph/types/vehicle.rb @@ -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 diff --git a/test/controllers/graphql_controller_test.rb b/test/controllers/graphql_controller_test.rb index 2e151f2..a6e2cfc 100644 --- a/test/controllers/graphql_controller_test.rb +++ b/test/controllers/graphql_controller_test.rb @@ -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" } } } @@ -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 @@ -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 @@ -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 From efd6553a645208de7fae304c88a3fb72673215c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sat, 11 Feb 2017 13:57:38 -0500 Subject: [PATCH 2/2] Schema typo --- app/models/graph/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/graph/schema.rb b/app/models/graph/schema.rb index a8a066b..d851cd8 100644 --- a/app/models/graph/schema.rb +++ b/app/models/graph/schema.rb @@ -2,7 +2,7 @@ module Graph Schema = GraphQL::Schema.define do query Graph::Types::Query resolve_type ->(obj, ctx) do - MySchema.types.values.find { |type| type.name == obj.class.name } + Graph::Schema.types.values.find { |type| type.name == obj.class.name } end end end