forked from preply/graphene-federation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.py
65 lines (49 loc) · 1.37 KB
/
entities.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import graphene
from graphene_federation import build_schema, key
def get_file_by_id(id):
return File(**{'id': id, 'name': 'test_name'})
@key(fields='id')
class File(graphene.ObjectType):
id = graphene.Int(required=True)
name = graphene.String()
def resolve_id(self, info, **kwargs):
return 1
def resolve_name(self, info, **kwargs):
return self.name
def __resolve_reference(self, info, **kwargs):
return get_file_by_id(self.id)
class Query(graphene.ObjectType):
file = graphene.Field(File)
def resolve_file(self, **kwargs):
return None # no direct access
schema = build_schema(Query)
query = '''
query getSDL {
_service {
sdl
}
}
'''
result = schema.execute(query)
print(result.data)
# OrderedDict([('_service', OrderedDict([('sdl', ' type File @key(fields: "id") { id: Int! name: String } extend type Query { hello: String file: File } ')]))])
query ='''
query entities($_representations: [_Any!]!) {
_entities(representations: $_representations) {
... on File {
id
name
}
}
}
'''
result = schema.execute(query, variables={
"_representations": [
{
"__typename": "File",
"id": 1
}
]
})
print(result.data)
# OrderedDict([('_entities', [OrderedDict([('id', 1), ('name', 'test_name')])])])