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

Commit 2c57d4c

Browse files
authored
Merge pull request #20 from GraphQLAcademy/node
Node Interface
2 parents 3509d10 + 715401b commit 2c57d4c

File tree

11 files changed

+64
-17
lines changed

11 files changed

+64
-17
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ gem 'sqlite3'
1212
# Use Puma as the app server
1313
gem 'puma', '~> 3.0'
1414
# Use GraphQL!
15-
gem 'graphql', '~> 1.4.2'
15+
gem 'graphql', '~> 1.4.3'
1616
# GraphiQL Interface
1717
gem 'graphiql-rails', '~> 1.4.1'
1818

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ GEM
4949
activesupport (>= 4.1.0)
5050
graphiql-rails (1.4.1)
5151
rails
52-
graphql (1.4.2)
52+
graphql (1.4.3)
5353
i18n (0.8.0)
5454
listen (3.0.8)
5555
rb-fsevent (~> 0.9, >= 0.9.4)
@@ -130,7 +130,7 @@ PLATFORMS
130130
DEPENDENCIES
131131
byebug
132132
graphiql-rails (~> 1.4.1)
133-
graphql (~> 1.4.2)
133+
graphql (~> 1.4.3)
134134
listen (~> 3.0.5)
135135
puma (~> 3.0)
136136
rails (~> 5.0.1)

app/models/graph/schema.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
module Graph
22
Schema = GraphQL::Schema.define do
33
query Graph::Types::Query
4+
45
resolve_type ->(obj, ctx) do
56
Graph::Schema.types.values.find { |type| type.name == obj.class.name }
67
end
8+
9+
id_from_object ->(object, type_definition, query_ctx) do
10+
URI::GID.build(app: GlobalID.app, model_name: type_definition.name, model_id: object.id)
11+
end
12+
13+
object_from_id ->(id, query_ctx) do
14+
gid = GlobalID.parse(id)
15+
possible_types = query_ctx.warden.possible_types(GraphQL::Relay::Node.interface)
16+
17+
return unless possible_types.map(&:name).include?(gid.model_name)
18+
return unless gid.app == GlobalID.app
19+
20+
Object.const_get(gid.model_name).find(gid.model_id)
21+
end
722
end
823
end

app/models/graph/types/film.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module Types
44
name "Film"
55
description "A single film."
66

7-
field :id, types.ID, "The ID of the Film."
7+
interfaces [GraphQL::Relay::Node.interface]
8+
9+
global_id_field :id
810

911
field :title, types.String, "The title of this film"
1012
field :episodeID, types.Int, "The episode number of this film.", property: :episode_id

app/models/graph/types/person.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module Types
44
name "Person"
55
description "An individual person or character within the Star Wars universe."
66

7-
field :id, types.ID, "The ID of the person."
7+
interfaces [GraphQL::Relay::Node.interface]
8+
9+
global_id_field :id
810

911
field :birthYear, types.String,
1012
"The birth year of the person, using the in-universe standard of BBY or ABY"\

app/models/graph/types/planet.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module Types
44
name "Planet"
55
description "A large mass, planet or planetoid in the Star Wars Universe, at the time of 0 ABY."
66

7-
field :id, types.ID, "The ID of the person."
7+
interfaces [GraphQL::Relay::Node.interface]
8+
9+
global_id_field :id
810

911
field :name, types.String, "The name of this planet."
1012
field :diameter, types.Int, "The diameter of this planet in kilometers."

app/models/graph/types/query.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module Types
44
name "Query"
55
description "The query root of this schema"
66

7+
# Relay
8+
field :node, GraphQL::Relay::Node.field
9+
field :nodes, GraphQL::Relay::Node.plural_field
10+
711
field :person, Graph::Types::Person do
812
argument :id, types.ID
913
resolve ->(_, args, _) { ::Person.find_by(id: args['id']) }

app/models/graph/types/species.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module Types
44
name "Species"
55
description "A type of person or character within the Star Wars Universe."
66

7-
field :id, types.ID, "The ID of this species."
7+
interfaces [GraphQL::Relay::Node.interface]
8+
9+
global_id_field :id
810

911
field :name, types.String, "The name of this species."
1012
field :classification, types.String, "The classification of this species, such as \"mammal\" or \"reptile\"."

app/models/graph/types/starship.rb

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

7-
interfaces [Graph::Types::TransportInterface]
7+
interfaces [GraphQL::Relay::Node.interface, Graph::Types::TransportInterface]
88

9-
field :id, types.ID, "The ID of this starship."
9+
global_id_field :id
1010

1111
# Starship Specific Fields
1212

app/models/graph/types/vehicle.rb

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

7-
interfaces [Graph::Types::TransportInterface]
7+
interfaces [GraphQL::Relay::Node.interface, Graph::Types::TransportInterface]
88

9-
field :id, types.ID, "The ID of this vehicle."
9+
global_id_field :id
1010

1111
# Vehicle Specific Fields
1212

0 commit comments

Comments
 (0)