graphene/UPGRADE-v2.0.md
2017-07-23 20:29:35 -07:00

3.7 KiB

v2.0 Upgrade Guide

ObjectType, Interface, InputObjectType, Scalar and Enum implementations have been quite simplified, without the need to define a explicit Metaclass for each subtype.

It also improves the function resolvers, simplifying the code the developer have to write to use them.

Deprecations:

Breaking changes:

New Features!

The type metaclases are now deleted as are no longer necessary, if your code was depending on this strategy for creating custom attrs, see an example on how to do it in 2.0.

Deprecations

AbstractType deprecated

AbstractType is deprecated in graphene 2.0, you can now use normal inheritance instead.

Before:

class CommonFields(AbstractType):
    name = String()

class Pet(CommonFields, Interface):
    pass

With 2.0:

class CommonFields(object):
    name = String()

class Pet(CommonFields, Interface):
    pass

resolve_only_args

resolve_only_args is now deprecated in favor of type annotations (using the polyfill @graphene.annotate in Python 2).

Before:

class User(ObjectType):
    name = String()

    @resolve_only_args
    def resolve_name(self):
        return self.name

With 2.0:

class User(ObjectType):
    name = String()

    # Decorate the resolver with @annotate in Python 2
    def resolve_name(self) -> str:
        return self.name

Breaking Changes

Node Connections

Node types no longer have a Connection by default. In 2.0 and onwards Connections should be defined explicitly.

Before:

class User(ObjectType):
    class Meta:
        interfaces = [relay.Node]
    name = String()
  
class Query(ObjectType):
    user_connection = relay.ConnectionField(User)

With 2.0:

class User(ObjectType):
    class Meta:
        interfaces = [relay.Node]
    name = String()

class UserConnection(relay.Connection):
    class Meta:
        node = User

class Query(ObjectType):
    user_connection = relay.ConnectionField(UserConnection)

New Features

InputObjectType

If you are using InputObjectType, you now can access it's fields via getattr (my_input.myattr) when resolving, instead of the classic way my_input['myattr'].

And also use custom defined properties on your input class.

Example. Before:

class UserInput(InputObjectType):
    id = ID()

def is_user_id(id):
    return id.startswith('userid_')

class Query(ObjectType):
    user = graphene.Field(User, id=UserInput())

    @resolve_only_args
    def resolve_user(self, input):
        user_id = input.get('id')
        if is_user_id(user_id):
            return get_user(user_id)

With 2.0:

class UserInput(InputObjectType):
    id = ID()

    @property
    def is_user_id(self):
        return self.id.startswith('userid_')

class Query(ObjectType):
    user = graphene.Field(User, id=UserInput())

    # Decorate the resolver with @annotate(input=UserInput) in Python 2
    def resolve_user(self, input: UserInput) -> User:
        if input.is_user_id:
            return get_user(input.id)

Meta as Class arguments

Now you can use the meta options as class arguments (ONLY PYTHON 3).

Before:

class Dog(ObjectType):
    class Meta:
        interfaces = [Pet]
    name = String()

With 2.0:

class Dog(ObjectType, interfaces=[Pet]):
    name = String()