2015-10-31 23:46:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
from graphene.core.fields import Field, ListField, StringField
|
2015-10-16 08:18:08 +03:00
|
|
|
from graphene.core.schema import Schema
|
2015-10-31 23:46:43 +03:00
|
|
|
from graphene.core.types import Interface, ObjectType
|
|
|
|
from graphql.core import graphql
|
|
|
|
from graphql.core.type import (GraphQLInterfaceType, GraphQLObjectType,
|
|
|
|
GraphQLSchema)
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Character(Interface):
|
|
|
|
name = StringField()
|
|
|
|
|
|
|
|
|
|
|
|
class Pet(ObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
type = StringField(resolve=lambda *_: 'Dog')
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Human(Character):
|
|
|
|
friends = ListField(Character)
|
|
|
|
pet = Field(Pet)
|
|
|
|
|
|
|
|
def resolve_name(self, *args):
|
|
|
|
return 'Peter'
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def resolve_friend(self, *args):
|
|
|
|
return Human(object())
|
|
|
|
|
|
|
|
def resolve_pet(self, *args):
|
|
|
|
return Pet(object())
|
|
|
|
# def resolve_friends(self, *args, **kwargs):
|
|
|
|
# return 'HEY YOU!'
|
|
|
|
|
2015-10-16 08:18:08 +03:00
|
|
|
|
|
|
|
schema = Schema()
|
2015-10-01 11:54:52 +03:00
|
|
|
|
2015-10-30 08:20:53 +03:00
|
|
|
Human_type = schema.T(Human)
|
2015-10-01 11:54:52 +03:00
|
|
|
|
2015-10-16 08:18:08 +03:00
|
|
|
|
2015-10-14 05:10:31 +03:00
|
|
|
def test_type():
|
2015-10-31 23:46:43 +03:00
|
|
|
assert Human._meta.fields_map['name'].resolve(
|
|
|
|
Human(object()), None, None) == 'Peter'
|
2015-10-01 11:54:52 +03:00
|
|
|
|
2015-10-16 08:18:08 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def test_query():
|
|
|
|
schema = GraphQLSchema(query=Human_type)
|
|
|
|
query = '''
|
|
|
|
{
|
|
|
|
name
|
|
|
|
pet {
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'name': 'Peter',
|
|
|
|
'pet': {
|
2015-10-03 08:17:51 +03:00
|
|
|
'type': 'Dog'
|
2015-10-01 11:54:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result = graphql(schema, query, root=Human(object()))
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|