Skip to content

Commit

Permalink
use relay global IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Sep 16, 2015
1 parent 760abab commit a70549d
Show file tree
Hide file tree
Showing 10 changed files with 25,703 additions and 26 deletions.
25,656 changes: 25,656 additions & 0 deletions app/assets/javascripts/graphiql.js

Large diffs are not rendered by default.

13 changes: 0 additions & 13 deletions app/assets/javascripts/graphiql.min.js

This file was deleted.

32 changes: 26 additions & 6 deletions app/graph/queries/introQuery.graphql
@@ -1,16 +1,36 @@
query readHero {
newHopeHero: hero(episode: NEWHOPE) {
... heroFields
# Use GraphQL to query data from
# a Ruby on Rails backend

query heroes {
# Find the hero for this episode
hero(episode: NEWHOPE) {
# And get some information
id,
name,
__typename
# if it's a droid, you can
# access this field
... on Droid {
primaryFunction
}
}
empireHero: hero(episode: EMPIRE) {
... heroFields

# Find a node by its global ID
node(id: "SHVtYW4tMTAwMA==") {
id,
... on Character {
# Use a predefined set of fields
# (called a fragment)
...heroFields
}
}
}

fragment heroFields on Character {
name,
id,
friends(first: 1) {

friends(first: 2) {
edges {
node {
name
Expand Down
2 changes: 1 addition & 1 deletion app/graph/types/character_interface.rb
Expand Up @@ -7,7 +7,7 @@
CharacterInterface = GraphQL::InterfaceType.define do
name "Character"
description "A sentient actor in Star Wars"
field :id, !types.Int, "The unique ID of this person"
field :id, !types.ID, "The unique ID of this person"
field :name, !types.String, "The name of this person"
connection :friends, -> { CharacterInterface.connection_type }, "Friends of this person"
field :appearsIn, types[EpisodeEnum], "Episodes this person appears in", property: :appears_in_names
Expand Down
4 changes: 2 additions & 2 deletions app/graph/types/droid_type.rb
Expand Up @@ -8,9 +8,9 @@
DroidType = GraphQL::ObjectType.define do
name "Droid"
description "A robotic character in Star Wars"
interfaces [CharacterInterface]
interfaces [NodeIdentification.interface, CharacterInterface]

field :id, !types.Int, "The unique ID of this droid"
global_id_field :id
field :name, !types.String, "The name of this droid"
connection :friends, CharacterInterface.connection_type, "Friends of this droid"
field :appearsIn, types[EpisodeEnum], "Episodes this droid appears in"
Expand Down
4 changes: 2 additions & 2 deletions app/graph/types/human_type.rb
Expand Up @@ -8,9 +8,9 @@
HumanType = GraphQL::ObjectType.define do
name "Human"
description "A flesh-and-blood character in Star Wars"
interfaces [CharacterInterface]
interfaces [NodeIdentification.interface, CharacterInterface]

field :id, !types.Int, "The unique ID of this person"
global_id_field :id
field :name, !types.String, "The name of this person"
connection :friends, CharacterInterface.connection_type, "Friends of this person"
field :appearsIn, types[EpisodeEnum], "Episodes this person appears in"
Expand Down
13 changes: 13 additions & 0 deletions app/graph/types/node_identification.rb
@@ -0,0 +1,13 @@
# Todo: handle code reloads!
GraphQL::Relay::GlobalNodeIdentification.instance_variable_set(:@instance, nil)

NodeIdentification = GraphQL::Relay::GlobalNodeIdentification.define do
object_from_id -> (id) do
type_name, id = NodeIdentification.from_global_id(id)
type_name.constantize.find(id)
end

type_from_object -> (object) do
StarWarsSchema.types[object.class.name]
end
end
1 change: 1 addition & 0 deletions app/graph/types/query_type.rb
Expand Up @@ -21,4 +21,5 @@

field :human, HumanType, field: FetchField.new(type: HumanType, model: Human)
field :droid, DroidType, field: FetchField.new(type: DroidType, model: Droid)
field :node, field: NodeIdentification.field
end
2 changes: 1 addition & 1 deletion app/views/pages/graphiql.html.erb
Expand Up @@ -5,7 +5,7 @@
<%= stylesheet_link_tag "graphiql" %>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.9.0/fetch.min.js"></script>
<%= javascript_include_tag 'graphiql.min' %>
<%= javascript_include_tag 'graphiql' %>
</head>
<body>
Loading...
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/assets.rb
Expand Up @@ -8,4 +8,4 @@

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += %w( graphiql.min.js graphiql.css )
Rails.application.config.assets.precompile += %w( graphiql.js graphiql.css )

0 comments on commit a70549d

Please sign in to comment.