| 1 |
# GraphQL schema |
| 2 |
|
| 3 |
GraphQL schema is generated by passing all the operations (query/mutation/subscription) functions to `apischema.graphql.graphql_schema`. |
| 4 |
|
| 5 |
Functions parameters and return types are then processed by *apischema* to generate the `Query`/`Mutation`/`Subscription` types with their resolvers/subscribers, which are then passed to `graphql.GraphQLSchema`. |
| 6 |
|
| 7 |
In fact, `graphql_schema` is just a wrapper around `graphql.GraphQLSchema` (same parameters plus a few extras); it just uses *apischema* abstraction to build `GraphQL` object types directly from your code. |
| 8 |
|
| 9 |
## Operations metadata |
| 10 |
|
| 11 |
GraphQL operations can be passed to `graphql_schema` either using simple functions or wrapping it into `apischema.graphql.Query`/`apischema.graphql.Mutation`/`apischema.graphql.Subscription`. These wrappers have the same parameters as `apischema.graphql.resolver`: `alias`, `conversions`, `error_handler`, `order` and `schema` (`Subscription` has an [additional parameter](#subscriptions)). |
| 12 |
|
| 13 |
```python |
| 14 |
{!operation.py!} |
| 15 |
``` |
| 16 |
|
| 17 |
## *camelCase* |
| 18 |
|
| 19 |
GraphQL use *camelCase* as a convention for resolvers; *apischema* follows this convention by automatically convert all resolver names (and their parameters) to *camelCase*. `graphql_schema` has an `aliaser` parameter if you want to use another case. |
| 20 |
|
| 21 |
## Type names |
| 22 |
|
| 23 |
Schema types are named the same way they are in generated JSON schema: type name is used by default, and it can be overridden using [`apischema.type_name`](../json_schema.md#customize-ref) |
| 24 |
|
| 25 |
```python |
| 26 |
{!graphql_type_name.py!} |
| 27 |
``` |
| 28 |
|
| 29 |
!!! note |
| 30 |
Type names can be distinguished between JSON schema and GraphQL schema using `type_name` named parameter. Indeed, `type_name("foo")` is equivalent to `type_name(json_schema="foo", graphql="foo")`. |
| 31 |
|
| 32 |
However, in GraphQL schema, unions must be named, so `typing.Union` used should be annotated with `apischema.type_name`. `graphql_schema` also provides a `union_ref` parameter which can be passed as a function to generate a type name from the union argument. Default `union_ref` is `"Or".join` meaning `typing.Union[Foo, Bar]` will result in `union FooOrBar = Foo | Bar` |
| 33 |
|
| 34 |
```python |
| 35 |
{!union_type_name.py!} |
| 36 |
``` |
| 37 |
|
| 38 |
## `Enum` metadata |
| 39 |
|
| 40 |
Contrary to dataclasses, `Enum` doesn't provide a way to set metadata for its members, especially description, but also deprecation reason. They can however be passed using `enum_schemas` parameter of `graphql_schema`. |
| 41 |
|
| 42 |
```python |
| 43 |
{!enum_schemas.py!} |
| 44 |
``` |
| 45 |
|
| 46 |
## Additional types |
| 47 |
|
| 48 |
*apischema* will only include in the schema the types annotating resolvers. However, it is possible to add other types by using the `types` parameter of `graphql_schema`. This is especially useful to add interface implementations where only interface is used in resolver types. |
| 49 |
|
| 50 |
```python |
| 51 |
{!additional_types.py!} |
| 52 |
``` |
| 53 |
|
| 54 |
## Subscriptions |
| 55 |
|
| 56 |
Subscriptions are particular operations which must return an `AsyncIterable`; this event generator can come with a dedicated resolver to post-process the event. |
| 57 |
|
| 58 |
### Event generator only |
| 59 |
|
| 60 |
```python |
| 61 |
{!subscription.py!} |
| 62 |
``` |
| 63 |
|
| 64 |
!!! note |
| 65 |
Because there is no post-processing of generated event in a dedicated resolver, `error_handler` cannot be called, but it will still modify the type of the event. |
| 66 |
|
| 67 |
### Event generator + resolver |
| 68 |
|
| 69 |
A resolver can be added by using the `resolver` parameter of `Subscription`. In this case, *apischema* will map subscription name, parameters and return type on the resolver instead of the event generator. It allows using the same event generator with several resolvers to create different subscriptions. |
| 70 |
|
| 71 |
The first resolver argument will be the event yielded by the event generator. |
| 72 |
|
| 73 |
```python |
| 74 |
{!subscription_resolve.py!} |
| 75 |
``` |
| 76 |
|