def test_is_type_of_used_to_resolve_runtime_type_for_interface(): PetType = GraphQLInterfaceType( name='Pet', fields={'name': GraphQLField(GraphQLString)}) DogType = GraphQLObjectType(name='Dog', interfaces=[PetType], is_type_of=is_type_of(Dog), fields={ 'name': GraphQLField(GraphQLString), 'woofs': GraphQLField(GraphQLBoolean) }) CatType = GraphQLObjectType(name='Cat', interfaces=[PetType], is_type_of=is_type_of(Cat), fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean) }) schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields={ 'pets': GraphQLField( GraphQLList(PetType), resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)]) })) query = ''' { pets { name ... on Dog { woofs } ... on Cat { meows } } } ''' result = graphql(schema, query) assert not result.errors assert result.data == { 'pets': [{ 'woofs': True, 'name': 'Odie' }, { 'name': 'Garfield', 'meows': False }] }
def generate_schema(db): _types = {} fields = {} mutations = {} for name, entity in db.entities.items(): typ = EntityConnectionType(entity, _types) fields[typ.name] = typ.make_field() for name, entity in db.entities.items(): typ = EntityType(entity, _types) mutations.update(typ.make_mutations()) if getattr(db, 'mutations', None): mutations.update(db.mutations) query = GraphQLObjectType(name='Query', fields=fields) mutation = GraphQLObjectType('Mutation', fields=mutations) return GraphQLSchema(query=query, mutation=mutation)
def get_edge_type(self): entity_name = super(EntitySetType, self).name name = "%sEdge" % entity_name if name in self.types_dict: return self.types_dict[name] edge_type = GraphQLObjectType(name, { 'node': GraphQLField(self.node_type), 'cursor': GraphQLField(GraphQLNonNull(GraphQLString)) }) self.types_dict[name] = edge_type return edge_type
def as_graphql(self): if self.name in self.types_dict: return self.types_dict[self.name] typ = GraphQLObjectType(self.name, { 'hasNextPage': GraphQLField( GraphQLNonNull(GraphQLBoolean), ), 'hasPreviousPage': GraphQLField( GraphQLNonNull(GraphQLBoolean), ), }) self.types_dict[self.name] = typ return typ
def as_graphql(self): if self.name in self.types_dict: return self.types_dict[self.name] def get_fields(): return { name: typ.make_field() for name, typ in self.get_field_types() } object_type = GraphQLObjectType( name=self.name, fields=get_fields, ) self.types_dict[self.name] = object_type return object_type
def as_graphql(self): entity_name = super(EntityConnectionType, self).name name = "%sConnection" % entity_name if name in self.types_dict: return self.types_dict[name] connection_type = GraphQLObjectType(name, { 'pageInfo': GraphQLField(self.get_page_info_type()), 'edges': GraphQLField( GraphQLList(self.get_edge_type()), ), 'items': GraphQLField( GraphQLList(self.node_type), ) }) self.types_dict[name] = connection_type return connection_type
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)
}, field { deepField { y } } } ''', [ fields_conflict('deepField', [ ('x', 'a and b are different fields') ], L(4, 13), L(7, 13), L(5, 17), L(8, 17)) ], sort_list=False) StringBox = GraphQLObjectType('StringBox', {'scalar': GraphQLField(GraphQLString)}) IntBox = GraphQLObjectType('IntBox', {'scalar': GraphQLField(GraphQLInt)}) NonNullStringBox1 = GraphQLObjectType( 'NonNullStringBox1', {'scalar': GraphQLField(GraphQLNonNull(GraphQLString))}) NonNullStringBox2 = GraphQLObjectType( 'NonNullStringBox2', {'scalar': GraphQLField(GraphQLNonNull(GraphQLString))}) BoxUnion = GraphQLUnionType( 'BoxUnion', [StringBox, IntBox, NonNullStringBox1, NonNullStringBox2], resolve_type=lambda *_: StringBox)
def test_resolve_type_on_union_yields_useful_error(): DogType = GraphQLObjectType(name='Dog', fields={ 'name': GraphQLField(GraphQLString), 'woofs': GraphQLField(GraphQLBoolean) }) HumanType = GraphQLObjectType(name='Human', fields={ 'name': GraphQLField(GraphQLString), }) CatType = GraphQLObjectType(name='Cat', fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean) }) PetType = GraphQLUnionType( name='Pet', types=[DogType, CatType], resolve_type=make_type_resolver( lambda: [(Dog, DogType), (Cat, CatType), (Human, HumanType)])) schema = GraphQLSchema( query=GraphQLObjectType(name='Query', fields={ 'pets': GraphQLField(GraphQLList(PetType), resolver=lambda *_: [ Dog('Odie', True), Cat('Garfield', False), Human('Jon') ]) })) query = ''' { pets { ... on Dog { name woofs } ... on Cat { name meows } } } ''' result = graphql(schema, query) assert result.errors[ 0].message == 'Runtime Object type "Human" is not a possible type for "Pet".' assert result.data == { 'pets': [{ 'woofs': True, 'name': 'Odie' }, { 'name': 'Garfield', 'meows': False }, None] }
} ''', [ fields_conflict('deepField', [ ('x', 'a and b are different fields') ], L(4, 13), L(5, 17), L(7, 13), L(8, 17)) ], sort_list=False) SomeBox = GraphQLInterfaceType('SomeBox', {'unrelatedField': GraphQLField(GraphQLString)}, resolve_type=lambda *_: StringBox) StringBox = GraphQLObjectType('StringBox', { 'scalar': GraphQLField(GraphQLString), 'unrelatedField': GraphQLField(GraphQLString) }, interfaces=[SomeBox]) IntBox = GraphQLObjectType('IntBox', { 'scalar': GraphQLField(GraphQLInt), 'unrelatedField': GraphQLField(GraphQLString) }, interfaces=[SomeBox]) NonNullStringBox1 = GraphQLInterfaceType( 'NonNullStringBox1', {'scalar': GraphQLField(GraphQLNonNull(GraphQLString))}, resolve_type=lambda *_: StringBox) NonNullStringBox1Impl = GraphQLObjectType(
def node_field(id, info): assert info.schema == schema return Node.instances[id] def global_id_field(): return GraphQLField( GraphQLNonNull(GraphQLID), description='The ID of an object', resolver=lambda obj, *_: id(obj), ) UserType = GraphQLObjectType('User', { 'id': global_id_field(), 'email': GraphQLField(GraphQLString), 'name': GraphQLField(GraphQLString), }, interfaces=[node_interface]) PostType = GraphQLObjectType( 'Post', lambda: { 'id': global_id_field(), 'post_id': GraphQLField(GraphQLInt, resolver=lambda obj, *_: obj.id), 'parent': GraphQLField(GraphQLInt), 'title': GraphQLField(GraphQLString), 'text':