def test_prints_object_field(): FooType = GraphQLObjectType(name="Foo", fields={"str": GraphQLField(GraphQLString)}) Root = GraphQLObjectType(name="Root", fields={"foo": GraphQLField(FooType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert ( output == """ schema { query: Root } type Foo { str: String } type Root { foo: Foo } """ )
def test_prints_unions(): FooType = GraphQLObjectType( name='Foo', fields={ 'bool': GraphQLField(GraphQLBoolean), }, ) BarType = GraphQLObjectType( name='Bar', fields={ 'str': GraphQLField(GraphQLString), }, ) SingleUnion = GraphQLUnionType(name='SingleUnion', resolve_type=lambda *_: None, types=[FooType]) MultipleUnion = GraphQLUnionType( name='MultipleUnion', resolve_type=lambda *_: None, types=[FooType, BarType], ) Root = GraphQLObjectType(name='Root', fields=OrderedDict([ ('single', GraphQLField(SingleUnion)), ('multiple', GraphQLField(MultipleUnion)), ])) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_prints_interface(): FooType = GraphQLInterfaceType( name='Foo', resolve_type=lambda *_: None, fields={ 'str': GraphQLField(GraphQLString) } ) BarType = GraphQLObjectType( name='Bar', fields={ 'str': GraphQLField(GraphQLString), }, interfaces=[FooType] ) Root = GraphQLObjectType( name='Root', fields={ 'bar': GraphQLField(BarType) } ) Schema = GraphQLSchema(Root, types=[BarType]) output = print_for_test(Schema) assert output == '''
def test_prints_interface(): FooType = GraphQLInterfaceType( name="Foo", resolve_type=lambda *_: None, fields={"str": GraphQLField(GraphQLString)}, ) BarType = GraphQLObjectType(name="Bar", fields={"str": GraphQLField(GraphQLString)}, interfaces=[FooType]) Root = GraphQLObjectType(name="Root", fields={"bar": GraphQLField(BarType)}) Schema = GraphQLSchema(Root, types=[BarType]) output = print_for_test(Schema) assert (output == """ schema { query: Root } type Bar implements Foo { str: String } interface Foo { str: String } type Root { bar: Bar } """)
def test_is_type_of_used_to_resolve_runtime_type_for_union(): # type: () -> None DogType = GraphQLObjectType( name="Dog", is_type_of=is_type_of(Dog), fields={ "name": GraphQLField(GraphQLString), "woofs": GraphQLField(GraphQLBoolean), }, ) CatType = GraphQLObjectType( name="Cat", is_type_of=is_type_of(Cat), fields={ "name": GraphQLField(GraphQLString), "meows": GraphQLField(GraphQLBoolean), }, ) PetType = GraphQLUnionType(name="Pet", types=[CatType, DogType]) schema = GraphQLSchema( query=GraphQLObjectType( name="Query", fields={ "pets": GraphQLField( GraphQLList(PetType), resolver=lambda *_: [Dog("Odie", True), Cat("Garfield", False)], ) }, ), types=[CatType, DogType], ) query = """ { pets { ... on Dog { name woofs } ... on Cat { name meows } } } """ result = graphql(schema, query) assert not result.errors assert result.data == { "pets": [{"woofs": True, "name": "Odie"}, {"name": "Garfield", "meows": False}] }
def test_is_type_of_used_to_resolve_runtime_type_for_union(): DogType = GraphQLObjectType( name='Dog', is_type_of=is_type_of(Dog), fields={ 'name': GraphQLField(GraphQLString), 'woofs': GraphQLField(GraphQLBoolean) } ) CatType = GraphQLObjectType( name='Cat', is_type_of=is_type_of(Cat), fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean) } ) PetType = GraphQLUnionType( name='Pet', types=[CatType, DogType] ) schema = GraphQLSchema( query=GraphQLObjectType( name='Query', fields={ 'pets': GraphQLField( GraphQLList(PetType), resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)] ) } ), types=[CatType, DogType] ) query = ''' { pets { ... on Dog { name woofs } ... on Cat { name meows } } } ''' result = graphql(schema, query) assert not result.errors assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}]}
def test_prints_object_field(): FooType = GraphQLObjectType(name='Foo', fields={'str': GraphQLField(GraphQLString)}) Root = GraphQLObjectType(name='Root', fields={'foo': GraphQLField(FooType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_prints_unions(): FooType = GraphQLObjectType( name="Foo", fields={"bool": GraphQLField(GraphQLBoolean)} ) BarType = GraphQLObjectType(name="Bar", fields={"str": GraphQLField(GraphQLString)}) SingleUnion = GraphQLUnionType( name="SingleUnion", resolve_type=lambda *_: None, types=[FooType] ) MultipleUnion = GraphQLUnionType( name="MultipleUnion", resolve_type=lambda *_: None, types=[FooType, BarType] ) Root = GraphQLObjectType( name="Root", fields=OrderedDict( [ ("single", GraphQLField(SingleUnion)), ("multiple", GraphQLField(MultipleUnion)), ] ), ) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert ( output == """ schema { query: Root } type Bar { str: String } type Foo { bool: Boolean } union MultipleUnion = Foo | Bar type Root { single: SingleUnion multiple: MultipleUnion } union SingleUnion = Foo """ )
def test_prints_multiple_interfaces(): FooType = GraphQLInterfaceType( name="Foo", resolve_type=lambda *_: None, fields={"str": GraphQLField(GraphQLString)}, ) BaazType = GraphQLInterfaceType( name="Baaz", resolve_type=lambda *_: None, fields={"int": GraphQLField(GraphQLInt)}, ) BarType = GraphQLObjectType( name="Bar", fields=OrderedDict( [("str", GraphQLField(GraphQLString)), ("int", GraphQLField(GraphQLInt))] ), interfaces=[FooType, BaazType], ) Root = GraphQLObjectType(name="Root", fields={"bar": GraphQLField(BarType)}) Schema = GraphQLSchema(Root, types=[BarType]) output = print_for_test(Schema) assert ( output == """ schema { query: Root } interface Baaz { int: Int } type Bar implements Foo & Baaz { str: String int: Int } interface Foo { str: String } type Root { bar: Bar } """ )
def test_print_enum(): RGBType = GraphQLEnumType( name="RGB", values=OrderedDict([ ("RED", GraphQLEnumValue(0)), ("GREEN", GraphQLEnumValue(1)), ("BLUE", GraphQLEnumValue(2)), ]), ) Root = GraphQLObjectType(name="Root", fields={"rgb": GraphQLField(RGBType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert (output == """ schema { query: Root } enum RGB { RED GREEN BLUE } type Root { rgb: RGB } """)
def test_prints_input_type(): InputType = GraphQLInputObjectType( name="InputType", fields={"int": GraphQLInputObjectField(GraphQLInt)}) Root = GraphQLObjectType( name="Root", fields={ "str": GraphQLField(GraphQLString, args={"argOne": GraphQLArgument(InputType)}) }, ) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert (output == """ schema { query: Root } input InputType { int: Int } type Root { str(argOne: InputType): String } """)
def test_prints_string_field_with_multiple_args_last_is_default(): output = print_single_field_schema( GraphQLField( type=GraphQLString, args=OrderedDict( [ ("argOne", GraphQLArgument(GraphQLInt)), ("argTwo", GraphQLArgument(GraphQLString)), ("argThree", GraphQLArgument(GraphQLBoolean, default_value=False)), ] ), ) ) assert ( output == """ schema { query: Root } type Root { singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String } """ )
def test_prints_string_field_with_multiple_args(): output = print_single_field_schema( GraphQLField( type=GraphQLString, args=OrderedDict( [ ("argOne", GraphQLArgument(GraphQLInt)), ("argTwo", GraphQLArgument(GraphQLString)), ] ), ) ) assert ( output == """ schema { query: Root } type Root { singleField(argOne: Int, argTwo: String): String } """ )
def test_prints_introspection_schema(): Root = GraphQLObjectType(name='Root', fields={'onlyField': GraphQLField(GraphQLString)}) Schema = GraphQLSchema(Root) output = '\n' + print_introspection_schema(Schema) assert output == '''
def test_prints_string_field_with_multiple_args(): output = print_single_field_schema( GraphQLField(type=GraphQLString, args=OrderedDict([('argOne', GraphQLArgument(GraphQLInt)), ('argTwo', GraphQLArgument(GraphQLString))]))) assert output == '''
def test_prints_custom_scalar(): OddType = GraphQLScalarType(name='Odd', serialize=lambda v: v if v % 2 == 1 else None) Root = GraphQLObjectType(name='Root', fields={'odd': GraphQLField(OddType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_prints_string_field(): output = print_single_field_schema(GraphQLField(GraphQLString)) assert (output == """ schema { query: Root } type Root { singleField: String } """)
def test_prints_string_field_with_multiple_args_last_is_default(): output = print_single_field_schema( GraphQLField(type=GraphQLString, args=OrderedDict([ ('argOne', GraphQLArgument(GraphQLInt)), ('argTwo', GraphQLArgument(GraphQLString)), ('argThree', GraphQLArgument(GraphQLBoolean, default_value=False)) ]))) assert output == '''
def test_prints_list_non_null_string_field(): output = print_single_field_schema( GraphQLField((GraphQLList(GraphQLNonNull(GraphQLString))))) assert (output == """ schema { query: Root } type Root { singleField: [String!] } """)
def test_prints_string_field_with_int_arg(): output = print_single_field_schema( GraphQLField(type=GraphQLString, args={"argOne": GraphQLArgument(GraphQLInt)})) assert (output == """ schema { query: Root } type Root { singleField(argOne: Int): String } """)
def test_prints_multiple_interfaces(): FooType = GraphQLInterfaceType(name='Foo', resolve_type=lambda *_: None, fields={'str': GraphQLField(GraphQLString)}) BaazType = GraphQLInterfaceType(name='Baaz', resolve_type=lambda *_: None, fields={'int': GraphQLField(GraphQLInt)}) BarType = GraphQLObjectType(name='Bar', fields=OrderedDict([ ('str', GraphQLField(GraphQLString)), ('int', GraphQLField(GraphQLInt)) ]), interfaces=[FooType, BaazType]) Root = GraphQLObjectType(name='Root', fields={'bar': GraphQLField(BarType)}) Schema = GraphQLSchema(Root, types=[BarType]) output = print_for_test(Schema) assert output == '''
def test_prints_string_field_with_int_arg_with_default(): output = print_single_field_schema( GraphQLField( type_=GraphQLString, args={"argOne": GraphQLArgument(GraphQLInt, default_value=2)}, )) assert (output == """ schema { query: Root } type Root { singleField(argOne: Int = 2): String } """)
def test_print_enum(): RGBType = GraphQLEnumType(name='RGB', values=OrderedDict([ ('RED', GraphQLEnumValue(0)), ('GREEN', GraphQLEnumValue(1)), ('BLUE', GraphQLEnumValue(2)) ])) Root = GraphQLObjectType(name='Root', fields={'rgb': GraphQLField(RGBType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_prints_input_type(): InputType = GraphQLInputObjectType( name='InputType', fields={'int': GraphQLInputObjectField(GraphQLInt)}) Root = GraphQLObjectType( name='Root', fields={ 'str': GraphQLField(GraphQLString, args={'argOne': GraphQLArgument(InputType)}) }) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_prints_custom_scalar(): OddType = GraphQLScalarType(name="Odd", serialize=lambda v: v if v % 2 == 1 else None) Root = GraphQLObjectType(name="Root", fields={"odd": GraphQLField(OddType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert (output == """ schema { query: Root } scalar Odd type Root { odd: Odd } """)
GraphQLObjectType, ) from graphql.type.scalars import GraphQLString from graphql.type.schema import GraphQLSchema def resolve_raises(*_): raise Exception("Throws!") # Sync schema QueryRootType = GraphQLObjectType( name="QueryRoot", fields={ "thrower": GraphQLField(GraphQLNonNull(GraphQLString), resolve=resolve_raises), "request": GraphQLField( GraphQLNonNull(GraphQLString), resolve=lambda obj, info: info.context["request"].args.get("q"), ), "context": GraphQLField( GraphQLObjectType( name="context", fields={ "session": GraphQLField(GraphQLString), "request": GraphQLField( GraphQLNonNull(GraphQLString),
from graphql.type.definition import GraphQLArgument, GraphQLField, GraphQLNonNull, GraphQLObjectType from graphql.type.scalars import GraphQLString from graphql.type.schema import GraphQLSchema def resolve_raises(*_): raise Exception("Throws!") QueryRootType = GraphQLObjectType( name='QueryRoot', fields={ 'thrower': GraphQLField(GraphQLNonNull(GraphQLString), resolver=resolve_raises), 'request': GraphQLField( GraphQLNonNull(GraphQLString), resolver=lambda obj, args, context, info: context.args.get('q')), 'test': GraphQLField(type=GraphQLString, args={'who': GraphQLArgument(GraphQLString)}, resolver=lambda obj, args, context, info: 'Hello %s' % (args.get('who') or 'World')) }) MutationRootType = GraphQLObjectType(name='MutationRoot', fields={ 'writeTest': GraphQLField( type=QueryRootType, resolver=lambda *_: QueryRootType)
) from graphql.type.scalars import GraphQLString from graphql.type.schema import GraphQLSchema def resolve_raises(*args): # pylint: disable=unused-argument raise Exception("Throws!") # Sync schema QueryRootType = GraphQLObjectType( name='QueryRoot', fields={ 'thrower': GraphQLField( GraphQLNonNull(GraphQLString), resolver=resolve_raises, ), 'request': GraphQLField( GraphQLNonNull(GraphQLString), resolver=lambda obj, info, *args: \ info.context['request'].query.get('q'), ), 'context': GraphQLField( GraphQLNonNull(GraphQLString), resolver=lambda obj, info, *args: info.context, ), 'test': GraphQLField( type=GraphQLString, args={'who': GraphQLArgument(GraphQLString)}, # resolver=lambda obj, args, context, info: \ resolver=lambda obj, info, **args: \
def test_prints_non_null_list_non_null_string_field(): output = print_single_field_schema( GraphQLField(GraphQLNonNull(GraphQLList( GraphQLNonNull(GraphQLString))))) assert output == '''
def test_prints_string_field(): output = print_single_field_schema(GraphQLField(GraphQLString)) assert output == '''