-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_schema.rb
34 lines (29 loc) · 971 Bytes
/
demo_schema.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class DemoSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
# Union and Interface Resolution
def self.resolve_type(abstract_type, obj, ctx)
case obj
when Product
Types::ProductType
else
raise("Unexpected object: #{obj}")
end
end
# Relay-style Object Identification:
# Return a string UUID for `object`
def self.id_from_object(object, type_definition, query_ctx)
# Here's a simple implementation which:
# - joins the type name & object.id
# - encodes it with base64:
GraphQL::Schema::UniqueWithinType.encode(type_definition.name, object.id)
end
# Given a string UUID, find the object
def self.object_from_id(id, query_ctx)
# Decode the UUIDs generated above:
type_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)
# Then, based on `type_name` and `id`
# find an object in your application
Object.const_get(type_name).find(item_id)
end
end