Skip to main content

@rest

The @rest directive is defined as follows:

Directive Definition
Directive Definition
directive @rest(
"""
HTTP method for the REST endpoint
"""
method: Method!

"""
Path for the REST endpoint, supports dynamic values
"""
path: String!

"""
Query parameters as key-value pairs
"""
query: [InputKeyValue!]
) on OPERATION_DEFINITION

API orchestration is essential, yet not all can adopt GraphQL despite its benefits. The Tailcall DSL feature leverages GraphQL at compile time to generate REST endpoints, aligning with traditional API infrastructure like CDNs and Gateways.

Usage

  • method: Specifies the HTTP method (GET, POST, etc.).
  • path: Sets the endpoint URL, with support for dynamic values from query arguments.
  • query: Defines the query parameters as key-value pairs.

Example

Define GraphQL types and queries, using the @rest directive to map fields to REST API endpoints.

schema.graphql

schema
@link(type: Operation, src: "user-operation.graphql") {
query: Query
}

type Query {
user(id: Int!): User
@rest(method: "GET", path: "/users/{{.args.id}}")
}

type User {
id: Int!
name: String!
email: String!
}

user-operation.graphql

query ($id: Int!) @rest(method: GET, path: "/user/$id") {
user(id: $id) {
id
name
}
}

REST Demo

This example demonstrates how to define a simple query to fetch user data from a REST endpoint using the @rest directive. By leveraging @rest, GraphQL can serve as a layer over RESTful services, combining REST's simplicity with GraphQL's flexibility.