-
Notifications
You must be signed in to change notification settings - Fork 16
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
57 non null fields #98
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,9 +52,10 @@ class Field(object): | |
creation_counter = 0 | ||
arguments = {} | ||
|
||
def __init__(self, description=None, arguments=None): | ||
def __init__(self, description=None, arguments=None, null=True): | ||
self.arguments = arguments or {} | ||
self.description = description | ||
self.nullable = null | ||
|
||
# Increase the creation counter, and save our local copy. | ||
self.creation_counter = Field.creation_counter | ||
|
@@ -64,13 +65,29 @@ def __init__(self, description=None, arguments=None): | |
self._bound = False | ||
self.errors = [] | ||
|
||
@property | ||
def value(self): | ||
value = self.get_value() | ||
if value is None: | ||
return self.return_null() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd prefer to see this all handled in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to keep using |
||
return value | ||
|
||
def return_null(self): | ||
if self.nullable: | ||
return None | ||
raise GraphQLError('Cannot be null') | ||
|
||
def get_value(self): | ||
raw_value = self.get_raw_value() | ||
if hasattr(self.type_, 'coerce_result'): | ||
try: | ||
return self.type_.coerce_result(raw_value) | ||
except ValueError: | ||
return None | ||
raise GraphQLError('Cannot coerce {} ({}) to {}'.format( | ||
type(raw_value).__name__, | ||
raw_value, | ||
self.type_.object_name | ||
)) | ||
return raw_value | ||
|
||
def get_raw_value(self): | ||
|
@@ -106,8 +123,12 @@ def get_resolver_args(self): | |
input_value = self.obj.variables.get(variable_name, default_value) | ||
try: | ||
arg_value = value.coerce_input(input_value) | ||
except TypeError: | ||
error = 'Argument {} expected a {} but got a {}'.format(key, type(value), type(input_value)) | ||
except ValueError: | ||
error = 'Query error: Argument {} expected a {} but got a {}'.format( | ||
key, | ||
type(value), | ||
type(input_value) | ||
) | ||
raise GraphQLError(error) | ||
resolver_args[key] = arg_value | ||
else: | ||
|
@@ -210,6 +231,13 @@ class Enum(Scalar): | |
values = () | ||
|
||
|
||
class NonNull(object): | ||
kind = NON_NULL | ||
|
||
def __init__(self, type_): | ||
self.type_ = type_ | ||
|
||
|
||
class List(object): | ||
kind = LIST | ||
|
||
|
@@ -311,7 +339,7 @@ def serialize(self): | |
data = {} | ||
for name, field in self.fields.items(): | ||
try: | ||
value = field.get_value() | ||
value = field.value | ||
self.errors.extend(field.errors) | ||
except Exception as e: | ||
value = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it might be easier for folks to get used to the API if the attribute were also called
null