GraphQL Resolver Context
Within Tailcall, Context
is a pivotal component that allows for dynamic retrieval of values during the resolution of fields for a given type within the schema.
Schema Definition​
type Context = {
args: Map<string, JSON>
value: JSON
env: Map<string, string>
vars: Map<string, string>
headers: Map<string, string>
}
Context
operates by storing values as key-value pairs, which can be accessed through mustache template syntax.
args​
This property facilitates access to query arguments. Consider the example:
type Query {
user(id: ID!): User @http(path: "/users/{{.args.id}}")
}
Here, args.id
is utilized to retrieve the id
argument provided to the user
query.
value​
This enables access to the fields of the specified type.
type Post {
id: ID!
title: String!
body: String!
comments: [Comment]
@http(path: "/posts/{{.value.id}}/comments")
}
In this case, value.id
accesses the id
field of the Post
type.
env​
Environment variables, set at server startup, allow directives to dynamically adapt behavior based on external configurations without altering the server configuration itself.
Example:
type Query {
users: [User]!
@http(baseUrl: "{{.env.API_ENDPOINT}}", path: "/users")
}
env.API_ENDPOINT
references an environment variable named API_ENDPOINT
, which specifies the base URL for HTTP requests.
vars​
vars
offers a mechanism for defining reusable variables within the configuration. Unlike env
, these are embedded and can be universally applied across configurations.
schema
@server(
vars: {key: "apiKey", value: "{{.env.AUTH_TOKEN}}"}
) {
query: Query
}
type Query {
user(id: ID!): [User]
@http(
url: "/users"
headers: [
{
key: "Authorization"
value: "Bearer {{.vars.apiKey}}"
}
]
)
}
Here, the variable apiKey
is set using an environment variable and subsequently utilized in the Authorization
header for HTTP requests.
headers​
Headers originate from the request made to the GraphQL server.
type Query {
commentsForUser: [Comment]
@http(path: "/users/{{.headers.x-user-id}}/comments")
}
In this example, headers.x-user-id
extracts the value of the x-user-id
header present in the request, dynamically constructing the request path.