Use new syntax for fields and arguments

This commit is contained in:
Syrus Akbary 2015-11-11 22:15:28 -08:00
parent a2ab008ead
commit bf168e7b12
17 changed files with 78 additions and 80 deletions

View File

@ -28,9 +28,9 @@ Here is one example for get you started:
```python
class Query(graphene.ObjectType):
hello = graphene.StringField(description='A typical hello world')
ping = graphene.StringField(description='Ping someone',
to=graphene.Argument(graphene.String))
hello = graphene.String(description='A typical hello world')
ping = graphene.String(description='Ping someone',
to=graphene.String())
def resolve_hello(self, args, info):
return 'World'

View File

@ -35,9 +35,9 @@ Here is one example for get you started:
.. code:: python
class Query(graphene.ObjectType):
hello = graphene.StringField(description='A typical hello world')
ping = graphene.StringField(description='Ping someone',
to=graphene.Argument(graphene.String))
hello = graphene.String(description='A typical hello world')
ping = graphene.String(description='Ping someone',
to=graphene.String())
def resolve_hello(self, args, info):
return 'World'

View File

@ -12,10 +12,10 @@ Episode = graphene.Enum('Episode', dict(
class Character(graphene.Interface):
id = graphene.IDField()
name = graphene.StringField()
friends = graphene.ListField('Character')
appears_in = graphene.ListField(Episode)
id = graphene.ID()
name = graphene.String()
friends = graphene.List('Character')
appears_in = graphene.List(Episode)
def resolve_friends(self, args, *_):
# The character friends is a list of strings
@ -23,11 +23,11 @@ class Character(graphene.Interface):
class Human(Character):
home_planet = graphene.StringField()
home_planet = graphene.String()
class Droid(Character):
primary_function = graphene.StringField()
primary_function = graphene.String()
class Query(graphene.ObjectType):
@ -35,10 +35,10 @@ class Query(graphene.ObjectType):
episode=graphene.Argument(Episode)
)
human = graphene.Field(Human,
id=graphene.Argument(graphene.String)
id=graphene.String()
)
droid = graphene.Field(Droid,
id=graphene.Argument(graphene.String)
id=graphene.String()
)
@resolve_only_args

View File

@ -40,8 +40,8 @@ class Faction(DjangoNode):
class IntroduceShip(relay.ClientIDMutation):
class Input:
ship_name = graphene.StringField(required=True)
faction_id = graphene.StringField(required=True)
ship_name = graphene.String(required=True)
faction_id = graphene.String(required=True)
ship = graphene.Field(Ship)
faction = graphene.Field(Faction)

View File

@ -8,7 +8,7 @@ schema = graphene.Schema(name='Starwars Relay Schema')
class Ship(relay.Node):
'''A ship in the Star Wars saga'''
name = graphene.StringField(description='The name of the ship.')
name = graphene.String(description='The name of the ship.')
@classmethod
def get_node(cls, id):
@ -17,7 +17,7 @@ class Ship(relay.Node):
class Faction(relay.Node):
'''A faction in the Star Wars saga'''
name = graphene.StringField(description='The name of the faction.')
name = graphene.String(description='The name of the faction.')
ships = relay.ConnectionField(
Ship, description='The ships used by the faction.')
@ -34,8 +34,8 @@ class Faction(relay.Node):
class IntroduceShip(relay.ClientIDMutation):
class Input:
ship_name = graphene.StringField(required=True)
faction_id = graphene.StringField(required=True)
ship_name = graphene.String(required=True)
faction_id = graphene.String(required=True)
ship = graphene.Field(Ship)
faction = graphene.Field(Faction)

View File

@ -10,6 +10,7 @@ from graphene.core.schema import (
from graphene.core.types import (
ObjectType,
InputObjectType,
Interface,
Mutation,
BaseType,
@ -55,6 +56,7 @@ __all__ = [
'BaseType',
'LazyType',
'ObjectType',
'InputObjectType',
'Interface',
'Mutation',
'Field',

View File

@ -2,7 +2,7 @@
from graphene import Schema
from graphene.contrib.django.types import DjangoInterface, DjangoNode
from graphene.core.fields import Field, IntField
from graphene.core.fields import Field
from graphene.core.types.scalars import Int
from graphene.relay.fields import GlobalIDField
from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
@ -23,7 +23,7 @@ class Character(DjangoInterface):
class Human(DjangoNode):
'''Human description'''
pub_date = IntField()
pub_date = Int()
def get_node(self, id):
pass

View File

@ -18,7 +18,7 @@ class Character(DjangoNode):
class Human(DjangoNode):
raises = graphene.StringField()
raises = graphene.String()
class Meta:
model = Article

View File

@ -5,15 +5,15 @@ my_id = 0
class Query(graphene.ObjectType):
base = graphene.StringField()
base = graphene.String()
class ChangeNumber(graphene.Mutation):
'''Result mutation'''
class Input:
to = graphene.IntField()
to = graphene.Int()
result = graphene.StringField()
result = graphene.String()
@classmethod
def mutate(cls, instance, args, info):

View File

@ -1,6 +1,6 @@
from py.test import raises
from graphene.core.fields import StringField
from graphene.core.fields import Field
from graphene.core.options import Options
@ -20,7 +20,7 @@ def test_field_added_in_meta():
pass
opt.contribute_to_class(ObjectType, '_meta')
f = StringField()
f = Field(None)
f.attname = 'string_field'
opt.add_field(f)
assert f in opt.fields

View File

@ -1,22 +1,22 @@
from graphene.core.fields import Field, ListField, StringField
from graphene.core.fields import Field
from graphene.core.schema import Schema
from graphene.core.types import Interface, ObjectType
from graphene.core.types import Interface, List, ObjectType, String
from graphql.core import graphql
from graphql.core.type import GraphQLSchema
class Character(Interface):
name = StringField()
name = String()
class Pet(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
class Human(Character):
friends = ListField(Character)
friends = List(Character)
pet = Field(Pet)
def resolve_name(self, *args):
@ -27,8 +27,6 @@ class Human(Character):
def resolve_pet(self, *args):
return Pet(object())
# def resolve_friends(self, *args, **kwargs):
# return 'HEY YOU!'
schema = Schema()

View File

@ -1,7 +1,7 @@
from py.test import raises
from graphene import Interface, ObjectType, Schema
from graphene.core.fields import Field, ListField, StringField
from graphene import Interface, List, ObjectType, Schema, String
from graphene.core.fields import Field
from graphene.core.types.base import LazyType
from graphql.core import graphql
from tests.utils import assert_equal_lists
@ -10,15 +10,15 @@ schema = Schema(name='My own schema')
class Character(Interface):
name = StringField()
name = String()
class Pet(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
class Human(Character):
friends = ListField(Character)
friends = List(Character)
pet = Field(Pet)
def resolve_name(self, *args):
@ -109,7 +109,7 @@ def test_schema_register():
@schema.register
class MyType(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
schema.query = MyType
@ -121,7 +121,7 @@ def test_schema_register_no_query_type():
@schema.register
class MyType(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
with raises(Exception) as excinfo:
schema.get_type('MyType')
@ -132,7 +132,7 @@ def test_schema_introspect():
schema = Schema(name='My own schema')
class MyType(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
schema.query = MyType
@ -147,7 +147,7 @@ def test_lazytype():
@schema.register
class MyType(ObjectType):
type = StringField(resolve=lambda *_: 'Dog')
type = String(resolver=lambda *_: 'Dog')
schema.query = MyType

View File

@ -1,8 +1,7 @@
from py.test import raises
from graphene.core.fields import IntField, StringField
from graphene.core.schema import Schema
from graphene.core.types import Interface
from graphene.core.types import Int, Interface, String
from graphql.core.execution.middlewares.utils import (resolver_has_tag,
tag_resolver)
from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
@ -10,7 +9,7 @@ from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
class Character(Interface):
'''Character description'''
name = StringField()
name = String()
class Meta:
type_name = 'core_Character'
@ -18,7 +17,7 @@ class Character(Interface):
class Human(Character):
'''Human description'''
friends = StringField()
friends = String()
class Meta:
type_name = 'core_Human'
@ -93,7 +92,7 @@ def test_object_type_container_too_many_args():
def test_field_clashes():
with raises(Exception) as excinfo:
class Droid(Character):
name = IntField()
name = Int()
assert 'clashes' in str(excinfo.value)
@ -104,7 +103,7 @@ def test_fields_inherited_should_be_different():
def test_field_mantain_resolver_tags():
class Droid(Character):
name = StringField()
name = String()
def resolve_name(self, *args):
return 'My Droid'

View File

@ -1,20 +1,21 @@
import graphene
from graphene import relay
from graphene.core.schema import Schema
from graphql.core.type import GraphQLInputObjectField
my_id = 0
class Query(graphene.ObjectType):
base = graphene.StringField()
base = graphene.String()
class ChangeNumber(relay.ClientIDMutation):
'''Result mutation'''
class Input:
to = graphene.IntField()
to = graphene.Int()
result = graphene.StringField()
result = graphene.String()
@classmethod
def mutate_and_get_payload(cls, input, info):
@ -33,16 +34,15 @@ schema = Schema(query=Query, mutation=MyResultMutation)
def test_mutation_arguments():
assert ChangeNumber.arguments
assert list(ChangeNumber.arguments) == ['input']
ChangeNumber.arguments['input']
# inner_type = _input.get_object_type(schema)
# client_mutation_id_field = inner_type._meta.fields_map[
# 'client_mutation_id']
# assert issubclass(inner_type, InputObjectType)
# assert isinstance(client_mutation_id_field, graphene.StringField)
# assert client_mutation_id_field.object_type == inner_type
# assert isinstance(client_mutation_id_field.internal_field(
# schema), GraphQLInputObjectField)
assert 'input' in ChangeNumber.arguments
inner_type = ChangeNumber.input_type
client_mutation_id_field = inner_type._meta.fields_map[
'client_mutation_id']
assert issubclass(inner_type, graphene.InputObjectType)
assert isinstance(client_mutation_id_field.type, graphene.NonNull)
assert isinstance(client_mutation_id_field.type.of_type, graphene.String)
assert client_mutation_id_field.object_type == inner_type
assert isinstance(schema.T(client_mutation_id_field), GraphQLInputObjectField)
def test_execute_mutations():

View File

@ -6,12 +6,12 @@ schema = graphene.Schema()
class MyConnection(relay.Connection):
my_custom_field = graphene.StringField(
resolve=lambda instance, *_: 'Custom')
my_custom_field = graphene.String(
resolver=lambda instance, *_: 'Custom')
class MyNode(relay.Node):
name = graphene.StringField()
name = graphene.String()
@classmethod
def get_node(cls, id):

View File

@ -7,7 +7,7 @@ schema = graphene.Schema()
class OtherNode(relay.Node):
name = graphene.StringField()
name = graphene.String()
@classmethod
def get_node(cls, id):
@ -17,7 +17,7 @@ class OtherNode(relay.Node):
def test_field_no_contributed_raises_error():
with raises(Exception) as excinfo:
class Part(relay.Node):
x = graphene.StringField()
x = graphene.String()
assert 'get_node' in str(excinfo.value)

View File

@ -1,6 +1,5 @@
from graphene.core.fields import BooleanField, Field, ListField, StringField
from graphene.core.types import (InputObjectType, Interface, Mutation,
ObjectType)
from graphene.core.types import (Boolean, Field, InputObjectType, Interface,
List, Mutation, ObjectType, String)
from graphene.core.types.argument import ArgumentsGroup
from graphene.core.types.base import LazyType
from graphene.core.types.definitions import NonNull
@ -10,15 +9,15 @@ from graphql_relay.node.node import to_global_id
class PageInfo(ObjectType):
has_next_page = BooleanField(
has_next_page = Boolean(
required=True,
description='When paginating forwards, are there more items?')
has_previous_page = BooleanField(
has_previous_page = Boolean(
required=True,
description='When paginating backwards, are there more items?')
start_cursor = StringField(
start_cursor = String(
description='When paginating backwards, the cursor to continue.')
end_cursor = StringField(
end_cursor = String(
description='When paginating forwards, the cursor to continue.')
@ -29,7 +28,7 @@ class Edge(ObjectType):
node = Field(LazyType(lambda object_type: object_type.node_type),
description='The item at the end of the edge')
cursor = StringField(
cursor = String(
required=True, description='A cursor for use in pagination')
@classmethod
@ -50,7 +49,7 @@ class Connection(ObjectType):
page_info = Field(PageInfo, required=True,
description='The Information to aid in pagination')
edges = ListField(LazyType(lambda object_type: object_type.edge_type),
edges = List(LazyType(lambda object_type: object_type.edge_type),
description='Information to aid in pagination.')
_connection_data = None
@ -104,11 +103,11 @@ class Node(BaseNode, Interface):
class MutationInputType(InputObjectType):
client_mutation_id = StringField(required=True)
client_mutation_id = String(required=True)
class ClientIDMutation(Mutation):
client_mutation_id = StringField(required=True)
client_mutation_id = String(required=True)
@classmethod
def _prepare_class(cls):