2015-10-11 00:53:46 +03:00
|
|
|
from pytest import raises
|
2015-10-16 09:26:20 +03:00
|
|
|
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 MyType(object):
|
|
|
|
name = 'my'
|
2015-10-20 08:52:35 +03:00
|
|
|
arg = None
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
return MyNode(MyType())
|
|
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
my_node = relay.NodeField(MyNode)
|
2015-10-20 08:52:35 +03:00
|
|
|
all_my_nodes = relay.ConnectionField(MyNode, connection_type=MyConnection, customArg=graphene.Argument(graphene.String))
|
2015-10-11 00:53:46 +03:00
|
|
|
|
2015-10-20 08:52:35 +03:00
|
|
|
def resolve_all_my_nodes(self, args, info):
|
|
|
|
t = MyType()
|
|
|
|
custom_arg = args.get('customArg')
|
|
|
|
assert custom_arg == "1"
|
|
|
|
return [MyNode(t)]
|
2015-10-11 00:53:46 +03:00
|
|
|
|
|
|
|
schema.query = Query
|
|
|
|
|
|
|
|
|
|
|
|
def test_nodefield_query():
|
|
|
|
query = '''
|
|
|
|
query RebelsShipsQuery {
|
|
|
|
myNode(id:"TXlOb2RlOjE=") {
|
|
|
|
name
|
2015-10-20 08:52:35 +03:00
|
|
|
},
|
|
|
|
allMyNodes (customArg:"1") {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
},
|
|
|
|
myCustomField
|
|
|
|
pageInfo {
|
|
|
|
hasNextPage
|
|
|
|
}
|
2015-10-11 00:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'myNode': {
|
|
|
|
'name': 'my'
|
2015-10-20 08:52:35 +03:00
|
|
|
},
|
|
|
|
'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
|
2015-10-16 09:26:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|