Skip to content

Commit dd38759

Browse files
committed
Fixed tests
1 parent 42a5ac3 commit dd38759

File tree

7 files changed

+347
-322
lines changed

7 files changed

+347
-322
lines changed

graphql/execution/experimental/executor.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import logging
2-
31
from promise import Promise
42

53
from ...type import GraphQLSchema
@@ -9,8 +7,6 @@
97
from ..middleware import MiddlewareManager
108
from .fragment import Fragment
119

12-
logger = logging.getLogger(__name__)
13-
1410

1511
def execute(schema, document_ast, root_value=None, context_value=None,
1612
variable_values=None, operation_name=None, executor=None,

graphql/execution/experimental/resolver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import collections
23
from functools import partial
34

@@ -8,6 +9,7 @@
89
GraphQLNonNull, GraphQLObjectType, GraphQLScalarType,
910
GraphQLUnionType)
1011
from ..base import default_resolve_fn
12+
from ...execution import executor
1113

1214
try:
1315
from itertools import imap
@@ -28,7 +30,7 @@ def on_complete_resolver(on_error, __func, exe_context, info, __resolver, *args,
2830
if isinstance(result, Exception):
2931
return on_error(result)
3032
# return Promise.resolve(result).then(__func).catch(on_error)
31-
if is_promise(result):
33+
if is_thenable(result):
3234
# TODO: Remove this, if a promise is resolved with an Exception,
3335
# it should raise by default. This is fixing an old behavior
3436
# in the Promise package
@@ -120,6 +122,10 @@ def on_error(exe_context, info, catch_error, e):
120122
error = GraphQLLocatedError(info.field_asts, original_error=e)
121123
if catch_error:
122124
exe_context.errors.append(error)
125+
executor.logger.exception("An error occurred while resolving field {}.{}".format(
126+
info.parent_type.name, info.field_name
127+
))
128+
error.stack = sys.exc_info()[2]
123129
return None
124130
raise error
125131

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# import pytest
2+
3+
# from promise import Promise
4+
5+
# from ....language import ast
6+
# from ....type import (GraphQLEnumType, GraphQLField, GraphQLInt,
7+
# GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
8+
# GraphQLObjectType, GraphQLScalarType, GraphQLSchema,
9+
# GraphQLString, GraphQLUnionType)
10+
# from ..fragment import Fragment
11+
# from ..resolver import type_resolver
12+
13+
# SIZE = 10000
14+
15+
16+
# def test_experimental_big_list_of_ints(benchmark):
17+
# big_int_list = [x for x in range(SIZE)]
18+
19+
# resolver = type_resolver(GraphQLList(GraphQLInt), lambda: big_int_list)
20+
# result = benchmark(resolver)
21+
22+
# assert result == big_int_list
23+
24+
25+
# def test_experimental_big_list_of_nested_ints(benchmark):
26+
# big_int_list = [x for x in range(SIZE)]
27+
28+
# Node = GraphQLObjectType(
29+
# 'Node',
30+
# fields={
31+
# 'id': GraphQLField(
32+
# GraphQLInt,
33+
# resolver=lambda obj,
34+
# args,
35+
# context,
36+
# info: obj)})
37+
# selection_set = ast.SelectionSet(selections=[
38+
# ast.Field(
39+
# alias=None,
40+
# name=ast.Name(value='id'),
41+
# arguments=[],
42+
# directives=[],
43+
# selection_set=None
44+
# )
45+
# ])
46+
# fragment = Fragment(type=Node, selection_set=selection_set)
47+
# type = GraphQLList(Node)
48+
# resolver = type_resolver(type, lambda: big_int_list, fragment=fragment)
49+
# resolved = benchmark(resolver)
50+
51+
# assert resolved == [{
52+
# 'id': n
53+
# } for n in big_int_list]
54+
55+
56+
# def test_experimental_big_list_of_objecttypes_with_two_int_fields(benchmark):
57+
# big_int_list = [x for x in range(SIZE)]
58+
59+
# Node = GraphQLObjectType('Node', fields={
60+
# 'id': GraphQLField(GraphQLInt, resolver=lambda obj, args, context, info: obj),
61+
# 'ida': GraphQLField(GraphQLInt, resolver=lambda obj, args, context, info: obj * 2)
62+
# })
63+
# selection_set = ast.SelectionSet(selections=[
64+
# ast.Field(
65+
# alias=None,
66+
# name=ast.Name(value='id'),
67+
# arguments=[],
68+
# directives=[],
69+
# selection_set=None
70+
# ),
71+
# ast.Field(
72+
# alias=None,
73+
# name=ast.Name(value='ida'),
74+
# arguments=[],
75+
# directives=[],
76+
# selection_set=None
77+
# )
78+
# ])
79+
# fragment = Fragment(type=Node, selection_set=selection_set)
80+
# type = GraphQLList(Node)
81+
# resolver = type_resolver(type, lambda: big_int_list, fragment=fragment)
82+
# resolved = benchmark(resolver)
83+
84+
# assert resolved == [{
85+
# 'id': n,
86+
# 'ida': n * 2
87+
# } for n in big_int_list]
88+
89+
90+
# def test_experimental_big_list_of_objecttypes_with_one_int_field(benchmark):
91+
# big_int_list = [x for x in range(SIZE)]
92+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt, resolver=lambda obj, *_, **__: obj)})
93+
# Query = GraphQLObjectType(
94+
# 'Query',
95+
# fields={
96+
# 'nodes': GraphQLField(
97+
# GraphQLList(Node),
98+
# resolver=lambda *_,
99+
# **__: big_int_list)})
100+
# node_selection_set = ast.SelectionSet(selections=[
101+
# ast.Field(
102+
# name=ast.Name(value='id'),
103+
# )
104+
# ])
105+
# selection_set = ast.SelectionSet(selections=[
106+
# ast.Field(
107+
# name=ast.Name(value='nodes'),
108+
# selection_set=node_selection_set
109+
# )
110+
# ])
111+
# query_fragment = Fragment(type=Query, selection_set=selection_set)
112+
# resolver = type_resolver(Query, lambda: object(), fragment=query_fragment)
113+
# resolved = benchmark(resolver)
114+
# assert resolved == {
115+
# 'nodes': [{
116+
# 'id': n
117+
# } for n in big_int_list]
118+
# }
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# import pytest
2+
3+
# from promise import Promise
4+
5+
# from ....language import ast
6+
# from ....language.parser import parse
7+
# from ....type import (GraphQLEnumType, GraphQLField, GraphQLInt,
8+
# GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
9+
# GraphQLObjectType, GraphQLScalarType, GraphQLSchema,
10+
# GraphQLString, GraphQLUnionType)
11+
# from ...base import ExecutionContext
12+
# from ..fragment import Fragment
13+
# from ..resolver import type_resolver
14+
15+
16+
# def test_fragment_equal():
17+
# selection1 = ast.SelectionSet(selections=[
18+
# ast.Field(
19+
# name=ast.Name(value='id'),
20+
# )
21+
# ])
22+
# selection2 = ast.SelectionSet(selections=[
23+
# ast.Field(
24+
# name=ast.Name(value='id'),
25+
# )
26+
# ])
27+
# assert selection1 == selection2
28+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt)})
29+
# Node2 = GraphQLObjectType('Node2', fields={'id': GraphQLField(GraphQLInt)})
30+
# fragment1 = Fragment(type=Node, selection_set=selection1)
31+
# fragment2 = Fragment(type=Node, selection_set=selection2)
32+
# fragment3 = Fragment(type=Node2, selection_set=selection2)
33+
# assert fragment1 == fragment2
34+
# assert fragment1 != fragment3
35+
# assert fragment1 != object()
36+
37+
38+
# def test_fragment_resolver():
39+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt, resolver=lambda obj, *_, **__: obj * 2)})
40+
# selection_set = ast.SelectionSet(selections=[
41+
# ast.Field(
42+
# name=ast.Name(value='id'),
43+
# )
44+
# ])
45+
# fragment = Fragment(type=Node, selection_set=selection_set)
46+
# assert fragment.resolve(1) == {'id': 2}
47+
# assert fragment.resolve(2) == {'id': 4}
48+
49+
50+
# def test_fragment_resolver_list():
51+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt, resolver=lambda obj, *_, **__: obj)})
52+
# selection_set = ast.SelectionSet(selections=[
53+
# ast.Field(
54+
# name=ast.Name(value='id'),
55+
# )
56+
# ])
57+
# fragment = Fragment(type=Node, selection_set=selection_set)
58+
# type = GraphQLList(Node)
59+
60+
# resolver = type_resolver(type, lambda: range(3), fragment=fragment)
61+
# resolved = resolver()
62+
# assert resolved == [{
63+
# 'id': n
64+
# } for n in range(3)]
65+
66+
67+
# def test_fragment_resolver_nested():
68+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt, resolver=lambda obj, *_, **__: obj)})
69+
# Query = GraphQLObjectType('Query', fields={'node': GraphQLField(Node, resolver=lambda *_, **__: 1)})
70+
# node_selection_set = ast.SelectionSet(selections=[
71+
# ast.Field(
72+
# name=ast.Name(value='id'),
73+
# )
74+
# ])
75+
# selection_set = ast.SelectionSet(selections=[
76+
# ast.Field(
77+
# name=ast.Name(value='node'),
78+
# selection_set=node_selection_set
79+
# )
80+
# ])
81+
# # node_fragment = Fragment(type=Node, field_asts=node_field_asts)
82+
# query_fragment = Fragment(type=Query, selection_set=selection_set)
83+
# resolver = type_resolver(Query, lambda: object(), fragment=query_fragment)
84+
# resolved = resolver()
85+
# assert resolved == {
86+
# 'node': {
87+
# 'id': 1
88+
# }
89+
# }
90+
91+
92+
# def test_fragment_resolver_abstract():
93+
# Node = GraphQLInterfaceType('Node', fields={'id': GraphQLField(GraphQLInt)})
94+
# Person = GraphQLObjectType(
95+
# 'Person',
96+
# interfaces=(
97+
# Node,
98+
# ),
99+
# is_type_of=lambda *_: True,
100+
# fields={
101+
# 'id': GraphQLField(
102+
# GraphQLInt,
103+
# resolver=lambda obj,
104+
# *_,
105+
# **__: obj)})
106+
# Query = GraphQLObjectType('Query', fields={'node': GraphQLField(Node, resolver=lambda *_, **__: 1)})
107+
# node_selection_set = ast.SelectionSet(selections=[
108+
# ast.Field(
109+
# name=ast.Name(value='id'),
110+
# )
111+
# ])
112+
# selection_set = ast.SelectionSet(selections=[
113+
# ast.Field(
114+
# name=ast.Name(value='node'),
115+
# selection_set=node_selection_set
116+
# )
117+
# ])
118+
# # node_fragment = Fragment(type=Node, field_asts=node_field_asts)
119+
# schema = GraphQLSchema(query=Query, types=[Person])
120+
# document_ast = parse('''{
121+
# node {
122+
# id
123+
# }
124+
# }''')
125+
126+
# root_value = None
127+
# context_value = None
128+
# operation_name = None
129+
# variable_values = {}
130+
# executor = None
131+
# middlewares = None
132+
# context = ExecutionContext(
133+
# schema,
134+
# document_ast,
135+
# root_value,
136+
# context_value,
137+
# variable_values,
138+
# operation_name,
139+
# executor,
140+
# middlewares
141+
# )
142+
143+
# query_fragment = Fragment(type=Query, selection_set=selection_set, context=context)
144+
# resolver = type_resolver(Query, lambda: object(), fragment=query_fragment)
145+
# resolved = resolver()
146+
# assert resolved == {
147+
# 'node': {
148+
# 'id': 1
149+
# }
150+
# }
151+
152+
153+
# def test_fragment_resolver_nested_list():
154+
# Node = GraphQLObjectType('Node', fields={'id': GraphQLField(GraphQLInt, resolver=lambda obj, *_, **__: obj)})
155+
# Query = GraphQLObjectType(
156+
# 'Query',
157+
# fields={
158+
# 'nodes': GraphQLField(
159+
# GraphQLList(Node),
160+
# resolver=lambda *_,
161+
# **__: range(3))})
162+
# node_selection_set = ast.SelectionSet(selections=[
163+
# ast.Field(
164+
# name=ast.Name(value='id'),
165+
# )
166+
# ])
167+
# selection_set = ast.SelectionSet(selections=[
168+
# ast.Field(
169+
# name=ast.Name(value='nodes'),
170+
# selection_set=node_selection_set
171+
# )
172+
# ])
173+
# # node_fragment = Fragment(type=Node, field_asts=node_field_asts)
174+
# query_fragment = Fragment(type=Query, selection_set=selection_set)
175+
# resolver = type_resolver(Query, lambda: object(), fragment=query_fragment)
176+
# resolved = resolver()
177+
# assert resolved == {
178+
# 'nodes': [{
179+
# 'id': n
180+
# } for n in range(3)]
181+
# }
182+
183+
# # '''
184+
# # {
185+
# # books {
186+
# # title
187+
# # author {
188+
# # name
189+
# # }
190+
# # }
191+
# # }'''
192+
# # BooksFragment(
193+
# # ('title', str(resolve_title())),
194+
# # ('author', AuthorFragment(
195+
# # ('name', str(resolve_author()))
196+
# # ))
197+
# # )

0 commit comments

Comments
 (0)