@@ -45,28 +45,48 @@ MySchema = GraphQL::Schema.define do
45
45
end
46
46
```
47
47
48
- ### UUID fields
49
-
50
- To participate in Relay's caching and refetching, objects must do two things:
48
+ ### Node interface
51
49
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.
54
51
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:
56
53
57
54
``` ruby
58
- PostType = GraphQL ::ObjectType .define do
55
+ Types :: PostType = GraphQL ::ObjectType .define do
59
56
name " Post"
60
57
# Implement the "Node" interface for Relay
61
58
implements GraphQL ::Relay ::Node .interface
62
59
# ...
63
60
end
64
61
```
65
62
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
+
66
86
To add a UUID field named ` "id" ` , use the ` global_id_field ` helper:
67
87
68
88
``` ruby
69
- PostType = GraphQL ::ObjectType .define do
89
+ Types :: PostType = GraphQL ::ObjectType .define do
70
90
name " Post"
71
91
# ...
72
92
# `id` exposes the UUID
@@ -75,7 +95,7 @@ PostType = GraphQL::ObjectType.define do
75
95
end
76
96
```
77
97
78
- Now, ` PostType ` can participate in Relay's UUID-based features .
98
+ This field will call the previously-defined ` id_from_object ` function .
79
99
80
100
### ` node ` field (find-by-UUID)
81
101
0 commit comments