def test_includes_nested_input_objects_in_the_map():
    NestedInputObject = GraphQLInputObjectType(
        name='NestedInputObject',
        fields={'value': GraphQLInputObjectField(GraphQLString)})

    SomeInputObject = GraphQLInputObjectType(
        name='SomeInputObject',
        fields={'nested': GraphQLInputObjectField(NestedInputObject)})

    SomeMutation = GraphQLObjectType(
        name='SomeMutation',
        fields={
            'mutateSomething':
            GraphQLField(type=BlogArticle,
                         args={'input': GraphQLArgument(SomeInputObject)})
        })
    SomeSubscription = GraphQLObjectType(
        name='SomeSubscription',
        fields={
            'subscribeToSomething':
            GraphQLField(type=BlogArticle,
                         args={'input': GraphQLArgument(SomeInputObject)})
        })

    schema = GraphQLSchema(query=BlogQuery,
                           mutation=SomeMutation,
                           subscription=SomeSubscription)

    assert schema.get_type_map()['NestedInputObject'] is NestedInputObject
def test_does_not_mutate_passed_field_definitions():
    fields = {
        'field1': GraphQLField(GraphQLString),
        'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
    }

    TestObject1 = GraphQLObjectType(name='Test1', fields=fields)
    TestObject2 = GraphQLObjectType(name='Test1', fields=fields)

    assert TestObject1.get_fields() == TestObject2.get_fields()
    assert fields == {
        'field1': GraphQLField(GraphQLString),
        'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
    }

    input_fields = {
        'field1': GraphQLInputObjectField(GraphQLString),
        'field2': GraphQLInputObjectField(GraphQLString),
    }

    TestInputObject1 = GraphQLInputObjectType(name='Test1', fields=input_fields)
    TestInputObject2 = GraphQLInputObjectType(name='Test2', fields=input_fields)

    assert TestInputObject1.get_fields() == TestInputObject2.get_fields()

    assert input_fields == {
        'field1': GraphQLInputObjectField(GraphQLString),
        'field2': GraphQLInputObjectField(GraphQLString),
    }
def test_builds_a_schema_with_an_input_object():
    AddressType = GraphQLInputObjectType(
        name='Address',
        description='An input address',
        fields=OrderedDict([
            ('street',
             GraphQLInputObjectField(
                 GraphQLNonNull(GraphQLString),
                 description='What street is this address?')),
            ('city',
             GraphQLInputObjectField(
                 GraphQLNonNull(GraphQLString),
                 description='The city the address is within?')),
            ('country',
             GraphQLInputObjectField(
                 GraphQLString,
                 description='The country (blank will assume USA).',
                 default_value='USA')),
        ]))

    schema = GraphQLSchema(query=GraphQLObjectType(
        name='HasInputObjectFields',
        fields={
            'geocode':
            GraphQLField(description='Get a geocode from an address',
                         type=GraphQLString,
                         args={
                             'address':
                             GraphQLArgument(
                                 description='The address to lookup',
                                 type=AddressType)
                         })
        }))

    _test_schema(schema)
Example #4
0
 def get_input_fields(self):
     GetEntity = GraphQLInputObjectType(
         name=self.name + 'Get',
         fields=self._get_entity_inputs())
     return {
         'get': GraphQLInputObjectField(GetEntity),
     }
Example #5
0
    def internal_type(cls, schema):
        if cls._meta.abstract:
            raise Exception("Abstract InputObjectTypes don't have a specific type.")

        return GraphQLInputObjectType(
            cls._meta.type_name,
            description=cls._meta.description,
            fields=partial(cls.fields_internal_types, schema),
        )
def test_does_not_mutate_passed_field_definitions():
    fields = {
        'field1': GraphQLField(GraphQLString),
        'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
    }

    TestObject1 = GraphQLObjectType(name='Test1', fields=fields)
    TestObject2 = GraphQLObjectType(name='Test1', fields=fields)

    assert TestObject1.get_fields() == TestObject2.get_fields()
    assert fields == {
        'field1': GraphQLField(GraphQLString),
        'field2': GraphQLField(GraphQLString, args={'id': GraphQLArgument(GraphQLString)}),
    }

    input_fields = {
        'field1': GraphQLInputObjectField(GraphQLString),
        'field2': GraphQLInputObjectField(GraphQLString),
    }

    TestInputObject1 = GraphQLInputObjectType(name='Test1', fields=input_fields)
    TestInputObject2 = GraphQLInputObjectType(name='Test2', fields=input_fields)

    assert TestInputObject1.get_fields() == TestInputObject2.get_fields()

    assert input_fields == {
        'field1': GraphQLInputObjectField(GraphQLString),
        'field2': GraphQLInputObjectField(GraphQLString),
    }
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_builds_a_schema_with_field_arguments_with_default_values():
    GeoType = GraphQLInputObjectType(
        name='Geo',
        fields=OrderedDict([
            ('lat', GraphQLInputObjectField(GraphQLFloat)),
            ('lon', GraphQLInputObjectField(GraphQLFloat)),
        ])
    )

    schema = GraphQLSchema(
        query=GraphQLObjectType(
            name='ArgFields',
            fields=OrderedDict([
                ('defaultInt', GraphQLField(
                    GraphQLString,
                    args={
                        'intArg': GraphQLArgument(
                            GraphQLInt,
                            default_value=10
                        )
                    }
                )),
                ('defaultList', GraphQLField(
                    GraphQLString,
                    args={
                        'listArg': GraphQLArgument(
                            GraphQLList(GraphQLInt),
                            default_value=[1, 2, 3]
                        )
                    }
                )),
                ('defaultObject', GraphQLField(
                    GraphQLString,
                    args={
                        'objArg': GraphQLArgument(
                            GeoType,
                            default_value={'lat': 37.485, 'lon': -122.148}
                        )
                    }
                ))
            ])
        )
    )

    _test_schema(schema)
Example #9
0
 def build(cls):
     self = cls()
     output_fields = self.get_output_fields()
     output_fields.update(
         {'clientMutationId': GraphQLField(GraphQLNonNull(GraphQLString))})
     output_type = GraphQLObjectType(self.name + 'Payload',
                                     fields=output_fields)
     input_fields = self.get_input_fields()
     input_fields.update({
         'clientMutationId':
         GraphQLInputObjectField(GraphQLNonNull(GraphQLString))
     })
     input_arg = GraphQLArgument(
         GraphQLNonNull(
             GraphQLInputObjectType(name=self.name + 'Input',
                                    fields=input_fields)))
     return GraphQLField(output_type,
                         args={
                             'input': input_arg,
                         },
                         resolver=self._resolver)
Example #10
0
def test_introspects_on_input_object():
    TestInputObject = GraphQLInputObjectType(
        'TestInputObject', {
            'a': GraphQLInputObjectField(GraphQLString, default_value='foo'),
            'b': GraphQLInputObjectField(GraphQLList(GraphQLString)),
        })
    TestType = GraphQLObjectType(
        'TestType', {
            'field':
            GraphQLField(type=GraphQLString,
                         args={'complex': GraphQLArgument(TestInputObject)},
                         resolver=lambda obj, args, info: json.dumps(
                             args.get('complex')))
        })
    schema = GraphQLSchema(TestType)
    request = '''
      {
        __schema {
          types {
            kind
            name
            inputFields {
              name
              type { ...TypeRef }
              defaultValue
            }
          }
        }
      }
      fragment TypeRef on __Type {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
            }
          }
        }
      }
    '''
    result = graphql(schema, request)
    assert not result.errors
    assert sort_lists({'kind': 'INPUT_OBJECT',
            'name': 'TestInputObject',
            'inputFields':
                [{'name': 'a',
                  'type':
                      {'kind': 'SCALAR',
                       'name': 'String',
                       'ofType': None},
                  'defaultValue': '"foo"'},
                 {'name': 'b',
                  'type':
                      {'kind': 'LIST',
                       'name': None,
                       'ofType':
                           {'kind': 'SCALAR',
                            'name': 'String',
                            'ofType': None}},
                  'defaultValue': None}]}) in \
                  sort_lists(result.data['__schema']['types'])
Example #11
0
 def internal_type(cls, schema):
     return GraphQLInputObjectType(
         cls._meta.type_name,
         description=cls._meta.description,
         fields=partial(cls.get_fields, schema),
     )
Example #12
0
    GraphQLString,
    GraphQLNonNull,
    GraphQLScalarType,
)
from graphql.core.error import GraphQLError, format_error

TestComplexScalar = GraphQLScalarType(
    name='ComplexScalar',
    serialize=lambda v: 'SerializedValue' if v == 'DeserializedValue' else None,
    parse_value=lambda v: 'DeserializedValue' if v == 'SerializedValue' else None,
    parse_literal=lambda v: 'DeserializedValue' if v.value == 'SerializedValue' else None
)

TestInputObject = GraphQLInputObjectType('TestInputObject', {
    'a': GraphQLInputObjectField(GraphQLString),
    'b': GraphQLInputObjectField(GraphQLList(GraphQLString)),
    'c': GraphQLInputObjectField(GraphQLNonNull(GraphQLString)),
    'd': GraphQLInputObjectField(TestComplexScalar)
})

stringify = lambda obj: json.dumps(obj, sort_keys=True)


def input_to_json(obj, args, info):
    input = args.get('input')
    if input:
        return stringify(input)


TestNestedInputObject = GraphQLInputObjectType(
    name='TestNestedInputObject',
    fields={
Example #13
0
        GraphQLField(BlogArticle,
                     args={
                         'id': GraphQLArgument(GraphQLString),
                     }),
        'feed':
        GraphQLField(GraphQLList(BlogArticle))
    })

BlogMutation = GraphQLObjectType('Mutation',
                                 {'writeArticle': GraphQLField(BlogArticle)})

ObjectType = GraphQLObjectType('Object', {})
InterfaceType = GraphQLInterfaceType('Interface')
UnionType = GraphQLUnionType('Union', [ObjectType])
EnumType = GraphQLEnumType('Enum', {})
InputObjectType = GraphQLInputObjectType('InputObject', {})


def test_defines_a_query_only_schema():
    BlogSchema = GraphQLSchema(BlogQuery)

    assert BlogSchema.get_query_type() == BlogQuery

    article_field = BlogQuery.get_fields()['article']
    assert article_field.type == BlogArticle
    assert article_field.type.name == 'Article'
    assert article_field.name == 'article'

    article_field_type = article_field.type
    assert isinstance(article_field_type, GraphQLObjectType)
DogOrHuman = GraphQLUnionType('DogOrHuman', [Dog, Human])

HumanOrAlien = GraphQLUnionType('HumanOrAlien', [Human, Alien])

FurColor = GraphQLEnumType('FurColor', {
    'BROWN': GraphQLEnumValue(0),
    'BLACK': GraphQLEnumValue(1),
    'TAN': GraphQLEnumValue(2),
    'SPOTTED': GraphQLEnumValue(3),
})

ComplexInput = GraphQLInputObjectType('ComplexInput', {
    'requiredField': GraphQLInputObjectField(GraphQLNonNull(GraphQLBoolean)),
    'intField': GraphQLInputObjectField(GraphQLInt),
    'stringField': GraphQLInputObjectField(GraphQLString),
    'booleanField': GraphQLInputObjectField(GraphQLBoolean),
    'stringListField': GraphQLInputObjectField(GraphQLList(GraphQLString)),
})

ComplicatedArgs = GraphQLObjectType('ComplicatedArgs', {
    'intArgField': GraphQLField(GraphQLString, {
        'intArg': GraphQLArgument(GraphQLInt)
    }),
    'nonNullIntArgField': GraphQLField(GraphQLString, {
        'nonNullIntArg': GraphQLArgument(GraphQLNonNull(GraphQLInt))
    }),
    'stringArgField': GraphQLField(GraphQLString, {
        'stringArg': GraphQLArgument(GraphQLString)
    }),
    'booleanArgField': GraphQLField(GraphQLString, {
Example #15
0
from graphql.core.type import (
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLField,
    GraphQLArgument,
    GraphQLInputObjectField,
    GraphQLInputObjectType,
    GraphQLList,
    GraphQLString,
    GraphQLNonNull,
)
from graphql.core.error import GraphQLError

TestInputObject = GraphQLInputObjectType('TestInputObject', {
    'a': GraphQLInputObjectField(GraphQLString),
    'b': GraphQLInputObjectField(GraphQLList(GraphQLString)),
    'c': GraphQLInputObjectField(GraphQLNonNull(GraphQLString)),
})

TestType = GraphQLObjectType('TestType', {
    'fieldWithObjectInput': GraphQLField(
        GraphQLString,
        args={'input': GraphQLArgument(TestInputObject)},
        resolver=lambda obj, args, *_: json.dumps(args.get('input'))),
    'fieldWithNullableStringInput': GraphQLField(
        GraphQLString,
        args={'input': GraphQLArgument(GraphQLString)},
        resolver=lambda obj, args, *_: json.dumps(args.get('input'))),
    'fieldWithNonNullableStringInput': GraphQLField(
        GraphQLString,
        args={'input': GraphQLArgument(GraphQLNonNull(GraphQLString))},
Example #16
0
        GraphQLField(GraphQLString, {
            'surname': GraphQLArgument(GraphQLBoolean),
        }),
        'pets':
        GraphQLField(GraphQLList(Pet)),
    })

FurColor = GraphQLEnumType(
    'FurColor', {
        'BROWN': GraphQLEnumValue(0),
        'BLACK': GraphQLEnumValue(1),
        'TAN': GraphQLEnumValue(2),
        'SPOTTED': GraphQLEnumValue(3),
    })

ComplexInput = GraphQLInputObjectType(
    'ComplexInput', {'stringField': GraphQLField(GraphQLString)})

ComplicatedArgs = GraphQLObjectType(
    'ComplicatedArgs', {
        'complexArgField':
        GraphQLField(GraphQLString, {
            'complexArg': GraphQLArgument(ComplexInput),
        })
    })

QueryRoot = GraphQLObjectType(
    'QueryRoot', {
        'human': GraphQLField(Human, {
            'id': GraphQLArgument(GraphQLID),
        }),
        'pet': GraphQLField(Pet),