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[])Target
Section titled “Target”Model
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| interfaces | Model[] | The models to compose into the target, each emitted as a GraphQL interface. |
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 })Target
Section titled “Target”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.mutationTarget
Section titled “Target”Operation
Parameters
Section titled “Parameters”None
Examples
Section titled “Examples”@mutation op createUser(name: string): User;@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[])Target
Section titled “Target”Model
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| operations | Operation | Interface[] | The operations, or interfaces of operations, to add to the target as fields. |
Examples
Section titled “Examples”op followers(query: string): Person[];
@operationFields(followers)model Person { name: string;}// Emits: type Person { name: String!; followers(query: String!): [Person!]! }@query
Section titled “@query”Specify the GraphQL Operation kind for the target operation to be QUERY.
@TypeSpec.GraphQL.queryTarget
Section titled “Target”Operation
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)Target
Section titled “Target”Namespace
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| options | valueof SchemaOptions | Options for the schema, such as its name. |
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)Target
Section titled “Target”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.subscriptionTarget
Section titled “Target”Operation
Parameters
Section titled “Parameters”None
Examples
Section titled “Examples”@subscription op onUserCreated(): User;