模式指令#
Strawberry 支持模式指令,这些指令不会改变 GraphQL 模式的行为,而是提供了向其添加额外元数据的方法。
例如,Apollo Federation 集成是基于模式指令的。
模式中使用:
# directives.py
import strawberry
from strawberry.schema_directive import Location
@strawberry.schema_directive(locations=[Location.OBJECT])
class Keys:
fields: str
from .directives import Keys
@strawberry.type(directives=[Keys(fields="id")])
class User:
id: strawberry.ID
name: str
这将产生以下模式:
type User @keys(fields: "id") {
id: ID!
name: String!
}
重载字段名称#
可以使用 strawberry.directive_field
重载字段名称:
@strawberry.schema_directive(locations=[Location.OBJECT])
class Keys:
fields: str = strawberry.directive_field(name="as")
站点#
模式指令可以应用于模式的许多不同部分。以下是所有允许的站点列表:
Name |
Description |
|
---|---|---|
SCHEMA |
|
The definition of a schema |
SCALAR |
|
The definition of a scalar |
OBJECT |
|
The definition of an object type |
FIELD_DEFINITION |
|
The definition of a field on an object type or interface |
ARGUMENT_DEFINITION |
|
The definition of an argument |
INTERFACE |
|
The definition of an interface |
UNION |
|
The definition of an union |
ENUM |
|
The definition of a enum |
ENUM_VALUE |
|
The definition of a enum value |
INPUT_OBJECT |
|
The definition of an input object type |
INPUT_FIELD_DEFINITION |
|
The definition of a field on an input type |