Skip to content

Add __repr__ and __str__ to ExecutionResult #81

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

Closed
Closed
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
28 changes: 27 additions & 1 deletion graphql/execution/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
from ..error import GraphQLError
import json
import six

from ..error import GraphQLError, format_error
from ..language import ast
from ..type.definition import GraphQLInterfaceType, GraphQLUnionType
from ..type.directives import GraphQLIncludeDirective, GraphQLSkipDirective
Expand Down Expand Up @@ -101,6 +104,29 @@ def __eq__(self, other):
)
)

def __repr__(self):
return str(self.response)

def __str__(self):
return json.dumps(self.response)

@staticmethod
def _format_error(error):
if isinstance(error, GraphQLError):
return format_error(error)
return {'message': six.text_type(error)}

@property
def response(self):
"""Returns the object as a GraphQL response.
https://facebook.github.io/graphql/#sec-Response"""
response = {}
if self.data and not self.invalid:
response['data'] = dict(self.data)
if self.errors:
response['errors'] = [self._format_error(e) for e in self.errors]
return response


def get_operation_root_type(schema, operation):
op = operation.operation
Expand Down
30 changes: 29 additions & 1 deletion graphql/execution/tests/test_execute_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphql.execution import execute
from graphql.execution import execute, ExecutionResult
from graphql.language.parser import parse
from graphql.type import (GraphQLArgument, GraphQLBoolean, GraphQLField,
GraphQLID, GraphQLInt, GraphQLList, GraphQLNonNull,
Expand Down Expand Up @@ -185,3 +185,31 @@ def __init__(self, uid, width, height):
}
}
}


def test_execution_result():

# Success
assert ExecutionResult(data={'foo': 'bar'}).response == \
{'data': {'foo': 'bar'}}

# Error
assert ExecutionResult(errors=['foo', 'bar']).response == \
{'errors': [{'message': 'foo'}, {'message': 'bar'}]}

# Partial success
assert ExecutionResult(data={'foo': 'bar'}, errors=['foo', 'bar']).response == \
{'data': {'foo': 'bar'}, 'errors': [{'message': 'foo'}, {'message': 'bar'}]}

# No arguments
assert ExecutionResult().response == {}

# Invalid
assert ExecutionResult(invalid=True).response == {}

# __repr__ and __str__
result = ExecutionResult(data={'foo': 'bar'}, errors=['foo', 'bar'])
assert "'data'" in repr(result)
assert "'errors'" in repr(result)
assert '"data"' in str(result)
assert '"errors"' in str(result)