Skip to content

Commit 2b0232f

Browse files
committed
Added definitions
1 parent a59dc21 commit 2b0232f

File tree

5 files changed

+118
-45
lines changed

5 files changed

+118
-45
lines changed

graphql/type/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
GraphQLScalarType,
44
GraphQLObjectType,
55
GraphQLField,
6+
GraphQLFieldDefinition,
67
GraphQLArgument,
78
GraphQLInterfaceType,
89
GraphQLUnionType,
910
GraphQLEnumType,
1011
GraphQLEnumValue,
1112
GraphQLInputObjectType,
1213
GraphQLInputObjectField,
14+
GraphQLInputFieldDefinition,
1315
GraphQLList,
1416
GraphQLNonNull,
1517
get_named_type,

graphql/type/definition.py

Lines changed: 98 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,22 @@ def define_field_map(type, field_map):
220220
'{}.{} must be an instance of GraphQLField.'.format(type, field_name)
221221
)
222222

223-
field = copy.copy(field)
224-
field.name = field_name
223+
field_args = field.args
224+
225+
field = GraphQLFieldDefinition(
226+
type=field.type,
227+
name=field_name,
228+
args=[],
229+
resolver=field.resolver,
230+
deprecation_reason=field.deprecation_reason,
231+
description=field.description,
232+
)
225233

226234
assert is_output_type(field.type), (
227235
'{}.{} field type must be Output Type but got: {}.'.format(type, field_name, field.type)
228236
)
229237

230-
if not field.args:
231-
field.args = []
232-
233-
else:
234-
field_args = field.args
238+
if field_args:
235239
assert isinstance(field_args, collections.Mapping), (
236240
'{}.{} args must be a mapping (dict / OrderedDict) with argument names as keys.'.format(type,
237241
field_name)
@@ -242,16 +246,20 @@ def define_field_map(type, field_map):
242246

243247
for arg_name, arg in field_args.items():
244248
assert_valid_name(arg_name)
245-
assert isinstance(arg, GraphQLArgument), (
249+
assert isinstance(arg, (GraphQLArgument, GraphQLArgumentDefinition)), (
246250
'{}.{}({}:) argument must be an instance of GraphQLArgument.'.format(type, field_name, arg_name)
247251
)
248252
assert is_input_type(arg.type), (
249253
'{}.{}({}:) argument type must be Input Type but got: {}.'.format(type, field_name, arg_name,
250254
arg.type)
251255
)
252256

253-
arg = copy.copy(arg)
254-
arg.name = arg_name
257+
arg = GraphQLArgumentDefinition(
258+
type=arg.type,
259+
name=arg_name,
260+
default_value=arg.default_value,
261+
description=arg.description,
262+
)
255263
args.append(arg)
256264

257265
field.args = args
@@ -289,10 +297,9 @@ def define_interfaces(type, interfaces):
289297

290298

291299
class GraphQLField(object):
292-
__slots__ = 'name', 'type', 'args', 'resolver', 'deprecation_reason', 'description'
300+
__slots__ = 'type', 'args', 'resolver', 'deprecation_reason', 'description'
293301

294302
def __init__(self, type, args=None, resolver=None, deprecation_reason=None, description=None):
295-
self.name = None
296303
self.type = type
297304
self.args = args
298305
self.resolver = resolver
@@ -303,7 +310,6 @@ def __eq__(self, other):
303310
return (
304311
self is other or (
305312
isinstance(other, GraphQLField) and
306-
self.name == other.name and
307313
self.type == other.type and
308314
self.args == other.args and
309315
self.resolver == other.resolver and
@@ -316,11 +322,38 @@ def __hash__(self):
316322
return id(self)
317323

318324

325+
class GraphQLFieldDefinition(object):
326+
__slots__ = 'name', 'type', 'args', 'resolver', 'deprecation_reason', 'description'
327+
328+
def __init__(self, type, name, args=None, resolver=None, deprecation_reason=None, description=None):
329+
self.type = type
330+
self.name = name
331+
self.args = args
332+
self.resolver = resolver
333+
self.deprecation_reason = deprecation_reason
334+
self.description = description
335+
336+
def __eq__(self, other):
337+
return (
338+
self is other or (
339+
isinstance(other, GraphQLFieldDefinition) and
340+
self.type == other.type and
341+
self.name == other.name and
342+
self.args == other.args and
343+
self.resolver == other.resolver and
344+
self.deprecation_reason == other.deprecation_reason and
345+
self.description == other.description
346+
)
347+
)
348+
349+
def __hash__(self):
350+
return id(self)
351+
352+
319353
class GraphQLArgument(object):
320-
__slots__ = 'name', 'type', 'default_value', 'description'
354+
__slots__ = 'type', 'default_value', 'description'
321355

322356
def __init__(self, type, default_value=None, description=None):
323-
self.name = None
324357
self.type = type
325358
self.default_value = default_value
326359
self.description = description
@@ -329,6 +362,29 @@ def __eq__(self, other):
329362
return (
330363
self is other or (
331364
isinstance(other, GraphQLArgument) and
365+
self.type == other.type and
366+
self.default_value == other.default_value and
367+
self.description == other.description
368+
)
369+
)
370+
371+
def __hash__(self):
372+
return id(self)
373+
374+
375+
class GraphQLArgumentDefinition(object):
376+
__slots__ = 'name', 'type', 'default_value', 'description'
377+
378+
def __init__(self, type, name, default_value=None, description=None):
379+
self.type = type
380+
self.name = name
381+
self.default_value = default_value
382+
self.description = description
383+
384+
def __eq__(self, other):
385+
return (
386+
self is other or (
387+
isinstance(other, GraphQLArgumentDefinition) and
332388
self.name == other.name and
333389
self.type == other.type and
334390
self.default_value == other.default_value and
@@ -610,24 +666,26 @@ def _define_field_map(self):
610666
assert isinstance(field, GraphQLInputObjectField), (
611667
'{}.{} must be an instance of GraphQLInputObjectField.'.format(self, field_name)
612668
)
613-
614-
field = copy.copy(field)
615-
field.name = field_name
616-
617669
assert is_input_type(field.type), (
618670
'{}.{} field type must be Input Type but got: {}.'.format(self, field_name, field.type)
619671
)
620672

673+
field = GraphQLInputFieldDefinition(
674+
type=field.type,
675+
name=field_name,
676+
default_value=field.default_value,
677+
description=field.description,
678+
)
679+
621680
field_map[field_name] = field
622681

623682
return field_map
624683

625684

626685
class GraphQLInputObjectField(object):
627-
__slots__ = 'name', 'type', 'default_value', 'description'
686+
__slots__ = 'type', 'default_value', 'description'
628687

629688
def __init__(self, type, default_value=None, description=None):
630-
self.name = None
631689
self.type = type
632690
self.default_value = default_value
633691
self.description = description
@@ -636,6 +694,25 @@ def __eq__(self, other):
636694
return (
637695
self is other or (
638696
isinstance(other, GraphQLInputObjectField) and
697+
self.type == other.type and
698+
self.description == other.description
699+
)
700+
)
701+
702+
703+
class GraphQLInputFieldDefinition(object):
704+
__slots__ = 'name', 'type', 'default_value', 'description'
705+
706+
def __init__(self, type, name, default_value=None, description=None):
707+
self.name = name
708+
self.type = type
709+
self.default_value = default_value
710+
self.description = description
711+
712+
def __eq__(self, other):
713+
return (
714+
self is other or (
715+
isinstance(other, GraphQLInputFieldDefinition) and
639716
self.name == other.name and
640717
self.type == other.type and
641718
self.description == other.description

graphql/type/directives.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import collections
22

33
from ..utils.assert_valid_name import assert_valid_name
4-
from .definition import GraphQLArgument, GraphQLNonNull, is_input_type
4+
from .definition import GraphQLArgument, GraphQLArgumentDefinition, GraphQLNonNull, is_input_type
55
from .scalars import GraphQLBoolean
66

77

@@ -52,20 +52,14 @@ def __init__(self, name, description=None, args=None, locations=None):
5252
name,
5353
arg_name,
5454
_arg.type)
55-
self.args.append(arg(
56-
arg_name,
57-
description=_arg.description,
55+
self.args.append(GraphQLArgumentDefinition(
5856
type=_arg.type,
59-
default_value=_arg.default_value
57+
name=arg_name,
58+
description=_arg.description,
59+
default_value=_arg.default_value,
6060
))
6161

6262

63-
def arg(name, *args, **kwargs):
64-
a = GraphQLArgument(*args, **kwargs)
65-
a.name = name
66-
return a
67-
68-
6963
GraphQLIncludeDirective = GraphQLDirective(
7064
name='include',
7165
args={

graphql/type/introspection.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from ..language.printer import print_ast
44
from ..utils.ast_from_value import ast_from_value
5-
from .definition import (GraphQLArgument, GraphQLEnumType, GraphQLEnumValue,
5+
from .definition import (GraphQLArgument, GraphQLArgumentDefinition, GraphQLEnumType, GraphQLEnumValue,
66
GraphQLField, GraphQLInputObjectType,
77
GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
88
GraphQLObjectType, GraphQLScalarType,
9-
GraphQLUnionType)
9+
GraphQLUnionType, GraphQLFieldDefinition)
1010
from .directives import DirectiveLocation
1111
from .scalars import GraphQLBoolean, GraphQLString
1212

@@ -347,29 +347,28 @@ def input_fields(type, *_):
347347

348348
IntrospectionSchema = __Schema
349349

350-
SchemaMetaFieldDef = GraphQLField(
350+
SchemaMetaFieldDef = GraphQLFieldDefinition(
351+
name='__schema',
351352
type=GraphQLNonNull(__Schema),
352353
description='Access the current type schema of this server.',
353354
resolver=lambda source, args, context, info: info.schema,
354355
args=[]
355356
)
356-
SchemaMetaFieldDef.name = '__schema'
357357

358-
TypeMetaFieldDef_args_name = GraphQLArgument(GraphQLNonNull(GraphQLString))
359-
TypeMetaFieldDef_args_name.name = 'name'
360-
TypeMetaFieldDef = GraphQLField(
358+
TypeMetaFieldDef_args_name = GraphQLArgumentDefinition(GraphQLNonNull(GraphQLString), name='name')
359+
TypeMetaFieldDef = GraphQLFieldDefinition(
361360
type=__Type,
361+
name='__type',
362362
description='Request the type information of a single type.',
363363
args=[TypeMetaFieldDef_args_name],
364364
resolver=lambda source, args, context, info: info.schema.get_type(args['name'])
365365
)
366-
TypeMetaFieldDef.name = '__type'
367366
del TypeMetaFieldDef_args_name
368367

369-
TypeNameMetaFieldDef = GraphQLField(
368+
TypeNameMetaFieldDef = GraphQLFieldDefinition(
370369
type=GraphQLNonNull(GraphQLString),
370+
name='__typename',
371371
description='The name of the current Object type at runtime.',
372372
resolver=lambda source, args, context, info: info.parent_type.name,
373373
args=[]
374374
)
375-
TypeNameMetaFieldDef.name = '__typename'

graphql/utils/build_client_schema.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
from .value_from_ast import value_from_ast
1616

1717

18-
def _false(*_): return False
18+
def _false(*_):
19+
return False
1920

2021

21-
def _none(*_): return None
22+
def _none(*_):
23+
return None
2224

2325

2426
def no_execution(*args):
@@ -204,7 +206,6 @@ def build_input_value(input_value_introspection, argument_type):
204206
type=get_input_type(input_value_introspection['type']),
205207
default_value=build_default_value(input_value_introspection)
206208
)
207-
input_value.name = input_value_introspection['name']
208209
return input_value
209210

210211
def build_directive(directive_introspection):

0 commit comments

Comments
 (0)