forked from graphql-python/flask-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
74 lines (63 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
from graphql.type.definition import GraphQLArgument, GraphQLField, GraphQLNonNull, GraphQLObjectType, GraphQLList
from graphql.type.scalars import GraphQLString, GraphQLScalarType
from graphql.type.schema import GraphQLSchema
def resolve_test_file(obj, info, what):
output = what.readline().decode('utf-8')
what.seek(0)
return output
def resolve_test_files(obj, info, whats):
output = ''.join(what.readline().decode('utf-8') for what in whats)
for what in whats:
what.seek(0)
return output
def resolve_raises(*_):
raise Exception("Throws!")
# This scalar should be added to graphql-core at some point
GraphQLUpload = GraphQLScalarType(
name="Upload",
description="The `Upload` scalar type represents an uploaded file",
serialize=lambda x: None,
parse_value=lambda x: x,
parse_literal=lambda x: x,
)
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
),
'testFile': GraphQLField(
type=GraphQLString,
args={
'what': GraphQLArgument(GraphQLNonNull(GraphQLUpload)),
},
resolver=resolve_test_file,
),
'testMultiFile': GraphQLField(
type=GraphQLString,
args={
'whats': GraphQLArgument(GraphQLNonNull(GraphQLList(GraphQLUpload))),
},
resolver=resolve_test_files,
)
}
)
MutationRootType = GraphQLObjectType(
name='MutationRoot',
fields={
'writeTest': GraphQLField(
type=QueryRootType,
resolver=lambda *_: QueryRootType
)
}
)
Schema = GraphQLSchema(QueryRootType, MutationRootType)