Skip to content

Commit

Permalink
Removed tracing middleware which should be in graphql-server-core
Browse files Browse the repository at this point in the history
  • Loading branch information
ekampf committed Nov 1, 2017
1 parent 9ac6bee commit c67320c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 236 deletions.
24 changes: 3 additions & 21 deletions graphql/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
get_operation_root_type, SubscriberExecutionContext)
from .executors.sync import SyncExecutor
from .middleware import MiddlewareManager
from .tracing import TracingMiddleware

logger = logging.getLogger(__name__)

Expand All @@ -31,8 +30,7 @@ def subscribe(*args, **kwargs):

def execute(schema, document_ast, root_value=None, context_value=None,
variable_values=None, operation_name=None, executor=None,
return_promise=False, middleware=None, allow_subscriptions=False,
tracing=False):
return_promise=False, middleware=None, allow_subscriptions=False):
assert schema, 'Must provide schema'
assert isinstance(schema, GraphQLSchema), (
'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
Expand All @@ -48,17 +46,6 @@ def execute(schema, document_ast, root_value=None, context_value=None,
' of MiddlewareManager. Received "{}".'.format(middleware)
)

tracing_middleware = None
if tracing:
tracing_middleware = TracingMiddleware()
tracing_middleware.start()

if middleware:
middleware.middlewares.insert(0, tracing_middleware)
else:
middleware = MiddlewareManager(tracing_middleware)


if executor is None:
executor = SyncExecutor()

Expand All @@ -85,15 +72,10 @@ def on_resolve(data):
if isinstance(data, Observable):
return data

extensions = dict()
if tracing_middleware:
tracing_middleware.end()
extensions['tracing'] = tracing_middleware.tracing_dict

if not context.errors:
return ExecutionResult(data=data, extensions=extensions)
return ExecutionResult(data=data)

return ExecutionResult(data=data, extensions=extensions, errors=context.errors)
return ExecutionResult(data=data, errors=context.errors)

promise = Promise.resolve(None).then(executor).catch(on_rejected).then(on_resolve)

Expand Down
147 changes: 146 additions & 1 deletion graphql/execution/tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from graphql.language.parser import parse
from graphql.type import (GraphQLArgument, GraphQLBoolean, GraphQLField,
GraphQLInt, GraphQLList, GraphQLObjectType,
GraphQLSchema, GraphQLString)
GraphQLSchema, GraphQLString, GraphQLNonNull, GraphQLID)
from promise import Promise


Expand Down Expand Up @@ -668,3 +668,148 @@ def resolve(self, next, *args, **kwargs):
middleware=middlewares_without_promise)
assert result1.data == result2.data and result1.data == {
'ok': 'ok', 'not_ok': 'not_ok'}


def test_executor_properly_propogates_path_data(mocker):
time_mock = mocker.patch('time.time')
time_mock.side_effect = range(0, 10000)

BlogImage = GraphQLObjectType('BlogImage', {
'url': GraphQLField(GraphQLString),
'width': GraphQLField(GraphQLInt),
'height': GraphQLField(GraphQLInt),
})

BlogAuthor = GraphQLObjectType('Author', lambda: {
'id': GraphQLField(GraphQLString),
'name': GraphQLField(GraphQLString),
'pic': GraphQLField(BlogImage,
args={
'width': GraphQLArgument(GraphQLInt),
'height': GraphQLArgument(GraphQLInt),
},
resolver=lambda obj, info, **args:
obj.pic(args['width'], args['height'])
),
'recentArticle': GraphQLField(BlogArticle),
})

BlogArticle = GraphQLObjectType('Article', {
'id': GraphQLField(GraphQLNonNull(GraphQLString)),
'isPublished': GraphQLField(GraphQLBoolean),
'author': GraphQLField(BlogAuthor),
'title': GraphQLField(GraphQLString),
'body': GraphQLField(GraphQLString),
'keywords': GraphQLField(GraphQLList(GraphQLString)),
})

BlogQuery = GraphQLObjectType('Query', {
'article': GraphQLField(
BlogArticle,
args={'id': GraphQLArgument(GraphQLID)},
resolver=lambda obj, info, **args: Article(args['id'])),
'feed': GraphQLField(
GraphQLList(BlogArticle),
resolver=lambda *_: map(Article, range(1, 2 + 1))),
})

BlogSchema = GraphQLSchema(BlogQuery)

class Article(object):

def __init__(self, id):
self.id = id
self.isPublished = True
self.author = Author()
self.title = 'My Article {}'.format(id)
self.body = 'This is a post'
self.hidden = 'This data is not exposed in the schema'
self.keywords = ['foo', 'bar', 1, True, None]

class Author(object):
id = 123
name = 'John Smith'

def pic(self, width, height):
return Pic(123, width, height)

@property
def recentArticle(self): return Article(1)

class Pic(object):
def __init__(self, uid, width, height):
self.url = 'cdn://{}'.format(uid)
self.width = str(width)
self.height = str(height)

class PathCollectorMiddleware(object):
def __init__(self):
self.paths = []

def resolve(self, _next, root, info, *args, **kwargs):
self.paths.append(info.path)
return _next(root, info, *args, **kwargs)

request = '''
{
feed {
id
...articleFields
author {
id
name
}
},
}
fragment articleFields on Article {
title,
body,
hidden,
}
'''

paths_middleware = PathCollectorMiddleware()

result = execute(BlogSchema, parse(request), middleware=(paths_middleware, ))
assert not result.errors
assert result.data == \
{
"feed": [
{
"id": "1",
"title": "My Article 1",
"body": "This is a post",
"author": {
"id": "123",
"name": "John Smith"
}
},
{
"id": "2",
"title": "My Article 2",
"body": "This is a post",
"author": {
"id": "123",
"name": "John Smith"
}
},
],
}

traversed_paths = paths_middleware.paths
assert traversed_paths == [
['feed'],
['feed', 0, 'id'],
['feed', 0, 'title'],
['feed', 0, 'body'],
['feed', 0, 'author'],
['feed', 1, 'id'],
['feed', 1, 'title'],
['feed', 1, 'body'],
['feed', 1, 'author'],
['feed', 0, 'author', 'id'],
['feed', 0, 'author', 'name'],
['feed', 1, 'author', 'id'],
['feed', 1, 'author', 'name']
]

153 changes: 0 additions & 153 deletions graphql/execution/tests/test_tracing.py

This file was deleted.

Loading

0 comments on commit c67320c

Please sign in to comment.