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 ```python
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.StringField(description='A typical hello world') hello = graphene.String(description='A typical hello world')
ping = graphene.StringField(description='Ping someone', ping = graphene.String(description='Ping someone',
to=graphene.Argument(graphene.String)) to=graphene.String())
def resolve_hello(self, args, info): def resolve_hello(self, args, info):
return 'World' return 'World'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
from py.test import raises from py.test import raises
from graphene.core.fields import StringField from graphene.core.fields import Field
from graphene.core.options import Options from graphene.core.options import Options
@ -20,7 +20,7 @@ def test_field_added_in_meta():
pass pass
opt.contribute_to_class(ObjectType, '_meta') opt.contribute_to_class(ObjectType, '_meta')
f = StringField() f = Field(None)
f.attname = 'string_field' f.attname = 'string_field'
opt.add_field(f) opt.add_field(f)
assert f in opt.fields 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.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 import graphql
from graphql.core.type import GraphQLSchema from graphql.core.type import GraphQLSchema
class Character(Interface): class Character(Interface):
name = StringField() name = String()
class Pet(ObjectType): class Pet(ObjectType):
type = StringField(resolve=lambda *_: 'Dog') type = String(resolver=lambda *_: 'Dog')
class Human(Character): class Human(Character):
friends = ListField(Character) friends = List(Character)
pet = Field(Pet) pet = Field(Pet)
def resolve_name(self, *args): def resolve_name(self, *args):
@ -27,8 +27,6 @@ class Human(Character):
def resolve_pet(self, *args): def resolve_pet(self, *args):
return Pet(object()) return Pet(object())
# def resolve_friends(self, *args, **kwargs):
# return 'HEY YOU!'
schema = Schema() schema = Schema()

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ schema = graphene.Schema()
class OtherNode(relay.Node): class OtherNode(relay.Node):
name = graphene.StringField() name = graphene.String()
@classmethod @classmethod
def get_node(cls, id): def get_node(cls, id):
@ -17,7 +17,7 @@ class OtherNode(relay.Node):
def test_field_no_contributed_raises_error(): def test_field_no_contributed_raises_error():
with raises(Exception) as excinfo: with raises(Exception) as excinfo:
class Part(relay.Node): class Part(relay.Node):
x = graphene.StringField() x = graphene.String()
assert 'get_node' in str(excinfo.value) 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 (Boolean, Field, InputObjectType, Interface,
from graphene.core.types import (InputObjectType, Interface, Mutation, List, Mutation, ObjectType, String)
ObjectType)
from graphene.core.types.argument import ArgumentsGroup from graphene.core.types.argument import ArgumentsGroup
from graphene.core.types.base import LazyType from graphene.core.types.base import LazyType
from graphene.core.types.definitions import NonNull from graphene.core.types.definitions import NonNull
@ -10,15 +9,15 @@ from graphql_relay.node.node import to_global_id
class PageInfo(ObjectType): class PageInfo(ObjectType):
has_next_page = BooleanField( has_next_page = Boolean(
required=True, required=True,
description='When paginating forwards, are there more items?') description='When paginating forwards, are there more items?')
has_previous_page = BooleanField( has_previous_page = Boolean(
required=True, required=True,
description='When paginating backwards, are there more items?') description='When paginating backwards, are there more items?')
start_cursor = StringField( start_cursor = String(
description='When paginating backwards, the cursor to continue.') description='When paginating backwards, the cursor to continue.')
end_cursor = StringField( end_cursor = String(
description='When paginating forwards, the cursor to continue.') 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), node = Field(LazyType(lambda object_type: object_type.node_type),
description='The item at the end of the edge') description='The item at the end of the edge')
cursor = StringField( cursor = String(
required=True, description='A cursor for use in pagination') required=True, description='A cursor for use in pagination')
@classmethod @classmethod
@ -50,8 +49,8 @@ class Connection(ObjectType):
page_info = Field(PageInfo, required=True, page_info = Field(PageInfo, required=True,
description='The Information to aid in pagination') 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.') description='Information to aid in pagination.')
_connection_data = None _connection_data = None
@ -104,11 +103,11 @@ class Node(BaseNode, Interface):
class MutationInputType(InputObjectType): class MutationInputType(InputObjectType):
client_mutation_id = StringField(required=True) client_mutation_id = String(required=True)
class ClientIDMutation(Mutation): class ClientIDMutation(Mutation):
client_mutation_id = StringField(required=True) client_mutation_id = String(required=True)
@classmethod @classmethod
def _prepare_class(cls): def _prepare_class(cls):