Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export directive to support batch processing. #99

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions graphql/execution/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ class ExecutionResult(object):
query, `errors` is null if no errors occurred, and is a
non-empty array if an error occurred."""

__slots__ = 'data', 'errors', 'invalid'
__slots__ = 'data', 'errors', 'invalid', 'variable_values'

def __init__(self, data=None, errors=None, invalid=False):
def __init__(self, data=None, errors=None, invalid=False, variable_values=None):
self.data = data
self.errors = errors
self.variable_values = variable_values

if invalid:
assert data is None
Expand Down
10 changes: 9 additions & 1 deletion graphql/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from six import string_types
from promise import Promise, promise_for_dict, is_thenable

from ..type.directives import GraphQLExportDirective
from ..error import GraphQLError, GraphQLLocatedError
from ..pyutils.default_ordered_dict import DefaultOrderedDict
from ..pyutils.ordereddict import OrderedDict
Expand Down Expand Up @@ -72,7 +73,7 @@ def on_rejected(error):

def on_resolve(data):
if not context.errors:
return ExecutionResult(data=data)
return ExecutionResult(data=data, variable_values=context.variable_values)
return ExecutionResult(data=data, errors=context.errors)

promise = Promise(executor).catch(on_rejected).then(on_resolve)
Expand Down Expand Up @@ -133,6 +134,13 @@ def execute_fields(exe_context, parent_type, source_value, fields):

for response_name, field_asts in fields.items():
result = resolve_field(exe_context, parent_type, source_value, field_asts)
directives = field_asts[0].directives
if directives:
for directive in directives:
if directive.name.value == GraphQLExportDirective.name:
variable_name = directive.arguments[0].value.value
exe_context.variable_values.setdefault(variable_name, []).append(result)

if result is Undefined:
continue

Expand Down
7 changes: 7 additions & 0 deletions graphql/execution/tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def test_if_true_includes_scalar():
assert result.data == {'a': 'a', 'b': 'b'}


def test_export():
result = execute_test_query('{ a, b @export(as: "ids") }')
assert not result.errors
assert result.data == {'a': 'a', 'b': 'b'}
assert result.variable_values == {'ids': ['b']}


def test_if_false_omits_on_scalar():
result = execute_test_query('{ a, b @include(if: false) }')
assert not result.errors
Expand Down
1 change: 0 additions & 1 deletion graphql/execution/tests/test_nonnull.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def test_nulls_a_nullable_field_that_throws_sync():
sync
}
'''

check(doc, ThrowingData(), {
'data': {'sync': None},
'errors': [{'locations': [{'column': 13, 'line': 3}], 'message': str(sync_error)}]
Expand Down
15 changes: 15 additions & 0 deletions graphql/type/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ def __init__(self, name, description=None, args=None, locations=None):
]
)

"""Used to export variable values as the result of a query (for batching)."""
GraphQLExportDirective = GraphQLDirective(
name='export',
description='Exports variable values from the results of the query.',
args={
'as': GraphQLArgument(
type=GraphQLNonNull(GraphQLString),
description='Variable name used for export.',
),
},
locations=[
DirectiveLocation.FIELD
]
)

"""Constant string used for default reason for a deprecation."""
DEFAULT_DEPRECATION_REASON = 'No longer supported'

Expand Down