Migrate to GraphQL Mesh v1
Live Queries
GraphQL Live Query implementation from Laurin Quast can be used in GraphQL Mesh with a few additions in the configuration.
npm i @graphql-mesh/plugin-live-queryMesh live-query config often names fields with Schema Coordinates
such as Mutation.addTodo or Query.todos
(RFC ). Invalidation can
also use live-query identifiers that embed values (for example Todo:123 or
Query.todo(id:"{args.id}")). Those identifiers are not Spec Schema Coordinates.
Basic Usage
You have a Query root field that returns all Todo entities from your data source like below.
query getTodos {
todos {
id
content
}
}And you want to update this operation result automatically without manual refresh when
Mutation.addTodo is called.
You only need to add the following to your existing configuration.
additionalTypeDefs: |
directive @live on QUERY
plugins:
- liveQuery:
invalidations:
- field: Mutation.addTodo
invalidate:
- Query.todosThen you can send a live query with @live directive.
query getTodos @live {
todos {
id
content
}
}This will start a real-time connection between the server and your client. The response of todos
will get updated whenever addTodo is called.
ID Based Invalidation
The patterns below are live-query identifiers (they can include argument values). They are not
Spec Argument Coordinates such as Query.todo(id:).
Let’s say you have the following query that returns a specific Todo entity based on id field;
query getTodo($id: ID!) {
todo(id: $id) {
id
content
}
}If you update this entity with editTodo mutation field on your backend, then you want to
invalidate this entity specifically instead of validating all todo queries;
invalidations:
- field: Mutation.editTodo
invalidate:
- Todo:{args.id}In a case where the field resolver resolves null but might resolve to an object type later, e.g., because the visibility got update the field that uses a specific id argument can be invalidated in the following way:
invalidations:
- field: Mutation.editTodo
invalidate:
- Query.todo(id:"{args.id}")Polling Based Invalidation
You can also invalidate queries in a polling interval by specifying a Spec
Schema Coordinate of the field to poll (for example
Query.products).
plugins:
- liveQuery:
invalidations:
- pollingInterval: 10000 # Polling interval in milliseconds
invalidate:
- Query.productsProgrammatic Usage
liveQueryStore is available in GraphQL Context, so you can access it in resolvers composition
functions that wrap existing resolvers or additional resolvers;
transforms:
- resolversComposition:
- resolver: Mutation.editTodo
composer: invalidate-todo#invalidateTodoAnd in this code file;
module.exports = {
invalidateTodo: next => async (root, args, context, info) => {
const result = await next(root, args, context, info)
context.liveQueryStore.invalidate(`Todo:${args.id}`)
return result
}
}Config API Reference
ttl(type:Float) - Maximum age in ms. Defaults toInfinity. Set it to 0 for disabling the global TTL.ttlPerCoordinate(type:Array of Object) - Overwrite the TTL for query operations whose selection contains a Spec Schema Coordinate (TypeorType.field, e.g.Query.usersorStock) or a Mesh schema selector with*(e.g.Query.*). Prefer Spec forms for one element; selectors stay supported for wildcards. See https://spec.graphql.org/September2025/#sec-Schema-Coordinates and https://github.com/graphql/graphql-wg/blob/main/rfcs/SchemaCoordinates.md :coordinate(type:String, required)ttl(type:Float, required)
ignoredTypes(type:Array of String) - Skip caching of following the types.idFields(type:Array of String) - List of fields that are used to identify the entity.invalidateViaMutation(type:Boolean) - Whether the mutation execution result should be used for invalidating resources. Defaults totrueincludeExtensionMetadata(type:Boolean) - Include extension values that provide useful information, such as whether the cache was hit or which resources a mutation invalidated.sessionId(type:String) - Allows to cache responses based on the resolved session id. Return a unique value for each session. Creates a global session by default. Example;
sessionId: "{context.headers.userId}"if(type:String) - Specify whether the cache should be used based on the context.
if: "context.headers.userId != null"cacheKey(type:String) - Customize the behavior how the response cache key is computed from the documentString, variableValues, contextValue and sessionId. If the given string is interpolated as empty, default behavior is used. Example;
# Cache by specific value
cacheKey: "{variableValues.userId}"
# Cache by documentString
cacheKey: "{documentString}"
# Cache by operationName
cacheKey: "{operationName}"
# Cache by some header value
cacheKey: "{contextValue.headers.authorization}"
# Or combine two of each
cacheKey: "{contextValue.headers.authorization}-{operationName}"shouldCacheResult(type:String) - Checks if the result should be cached.
shouldCacheResult: "result.errors.length > 0"CodeSandBox Example
You can learn more about GraphQL Live Query in its documentation. You can check out our example that uses live queries