Skip to content

Commit 747c64a

Browse files
committed
Removed default object value in graphql query. Add tests and refine default resolve function.
Related GraphQL-js commit: graphql/graphql-js@d506c23
1 parent 3c5ea56 commit 747c64a

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

graphql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def graphql(schema, request='', root=None, args=None, operation_name=None):
3535
return execute(
3636
schema,
3737
ast,
38-
root or object(),
38+
root,
3939
operation_name=operation_name,
4040
variable_values=args or {},
4141
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from collections import OrderedDict
2+
import json
3+
4+
from graphql import graphql
5+
from graphql.type import GraphQLSchema, GraphQLString, GraphQLObjectType, GraphQLInt, GraphQLField, GraphQLArgument
6+
7+
8+
def _test_schema(test_field):
9+
return GraphQLSchema(
10+
query=GraphQLObjectType(
11+
name='Query',
12+
fields={
13+
'test': test_field
14+
}
15+
)
16+
)
17+
18+
19+
def test_default_function_accesses_properties():
20+
schema = _test_schema(GraphQLField(GraphQLString))
21+
22+
class source:
23+
test = 'testValue'
24+
25+
result = graphql(schema, '{ test }', source)
26+
assert not result.errors
27+
assert result.data == {'test': 'testValue'}
28+
29+
30+
def test_default_function_calls_methods():
31+
schema = _test_schema(GraphQLField(GraphQLString))
32+
33+
class source:
34+
_secret = 'testValue'
35+
36+
def test(self):
37+
return self._secret
38+
39+
result = graphql(schema, '{ test }', source())
40+
assert not result.errors
41+
assert result.data == {'test': 'testValue'}
42+
43+
44+
def test_uses_provided_resolve_function():
45+
def resolver(source, args, *_):
46+
return json.dumps([source, args], separators=(',', ':'))
47+
48+
schema = _test_schema(GraphQLField(
49+
GraphQLString,
50+
args=OrderedDict([
51+
('aStr', GraphQLArgument(GraphQLString)),
52+
('aInt', GraphQLArgument(GraphQLInt)),
53+
]),
54+
resolver=resolver
55+
))
56+
57+
result = graphql(schema, '{ test }', None)
58+
assert not result.errors
59+
assert result.data == {'test': '[null,{}]'}
60+
61+
result = graphql(schema, '{ test(aStr: "String!") }', 'Source!')
62+
assert not result.errors
63+
assert result.data == {'test': '["Source!",{"aStr":"String!"}]'}
64+
65+
result = graphql(schema, '{ test(aInt: -123, aStr: "String!",) }', 'Source!')
66+
assert not result.errors
67+
assert result.data in [
68+
{'test': '["Source!",{"aStr":"String!","aInt":-123}]'},
69+
{'test': '["Source!",{"aInt":-123,"aStr":"String!"}]'}
70+
]

0 commit comments

Comments
 (0)