Skip to content

Commit 7d6aa95

Browse files
committed
Improved Enum type docs and Include test for extending a schema that uses Enums
Related GraphQL-js commit: graphql/graphql-js@37924d2
1 parent c957874 commit 7d6aa95

File tree

2 files changed

+116
-9
lines changed

2 files changed

+116
-9
lines changed

graphql/type/definition.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,14 @@ class GraphQLEnumType(GraphQLType):
495495
496496
Example:
497497
498-
RGBType = GraphQLEnumType('RGB', {
499-
'RED': 0,
500-
'GREEN': 1,
501-
'BLUE': 2,
502-
})
498+
RGBType = GraphQLEnumType(
499+
name='RGB',
500+
values=OrderedDict([
501+
('RED', GraphQLEnumValue(0)),
502+
('GREEN', GraphQLEnumValue(1)),
503+
('BLUE', GraphQLEnumValue(2))
504+
])
505+
)
503506
504507
Note: If a value is not provided in a definition, the name of the enum value will be used as it's internal value.
505508
"""

graphql/utils/tests/test_extend_schema.py

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from graphql import parse
66
from graphql.execution import execute
7-
from graphql.type import (GraphQLArgument, GraphQLField, GraphQLID,
8-
GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
9-
GraphQLObjectType, GraphQLSchema, GraphQLString,
10-
GraphQLUnionType)
7+
from graphql.type import (GraphQLArgument, GraphQLEnumType, GraphQLEnumValue,
8+
GraphQLField, GraphQLID, GraphQLInterfaceType,
9+
GraphQLList, GraphQLNonNull, GraphQLObjectType,
10+
GraphQLSchema, GraphQLString, GraphQLUnionType)
1111
from graphql.utils.extend_schema import extend_schema
1212
from graphql.utils.schema_printer import print_schema
1313

@@ -55,12 +55,21 @@
5555
types=[FooType, BizType],
5656
)
5757

58+
SomeEnumType = GraphQLEnumType(
59+
name='SomeEnum',
60+
values=OrderedDict([
61+
('ONE', GraphQLEnumValue(1)),
62+
('TWO', GraphQLEnumValue(2)),
63+
])
64+
)
65+
5866
test_schema = GraphQLSchema(
5967
query=GraphQLObjectType(
6068
name='Query',
6169
fields=lambda: OrderedDict([
6270
('foo', GraphQLField(FooType)),
6371
('someUnion', GraphQLField(SomeUnionType)),
72+
('someEnum', GraphQLField(SomeEnumType)),
6473
('someInterface', GraphQLField(
6574
SomeInterfaceType,
6675
args={
@@ -143,9 +152,15 @@ def test_extends_objects_by_adding_new_fields():
143152
type Query {
144153
foo: Foo
145154
someUnion: SomeUnion
155+
someEnum: SomeEnum
146156
someInterface(id: ID!): SomeInterface
147157
}
148158
159+
enum SomeEnum {
160+
ONE
161+
TWO
162+
}
163+
149164
interface SomeInterface {
150165
name: String
151166
some: SomeInterface
@@ -201,9 +216,74 @@ def test_extends_objects_by_adding_new_fields_with_arguments():
201216
type Query {
202217
foo: Foo
203218
someUnion: SomeUnion
219+
someEnum: SomeEnum
204220
someInterface(id: ID!): SomeInterface
205221
}
206222
223+
enum SomeEnum {
224+
ONE
225+
TWO
226+
}
227+
228+
interface SomeInterface {
229+
name: String
230+
some: SomeInterface
231+
}
232+
233+
union SomeUnion = Foo | Biz
234+
'''
235+
236+
237+
def test_extends_objects_by_adding_new_fields_with_existing_types():
238+
ast = parse('''
239+
extend type Foo {
240+
newField(arg1: SomeEnum!): SomeEnum
241+
}
242+
243+
input NewInputObj {
244+
field1: Int
245+
field2: [Float]
246+
field3: String!
247+
}
248+
''')
249+
original_print = print_schema(test_schema)
250+
extended_schema = extend_schema(test_schema, ast)
251+
assert extended_schema != test_schema
252+
assert print_schema(test_schema) == original_print
253+
assert print_schema(extended_schema) == \
254+
'''schema {
255+
query: Query
256+
}
257+
258+
type Bar implements SomeInterface {
259+
name: String
260+
some: SomeInterface
261+
foo: Foo
262+
}
263+
264+
type Biz {
265+
fizz: String
266+
}
267+
268+
type Foo implements SomeInterface {
269+
name: String
270+
some: SomeInterface
271+
tree: [Foo]!
272+
newField(arg1: SomeEnum!): SomeEnum
273+
}
274+
275+
type Query {
276+
foo: Foo
277+
someUnion: SomeUnion
278+
someEnum: SomeEnum
279+
someInterface(id: ID!): SomeInterface
280+
}
281+
282+
enum SomeEnum {
283+
ONE
284+
TWO
285+
}
286+
207287
interface SomeInterface {
208288
name: String
209289
some: SomeInterface
@@ -250,9 +330,15 @@ def test_extends_objects_by_adding_implemented_interfaces():
250330
type Query {
251331
foo: Foo
252332
someUnion: SomeUnion
333+
someEnum: SomeEnum
253334
someInterface(id: ID!): SomeInterface
254335
}
255336
337+
enum SomeEnum {
338+
ONE
339+
TWO
340+
}
341+
256342
interface SomeInterface {
257343
name: String
258344
some: SomeInterface
@@ -343,9 +429,15 @@ def test_extends_objects_by_adding_implemented_interfaces_2():
343429
type Query {
344430
foo: Foo
345431
someUnion: SomeUnion
432+
someEnum: SomeEnum
346433
someInterface(id: ID!): SomeInterface
347434
}
348435
436+
enum SomeEnum {
437+
ONE
438+
TWO
439+
}
440+
349441
interface SomeInterface {
350442
name: String
351443
some: SomeInterface
@@ -397,9 +489,15 @@ def test_extends_objects_by_adding_implemented_new_interfaces():
397489
type Query {
398490
foo: Foo
399491
someUnion: SomeUnion
492+
someEnum: SomeEnum
400493
someInterface(id: ID!): SomeInterface
401494
}
402495
496+
enum SomeEnum {
497+
ONE
498+
TWO
499+
}
500+
403501
interface SomeInterface {
404502
name: String
405503
some: SomeInterface
@@ -464,9 +562,15 @@ def test_extends_objects_multiple_times():
464562
type Query {
465563
foo: Foo
466564
someUnion: SomeUnion
565+
someEnum: SomeEnum
467566
someInterface(id: ID!): SomeInterface
468567
}
469568
569+
enum SomeEnum {
570+
ONE
571+
TWO
572+
}
573+
470574
interface SomeInterface {
471575
name: String
472576
some: SomeInterface

0 commit comments

Comments
 (0)