-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathschema.py
85 lines (69 loc) · 2.23 KB
/
schema.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from graphql.type.definition import (
GraphQLArgument, GraphQLField, GraphQLNonNull, GraphQLObjectType,
GraphQLScalarType)
from graphql.type.scalars import GraphQLString
from graphql.type.schema import GraphQLSchema
def resolve_raises(*_):
raise Exception("Throws!")
QueryRootType = GraphQLObjectType(
name='QueryRoot',
fields={
'thrower': GraphQLField(GraphQLNonNull(GraphQLString), resolver=resolve_raises),
'request': GraphQLField(GraphQLNonNull(GraphQLString),
resolver=lambda obj, info: info.context.args.get('q')),
'context': GraphQLField(GraphQLNonNull(GraphQLString),
resolver=lambda obj, info: info.context),
'test': GraphQLField(
type=GraphQLString,
args={
'who': GraphQLArgument(GraphQLString)
},
resolver=lambda obj, info, who='World': 'Hello %s' % who
)
}
)
FileUploadTestResult = GraphQLObjectType(
name='FileUploadTestResult',
fields={
'data': GraphQLField(GraphQLString),
'name': GraphQLField(GraphQLString),
'type': GraphQLField(GraphQLString),
}
)
GraphQLFileUpload = GraphQLScalarType(
name='FileUpload',
description='File upload',
serialize=lambda x: None,
parse_value=lambda value: value,
parse_literal=lambda node: None,
)
def to_object(dct):
class MyObject(object):
pass
obj = MyObject()
for key, val in dct.items():
setattr(obj, key, val)
return obj
def resolve_file_upload_test(obj, info, file):
data = file.stream.read().decode()
# Need to return an object, not a dict
return to_object({
'data': data,
'name': file.filename,
'type': file.content_type,
})
MutationRootType = GraphQLObjectType(
name='MutationRoot',
fields={
'writeTest': GraphQLField(
type=QueryRootType,
resolver=lambda *_: QueryRootType
),
'fileUploadTest': GraphQLField(
type=FileUploadTestResult,
args={'file': GraphQLArgument(GraphQLFileUpload)},
resolver=resolve_file_upload_test,
),
}
)
Schema = GraphQLSchema(QueryRootType, MutationRootType)