graphene/tests/relay/test_relayfields.py

80 lines
1.7 KiB
Python
Raw Normal View History

from graphql.core.type import (
GraphQLNonNull,
GraphQLID
)
2015-10-11 00:53:46 +03:00
import graphene
from graphene import relay
schema = graphene.Schema()
class MyConnection(relay.Connection):
my_custom_field = graphene.StringField(resolve=lambda instance, *_: 'Custom')
2015-10-11 00:53:46 +03:00
class MyNode(relay.Node):
name = graphene.StringField()
@classmethod
def get_node(cls, id):
2015-10-27 09:54:51 +03:00
return MyNode(name='mo')
2015-10-11 00:53:46 +03:00
class Query(graphene.ObjectType):
my_node = relay.NodeField(MyNode)
all_my_nodes = relay.ConnectionField(MyNode, connection_type=MyConnection, customArg=graphene.Argument(graphene.String))
2015-10-11 00:53:46 +03:00
def resolve_all_my_nodes(self, args, info):
custom_arg = args.get('customArg')
assert custom_arg == "1"
2015-10-27 09:54:51 +03:00
return [MyNode(name='my')]
2015-10-11 00:53:46 +03:00
schema.query = Query
def test_nodefield_query():
query = '''
query RebelsShipsQuery {
myNode(id:"TXlOb2RlOjE=") {
name
},
allMyNodes (customArg:"1") {
edges {
node {
name
}
},
myCustomField
pageInfo {
hasNextPage
}
2015-10-11 00:53:46 +03:00
}
}
'''
expected = {
'myNode': {
2015-10-27 09:54:51 +03:00
'name': 'mo'
},
'allMyNodes': {
'edges': [{
'node': {
'name': 'my'
}
}],
'myCustomField': 'Custom',
'pageInfo': {
'hasNextPage': False,
}
2015-10-11 00:53:46 +03:00
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected
def test_nodeidfield():
id_field = MyNode._meta.fields_map['id']
assert isinstance(id_field.internal_field(schema).type, GraphQLNonNull)
assert id_field.internal_field(schema).type.of_type == GraphQLID