Schema Directives

Strawberry supports schema directives , which are directives that don’t change the behavior of your GraphQL schema but instead provide a way to add additional metadata to it.

For example our Apollo Federation integration is based on schema directives.

Let’s see how you can implement a schema directive in Strawberry, here we are creating a directive called keys that can be applied to Object types definitions and accepts one parameter called fields . Note that directive names, by default, are converted to camelCase on the GraphQL schema.

Here’s how we can use it in our schema:

import strawberry
from strawberry.schema_directive import Location
class Keys:
fields: str
from .directives import Keys
class User:
id: strawberry.ID
name: str

This will result in the following schema:

type User @keys(fields: "id") {
id: ID!
name: String!

Overriding field names

You can use strawberry.directive_field to override the name of a field:

class Keys:
fields: str = strawberry.directive_field(name="as")


Schema directives can be applied to many different parts of a schema. Here’s the list of all the allowed locations:

SCHEMAstrawberry.Schema The definition of a schema
SCALARstrawberry.scalar The definition of a scalar
OBJECTstrawberry.type The definition of an object type
FIELD_DEFINITIONstrawberry.field The definition of a field on an object type or interface
ARGUMENT_DEFINITIONstrawberry.argument The definition of an argument
INTERFACEstrawberry.interface The definition of an interface
UNIONstrawberry.union The definition of an union
ENUMstrawberry.enum The definition of a enum
ENUM_VALUEstrawberry.enum_value The definition of a enum value
INPUT_OBJECTstrawberry.input The definition of an input object type
INPUT_FIELD_DEFINITIONstrawberry.field The definition of a field on an input type
Edit this page on GitHub