|
| 1 | +## Migrating to Strawberry Django |
| 2 | + |
| 3 | +All the extra features provided by this lib were contributed and merged directly |
| 4 | +into the official |
| 5 | +[strawberry-graphql-django](https://github.com/strawberry-graphql/strawberry-graphql-django) |
| 6 | +lib. Since then this lib is deprecated and the official integration should be used instead. |
| 7 | + |
| 8 | +Follow these steps to migrate your existing code: |
| 9 | + |
| 10 | +### 1) Required dependencies |
| 11 | + |
| 12 | +Make sure you have `strawberry-graphql>=0.10.0` in your dependencies. After the migration |
| 13 | +is complete you can safely remove `strawberry-django-plus` from them. |
| 14 | + |
| 15 | +### 2) Replace your `gql.*` aliases |
| 16 | + |
| 17 | +The `gql.*` alias should be replaces by their correct counterpart. For example: |
| 18 | + |
| 19 | +- `gql.type` -> `strawberry.type` |
| 20 | +- `gql.field` -> `strawberry.field` |
| 21 | +- `gql.django.type` -> `strawberry.django.type` (or `strawberry_django.type`) |
| 22 | +- `gql.django.field` -> `strawberry.django.field` (or `strawberry_django.field`) |
| 23 | + |
| 24 | +### 3) Relay API adjustments |
| 25 | + |
| 26 | +The relay integration was from `v3.0` in this lib was ported "as is" to |
| 27 | +strawberry django, meaning that the `gql.*` step adjustment will also |
| 28 | +adjust the relay APIs. |
| 29 | + |
| 30 | +In case you are migrating from `v2.x`, check the `v3.0.0` migration guide below. |
| 31 | +You don't need to upgrade to `v3.0.0` first, but you can use it to help adjusting |
| 32 | +your relay code. |
| 33 | + |
| 34 | +If you were not using the relay integration, you can skip this step. |
| 35 | + |
| 36 | +### 4) Mutation API adjustments |
| 37 | + |
| 38 | +There are some differences to be aware for the mutations API: |
| 39 | + |
| 40 | +1. `strawberry_django.mutation` and `strawberry_django.input_mutation` changed the |
| 41 | + `handle_django_errors` argument default value from `True` to `False`. If you want |
| 42 | + the old behaviour for all mutations without having to modify the argument by hand, |
| 43 | + you can set the `"MUTATIONS_DEFAULT_HANDLE_ERRORS": True` in your |
| 44 | + [strawberry django settings](https://strawberry-graphql.github.io/strawberry-graphql-django/guide/settings/) |
| 45 | +2. CUD mutations are now based on strawberry django's ones. You should rename your |
| 46 | + `create_mutation`/`update_mutation`/`delete_mutation` calls to |
| 47 | + `create`/`update`/`delete` respectively. |
| 48 | +3. CUD mutations from strawberry django define the input field argument's name to |
| 49 | + `data` by default. You can change it to `input` (this lib's argument name) by passing |
| 50 | + `argument_name="input"` to the mutation. If you want that name for all mutations |
| 51 | + regardless, you can set the `"MUTATIONS_DEFAULT_ARGUMENT_NAME": "input"` in your |
| 52 | + [strawberry django settings](https://strawberry-graphql.github.io/strawberry-graphql-django/guide/settings/) |
| 53 | + |
| 54 | +### 5) Permissions refactored to use Field Extensions |
| 55 | + |
| 56 | +Permission checking used to require including a "Schema Directive Extension" in your |
| 57 | +schema's extensions. That is not required anymore since the new implementation |
| 58 | +is based on the official "Field Extensions" support from strawberry. |
| 59 | + |
| 60 | +Most extensions have the same name, except for `HasRootPerm` and `HasSourcePerm` |
| 61 | +that were renamed like this: |
| 62 | + |
| 63 | +- `HasRootPerm` -> `HasSourcePerm` |
| 64 | +- `HasObjPerm` -> `HasRetvalPerm` |
| 65 | + |
| 66 | +To migrate, all you need to do is change the directive your were previously |
| 67 | +inserting in your field with the related extension. For example, the following code: |
| 68 | + |
| 69 | +```python |
| 70 | +import strawberry |
| 71 | +from strawberry_django_plus import gql |
| 72 | +from strawberry_django_plus.permissions import IsAuthenticated, HasObjPerm |
| 73 | +from strawberry_django_plus.directives import SchemaDirectiveExtension |
| 74 | + |
| 75 | +@gql.type |
| 76 | +class Query: |
| 77 | + fruit: Fruit = gql.django.field(directives=[IsAuthenticated()]) |
| 78 | + fruit2: Fruit = gql.django.field(directives=[HasObjPerm("can_view_fruit")]) |
| 79 | + |
| 80 | +schema = strawberry.schema( |
| 81 | + query=Query, |
| 82 | + extensions=[ |
| 83 | + SchemaDirectiveExtension, |
| 84 | + ], |
| 85 | +) |
| 86 | +``` |
| 87 | + |
| 88 | +Can be migrated to: |
| 89 | + |
| 90 | +```python |
| 91 | +import strawberry |
| 92 | +from strawberry_django.permissions import IsAuthenticated, HasRetvalPerm |
| 93 | + |
| 94 | +@strawberry.type |
| 95 | +class Query: |
| 96 | + fruit: Fruit = strawberry.django.field(extensions=[IsAuthenticated()]) |
| 97 | + fruit2: Fruit = strawberry.django.field(extensions=[HasRetvalPerm("can_view_fruit")]) |
| 98 | + |
| 99 | +schema = strawberry.schema( |
| 100 | + query=Query, |
| 101 | +) |
| 102 | +``` |
| 103 | + |
| 104 | +### 6) Types/Fields description from model's docstring and field's `help_text` |
| 105 | + |
| 106 | +The ability to retrieve types/fields description directly from the model's |
| 107 | +docstring and/or field's `help_text` is available on strawberry django, |
| 108 | +but it is an opt-in feature. |
| 109 | + |
| 110 | +To enable those, you can set the `"FIELD_DESCRIPTION_FROM_HELP_TEXT": True` and |
| 111 | +`"TYPE_DESCRIPTION_FROM_MODEL_DOCSTRING": True` in your |
| 112 | +[strawberry django settings](https://strawberry-graphql.github.io/strawberry-graphql-django/guide/settings/) |
| 113 | + |
| 114 | +### 7) Enjoy! 😊 |
| 115 | + |
| 116 | +If you followed all those steps correctly, your code should be working just like |
| 117 | +it was before. |
| 118 | + |
| 119 | +Be sure to check |
| 120 | +[strawberry django's documentation page](https://strawberry-graphql.github.io/strawberry-graphql-django/) |
| 121 | +to know more about all the feature it provides. |
| 122 | + |
| 123 | +Also, if you find any issues during your migration, be sure to open an issue at its repository. |
| 124 | + |
| 125 | +Don't forget you can also reach us in our [discord page](https://strawberry.rocks/discord). |
| 126 | + |
1 | 127 | ## Version 3.0.0 |
2 | 128 |
|
3 | 129 | ### Debug toolbar integration moved to strawberry-graphql-django |
|
0 commit comments