Skip to main content

GraphQL

image

This handler allows you to load remote GraphQL schemas and use it with schema-stitching, based on graphql-tools.

To get started, install the handler library from NPM:

$ yarn add @graphql-mesh/graphql

Now, you can use it directly in your Mesh config file:

sources:
- name: MyGraphQLApi
handler:
graphql:
endpoint: http://my-service-url:3000/graphql

You can check out our example that uses schema stitching with a PostgreSQL datasource. Click here to open the example on GitHub

Dynamic Header Values (e.g. for Authorization)#

Mesh can take dynamic values from the GraphQL Context or the environmental variables. If you use mesh serve, GraphQL Context will be the incoming HTTP request.

The expression inside dynamic values should be as in JS.

From Context (HTTP Header for mesh serve)#

sources:
- name: MyGraphQLApi
handler:
graphql:
endpoint: http://my-service-url:3000/graphql
operationHeaders:
# Please do not use capital letters while getting the headers
Authorization: Bearer {context.headers['x-my-api-token']}
# You can also access to the cookies like below;
# Authorization: Bearer {context.cookies.myApiToken}

And for mesh serve, you can pass the value using x-my-graphql-api-token HTTP header.

From Environmental Variable#

MY_API_TOKEN is the name of the environmental variable you have the value.

sources:
- name: MyGraphQLApi
handler:
graphql:
endpoint: http://my-service-url:3000/graphql
operationHeaders:
Authorization: Bearer ${MY_API_TOKEN}

Config API Reference#

  • endpoint (type: String, required) - A url or file path to your remote GraphQL endpoint. If you provide a path to a code file(js or ts), other options will be ignored and the schema exported from the file will be used directly.
  • schemaHeaders (type: Any) - JSON object representing the Headers to add to the runtime of the API calls only for schema introspection You can also provide .js or .ts file path that exports schemaHeaders as an object
  • operationHeaders (type: JSON) - JSON object representing the Headers to add to the runtime of the API calls only for operation during runtime
  • useGETForQueries (type: Boolean) - Use HTTP GET for Query operations
  • method (type: String (GET | POST)) - HTTP method used for GraphQL operations
  • useSSEForSubscription (type: Boolean) - Use Server Sent Events instead of WebSocket for Subscriptions
  • customFetch (type: Any) - Path to a custom W3 Compatible Fetch Implementation
  • webSocketImpl (type: String) - Path to a custom W3 Compatible WebSocket Implementation
  • introspection (type: String) - Path to the introspection You can seperately give schema introspection
  • multipart (type: Boolean) - Enable multipart/formdata in order to support file uploads
  • batch (type: Boolean) - Batch requests