graphene/UPGRADE-v2.0.md

223 lines
4.4 KiB
Markdown
Raw Normal View History

2017-07-12 09:52:24 +03:00
# v2.0 Upgrade Guide
2017-07-12 06:33:03 +03:00
2017-07-24 06:12:54 +03:00
`ObjectType`, `Interface`, `InputObjectType`, `Scalar` and `Enum` implementations
have been quite simplified, without the need to define a explicit Metaclass for each subtype.
2017-07-27 06:08:11 +03:00
It also improves the field resolvers, [simplifying the code](#resolve_only_args) the
2017-07-24 06:12:54 +03:00
developer have to write to use them.
Deprecations:
* [`AbstractType`](#abstracttype-deprecated)
* [`resolve_only_args`](#resolve_only_args)
2017-07-27 05:44:17 +03:00
* [`Mutation.Input`](#mutation-input)
2017-07-24 06:12:54 +03:00
Breaking changes:
* [`Node Connections`](#node-connections)
New Features!
* [`InputObjectType`](#inputobjecttype)
* [`Meta as Class arguments`](#meta-ass-class-arguments) (_only available for Python 3_)
> 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](https://github.com/graphql-python/graphene/blob/2.0/graphene/tests/issues/test_425.py).
2017-07-12 06:33:03 +03:00
## Deprecations
2017-07-24 06:12:54 +03:00
### AbstractType deprecated
2017-07-12 06:33:03 +03:00
2017-07-24 06:12:54 +03:00
AbstractType is deprecated in graphene 2.0, you can now use normal inheritance instead.
2017-07-12 06:33:03 +03:00
2017-07-24 06:12:54 +03:00
Before:
2017-07-12 06:33:03 +03:00
2017-07-24 06:12:54 +03:00
```python
class CommonFields(AbstractType):
name = String()
2017-07-12 06:33:03 +03:00
2017-07-24 06:12:54 +03:00
class Pet(CommonFields, Interface):
pass
```
2017-07-24 06:12:54 +03:00
With 2.0:
2017-07-24 06:12:54 +03:00
```python
class CommonFields(object):
name = String()
2017-07-24 06:12:54 +03:00
class Pet(CommonFields, Interface):
pass
```
### resolve\_only\_args
2017-07-24 09:10:15 +03:00
`resolve_only_args` is now deprecated in favor of type annotations (using the polyfill `@graphene.annotate` in Python 2 in case is necessary for accessing `context` or `info`).
2017-07-24 06:12:54 +03:00
Before:
```python
class User(ObjectType):
name = String()
@resolve_only_args
def resolve_name(self):
return self.name
```
With 2.0:
```python
class User(ObjectType):
name = String()
2017-07-24 09:10:15 +03:00
def resolve_name(self):
2017-07-24 06:12:54 +03:00
return self.name
```
2017-07-27 05:44:17 +03:00
### Mutation.Input
`Mutation.Input` is now deprecated in favor using `Mutation.Arguments` (`ClientIDMutation` still uses `Input`).
Before:
```python
class User(Mutation):
class Input:
name = String()
```
With 2.0:
```python
class User(Mutation):
class Arguments:
name = String()
```
## Breaking Changes
2017-07-24 06:12:54 +03:00
### Node Connections
Node types no longer have a `Connection` by default.
In 2.0 and onwards `Connection`s should be defined explicitly.
Before:
```python
class User(ObjectType):
class Meta:
interfaces = [relay.Node]
name = String()
2017-07-24 09:16:51 +03:00
2017-07-24 06:12:54 +03:00
class Query(ObjectType):
user_connection = relay.ConnectionField(User)
```
With 2.0:
```python
class User(ObjectType):
class Meta:
interfaces = [relay.Node]
name = String()
class UserConnection(relay.Connection):
class Meta:
node = User
2017-07-24 06:12:54 +03:00
class Query(ObjectType):
user_connection = relay.ConnectionField(UserConnection)
```
## New Features
### InputObjectType
2017-07-24 06:12:54 +03:00
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:
```python
class UserInput(InputObjectType):
id = ID()
def is_user_id(id):
return id.startswith('userid_')
class Query(ObjectType):
2017-07-24 09:10:15 +03:00
user = graphene.Field(User, input=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:
```python
class UserInput(InputObjectType):
id = ID()
@property
def is_user_id(self):
2017-07-24 06:12:54 +03:00
return self.id.startswith('userid_')
class Query(ObjectType):
2017-07-24 09:10:15 +03:00
user = graphene.Field(User, input=UserInput())
2017-07-24 09:10:15 +03:00
def resolve_user(self, input):
if input.is_user_id:
return get_user(input.id)
2017-07-24 06:12:54 +03:00
```
### Meta as Class arguments
Now you can use the meta options as class arguments (**ONLY PYTHON 3**).
2017-07-24 06:12:54 +03:00
Before:
```python
class Dog(ObjectType):
class Meta:
interfaces = [Pet]
name = String()
```
With 2.0:
```python
class Dog(ObjectType, interfaces=[Pet]):
name = String()
```
2017-07-25 09:15:47 +03:00
2017-07-27 05:44:17 +03:00
### Abstract types
Now you can create abstact types super easily, without the need of subclassing the meta.
```python
class Base(ObjectType):
class Meta:
abstract = True
id = ID()
def resolve_id(self):
return "{type}_{id}".format(
type=self.__class__.__name__,
id=self.id
)
```
2017-07-25 09:15:47 +03:00
### UUID Scalar
In Graphene 2.0 there is a new dedicated scalar for UUIDs, `UUID`.