Decorators
TypeSpec.GraphQL
Section titled âTypeSpec.GraphQLâ@compose
Section titled â@composeâSpecify the GraphQL interfaces that should be implemented by a model.
The interfaces must be decorated with the @graphqlInterface decorator,
and all of the interfacesâ properties must be present and compatible.
@TypeSpec.GraphQL.compose(...interfaces: Model[])Model
Parameters
Section titled âParametersâ| Name | Type | Description |
|---|---|---|
| interfaces | Model[] |
Examples
Section titled âExamplesâ@graphqlInterface(#{ interfaceOnly: true })model Node { id: string;}
@compose(Node)model User { ...Node; name: string;}@graphqlInterface
Section titled â@graphqlInterfaceâMark this model as a GraphQL Interface. Interfaces can be implemented by other models
using the @compose decorator.
@TypeSpec.GraphQL.graphqlInterface(options?: valueof { interfaceOnly: boolean })Model
Parameters
Section titled âParametersâ| Name | Type | Description |
|---|---|---|
| options | valueof {...} | .interfaceOnly When true, the model will only be emitted as an interface (no âInterfaceâ suffix is added to the name). Use this for abstract interfaces that will never be used directly as output/input types (e.g., Node, Connection). Defaults to false. |
Examples
Section titled âExamplesâ@graphqlInterface(#{ interfaceOnly: true })model Node { id: string;}
@compose(Node)model User { ...Node; name: string;}// Emits: interface Node { id: String! }// type User implements Node { id: String!; name: String! }@mutation
Section titled â@mutationâSpecify the GraphQL Operation kind for the target operation to be MUTATION.
@TypeSpec.GraphQL.mutationOperation
Parameters
Section titled âParametersâNone
Examples
Section titled âExamplesâ@mutation op createUser(name: string): User;@nullable
Section titled â@nullableâMark a field, operation, or type as nullable in the emitted GraphQL schema.
Applied automatically by the mutation engine when it strips | null from
union types. The decoratorâs presence on the typeâs decorators array is
the signal â the implementation is a no-op.
@TypeSpec.GraphQL.nullableModelProperty | Operation | Union | Model
Parameters
Section titled âParametersâNone
@nullableElements
Section titled â@nullableElementsâMark a field or operation as having nullable array elements in the emitted GraphQL schema.
Applied automatically by the mutation engine when it detects Array<T | null>
patterns. Causes the emitter to emit [T] instead of [T!].
@TypeSpec.GraphQL.nullableElementsModelProperty | Operation
Parameters
Section titled âParametersâNone
Mark a model as a @oneOf input object in the emitted GraphQL schema.
This decorator is applied automatically by the mutation engine when it converts
a union type in input context to a synthetic input object (since GraphQL unions
are output-only). The emitter uses this to emit the @oneOf directive.
@TypeSpec.GraphQL.oneOfModel
Parameters
Section titled âParametersâNone
@operationFields
Section titled â@operationFieldsâAssign one or more operations or interfaces to act as fields with arguments on a model. The operations become fields on the GraphQL type with their parameters as arguments.
@TypeSpec.GraphQL.operationFields(...operations: Operation | Interface[])Model
Parameters
Section titled âParametersâ| Name | Type | Description |
|---|---|---|
| operations | Operation | Interface[] |
Examples
Section titled âExamplesâop followers(query: string): Person[];
@operationFields(followers)model Person { name: string;}// Emits: type Person { name: String!; followers(query: String!): [Person!]! }Specify the GraphQL Operation kind for the target operation to be QUERY.
@TypeSpec.GraphQL.queryOperation
Parameters
Section titled âParametersâNone
Examples
Section titled âExamplesâ@query op getUser(id: string): User;@schema
Section titled â@schemaâMark this namespace as describing a GraphQL schema and configure schema properties. All types and operations within the namespace will be emitted to a single GraphQL schema file.
@TypeSpec.GraphQL.schema(options?: valueof TypeSpec.GraphQL.Schema.SchemaOptions)Namespace
Parameters
Section titled âParametersâ| Name | Type | Description |
|---|---|---|
| options | valueof SchemaOptions |
Examples
Section titled âExamplesâ@schema(#{ name: "MyAPI" })namespace MyAPI { model User { id: string; name: string; } @query op getUser(id: string): User;}// Emits: MyAPI.graphql@specifiedBy
Section titled â@specifiedByâProvide a specification URL for a custom GraphQL scalar type.
This maps to the @specifiedBy directive in the emitted GraphQL schema.
@TypeSpec.GraphQL.specifiedBy(url: valueof url)Scalar
Parameters
Section titled âParametersâ| Name | Type | Description |
|---|---|---|
| url | valueof url | URL to the scalar type specification |
Examples
Section titled âExamplesâ@specifiedBy("https://scalars.graphql.org/andimarek/date-time")scalar DateTime extends utcDateTime;@subscription
Section titled â@subscriptionâSpecify the GraphQL Operation kind for the target operation to be SUBSCRIPTION.
@TypeSpec.GraphQL.subscriptionOperation
Parameters
Section titled âParametersâNone
Examples
Section titled âExamplesâ@subscription op onUserCreated(): User;