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
Example #2
0
 def get_input_fields(self):
     return {
         'parent_id': GraphQLInputObjectField(GraphQLInt),
         'title': GraphQLInputObjectField(GraphQLString),
         'text': GraphQLInputObjectField(GraphQLString),
         'tags': GraphQLInputObjectField(GraphQLList(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)
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),
    }
Example #5
0
 def get_input_fields(self):
     GetEntity = GraphQLInputObjectType(
         name=self.name + 'Get',
         fields=self._get_entity_inputs())
     return {
         'get': GraphQLInputObjectField(GetEntity),
     }
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 #7
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 #8
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 #9
0
 def internal_type(self, schema):
     return GraphQLInputObjectField(schema.T(self.type),
                                    default_value=self.default,
                                    description=self.description)
Example #10
0
    GraphQLList,
    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(
    },
)

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)
Example #12
0

def mutate_and_get_payload(data, *_):
    shipName = data.get('shipName')
    factionId = data.get('factionId')
    newShip = createShip(shipName, factionId)
    return IntroduceShipMutation(
        shipId=newShip.id,
        factionId=factionId,
    )

shipMutation = mutation_with_client_mutation_id(
    'IntroduceShip',
    input_fields={
        'shipName': GraphQLInputObjectField(
            GraphQLNonNull(GraphQLString)
        ),
        'factionId': GraphQLInputObjectField(
            GraphQLNonNull(GraphQLID)
        )
    },
    output_fields={
        'ship': GraphQLField(
            shipType,
            resolver=lambda payload, *_: getShip(payload.shipId)
        ),
        'faction': GraphQLField(
            factionType,
            resolver=lambda payload, *_: getFaction(payload.factionId)
        )
    },
Example #13
0
 def _get_entity_inputs(self):
     result = {}
     for key, typ in self.entity_type.get_field_types():
         result[key] = GraphQLInputObjectField(typ.as_input())
     return result
Example #14
0
from graphql.core.language.parser import parse
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,
Example #15
0
 def get_input_fields(self):
     ret = EntityMutation.get_input_fields(self)
     ret.update({'age': GraphQLInputObjectField(GraphQLInt)})
     return ret