Skip to content

Commit 4539714

Browse files
committed
Add first-class documentation for Node interface
1 parent bfa7d02 commit 4539714

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

guides/relay/object_identification.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,48 @@ MySchema = GraphQL::Schema.define do
4545
end
4646
```
4747

48-
### UUID fields
49-
50-
To participate in Relay's caching and refetching, objects must do two things:
48+
### Node interface
5149

52-
- Implement the `"Node"` interface
53-
- Define an `"id"` field which returns a UUID
50+
One requirement for Relay's object management is implementing the `"Node"` interface.
5451

55-
To implement the node interface, include `GraphQL::Relay::Node.interface` in your list of interfaces:
52+
To implement the node interface, include {{ "GraphQL::Relay::Node.interface" | api_doc }} in your list of interfaces:
5653

5754
```ruby
58-
PostType = GraphQL::ObjectType.define do
55+
Types::PostType = GraphQL::ObjectType.define do
5956
name "Post"
6057
# Implement the "Node" interface for Relay
6158
implements GraphQL::Relay::Node.interface
6259
# ...
6360
end
6461
```
6562

63+
To tell GraphQL how to resolve members of the `"Node"` interface, you must also define `Schema#resolve_type`:
64+
65+
```ruby
66+
MySchema = GraphQL::Schema.define do
67+
# You'll also need to define `resolve_type` for
68+
# telling the schema what type Relay `Node` objects are
69+
resolve_type ->(obj, ctx) {
70+
case obj
71+
when Post
72+
Types::PostType
73+
when Comment
74+
Types::CommentType
75+
else
76+
raise("Unexpected object: #{obj}")
77+
end
78+
}
79+
end
80+
```
81+
82+
### UUID fields
83+
84+
Relay Nodes must have a field named `"id"` which returns a globally unique ID.
85+
6686
To add a UUID field named `"id"`, use the `global_id_field` helper:
6787

6888
```ruby
69-
PostType = GraphQL::ObjectType.define do
89+
Types::PostType = GraphQL::ObjectType.define do
7090
name "Post"
7191
# ...
7292
# `id` exposes the UUID
@@ -75,7 +95,7 @@ PostType = GraphQL::ObjectType.define do
7595
end
7696
```
7797

78-
Now, `PostType` can participate in Relay's UUID-based features.
98+
This field will call the previously-defined `id_from_object` function.
7999

80100
### `node` field (find-by-UUID)
81101

lib/graphql/function.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module GraphQL
1616
# @type = type
1717
# end
1818
#
19-
# argument :id, GraphQL::Id
19+
# argument :id, GraphQL::ID_TYPE
2020
#
2121
# def resolve(obj, args, ctx)
2222
# @model.find(args.id)

0 commit comments

Comments
 (0)