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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use GraphQL!
gem 'graphql', '~> 1.4.2'
gem 'graphql', '~> 1.4.3'
# GraphiQL Interface
gem 'graphiql-rails', '~> 1.4.1'

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ GEM
activesupport (>= 4.1.0)
graphiql-rails (1.4.1)
rails
graphql (1.4.2)
graphql (1.4.3)
i18n (0.8.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand Down Expand Up @@ -130,7 +130,7 @@ PLATFORMS
DEPENDENCIES
byebug
graphiql-rails (~> 1.4.1)
graphql (~> 1.4.2)
graphql (~> 1.4.3)
listen (~> 3.0.5)
puma (~> 3.0)
rails (~> 5.0.1)
Expand Down
15 changes: 15 additions & 0 deletions app/models/graph/schema.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
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

id_from_object ->(object, type_definition, query_ctx) do
URI::GID.build(app: GlobalID.app, model_name: type_definition.name, model_id: object.id)
end

object_from_id ->(id, query_ctx) do
gid = GlobalID.parse(id)
possible_types = query_ctx.warden.possible_types(GraphQL::Relay::Node.interface)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @cjoudrey good point, I think this is more even more accurate ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Forgot about this.


return unless possible_types.map(&:name).include?(gid.model_name)
return unless gid.app == GlobalID.app

Object.const_get(gid.model_name).find(gid.model_id)
end
end
end
4 changes: 3 additions & 1 deletion app/models/graph/types/film.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Types
name "Film"
description "A single film."

field :id, types.ID, "The ID of the Film."
interfaces [GraphQL::Relay::Node.interface]

global_id_field :id

field :title, types.String, "The title of this film"
field :episodeID, types.Int, "The episode number of this film.", property: :episode_id
Expand Down
4 changes: 3 additions & 1 deletion app/models/graph/types/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Types
name "Person"
description "An individual person or character within the Star Wars universe."

field :id, types.ID, "The ID of the person."
interfaces [GraphQL::Relay::Node.interface]

global_id_field :id

field :birthYear, types.String,
"The birth year of the person, using the in-universe standard of BBY or ABY"\
Expand Down
4 changes: 3 additions & 1 deletion app/models/graph/types/planet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Types
name "Planet"
description "A large mass, planet or planetoid in the Star Wars Universe, at the time of 0 ABY."

field :id, types.ID, "The ID of the person."
interfaces [GraphQL::Relay::Node.interface]

global_id_field :id

field :name, types.String, "The name of this planet."
field :diameter, types.Int, "The diameter of this planet in kilometers."
Expand Down
4 changes: 4 additions & 0 deletions app/models/graph/types/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Types
name "Query"
description "The query root of this schema"

# Relay
field :node, GraphQL::Relay::Node.field
field :nodes, GraphQL::Relay::Node.plural_field

field :person, Graph::Types::Person do
argument :id, types.ID
resolve ->(_, args, _) { ::Person.find_by(id: args['id']) }
Expand Down
4 changes: 3 additions & 1 deletion app/models/graph/types/species.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Types
name "Species"
description "A type of person or character within the Star Wars Universe."

field :id, types.ID, "The ID of this species."
interfaces [GraphQL::Relay::Node.interface]

global_id_field :id

field :name, types.String, "The name of this species."
field :classification, types.String, "The classification of this species, such as \"mammal\" or \"reptile\"."
Expand Down
4 changes: 2 additions & 2 deletions app/models/graph/types/starship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Types
name "Starship"
description "A single transport craft that has hyperdrive capability."

interfaces [Graph::Types::TransportInterface]
interfaces [GraphQL::Relay::Node.interface, Graph::Types::TransportInterface]

field :id, types.ID, "The ID of this starship."
global_id_field :id

# Starship Specific Fields

Expand Down
4 changes: 2 additions & 2 deletions app/models/graph/types/vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Types
name "Vehicle"
description "A single transport craft that has hyperdrive capability."

interfaces [Graph::Types::TransportInterface]
interfaces [GraphQL::Relay::Node.interface, Graph::Types::TransportInterface]

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

# Vehicle Specific Fields

Expand Down
32 changes: 26 additions & 6 deletions test/controllers/graphql_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
"homeworld" => {
"name" => "Tatooine"
},
"id" => "242320449",
"id" => "gid://swapi/Person/242320449",
"mass" => 77,
"name" => "Luke Skywalker",
"skinColor" => "fair",
},
"film" => {
"director" => "George Lucas",
"episodeID" => 4,
"id" => "846649883",
"id" => "gid://swapi/Film/846649883",
"producers" => ["Gary Kurtz", "Rick McCallum"],
"releaseDate" => "1977-05-25",
"title" => "A New Hope"
Expand All @@ -30,7 +30,7 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
"climate" => "temperate",
"diameter" => 12500,
"gravity" => "1 standard",
"id" => "688730903",
"id" => "gid://swapi/Planet/688730903",
"name" => "Alderaan",
"orbitalPeriod" => 364,
"population" => 2000.0,
Expand All @@ -46,13 +46,13 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
"eyeColors" => ["yellow", "red"],
"hairColors" => ["n/a"],
"homeworld" => { "name" => "Nal Hutta" },
"id" => "299344798",
"id" => "gid://swapi/Species/299344798",
"language" => "Huttese",
"name" => "Hutt",
"skinColors" => ["green", "brown", "tan"],
},
"starship" => {
"id" => "113504200",
"id" => "gid://swapi/Starship/113504200",
"name" => "Millennium Falcon",
"model" => "YT-1300 light freighter",
"MGLT" => 75,
Expand All @@ -79,7 +79,7 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
"starshipClass" => "Light freighter"
},
"vehicle" => {
"id" => "82948950",
"id" => "gid://swapi/Vehicle/82948950",
"name" => "Snowspeeder",
"model" => "t-47 airspeeder",
"cargoCapacity" => 10.0,
Expand All @@ -105,6 +105,26 @@ class GraphQLControllerTest < ActionDispatch::IntegrationTest
assert_equal expected, JSON.parse(response.body)
end

test '#execute can execute node queries' do
starship = starships(:"millennium-falcon")

query = "
query {
node(id: \"#{starship.to_global_id.to_s}\") {
... on Starship {
name
}
}
}
"

expected = { "data" => { "node" => { "name" => "Millennium Falcon" } } }

post graphql_url, params: { query: query }

assert_equal expected, JSON.parse(response.body)
end

private

def full_graphql_query
Expand Down