Adding a Remote Schema
Follow the steps below to merge your remote GraphQL API with the GraphQL engine's auto-generated API.
Step 0: Write a custom GraphQL server
You can skip this step if you are using a 3rd party GraphQL API or already have a functional GraphQL server that meets your requirements.
You need to create a custom GraphQL server with a schema and corresponding resolvers that solve your use case.
You can use any language/framework of your choice to author this server and deploy it anywhere. A great way to get started is to use one of our boilerplates.
Step 1: Merge remote schema
The following details need to be specified for merging a remote schema:
Remote Schema name: an alias for the remote schema that must be unique on an instance of the GraphQL engine.
GraphQL server URL: the endpoint at which your remote GraphQL server is available. This value can be entered manually or by specifying an environment variable that contains this information.
Headers: configure the headers to be sent to your custom GraphQL server:
Toggle forwarding all headers sent by the client (when making a GraphQL query) to your remote GraphQL server.
Send additional headers to your remote server - these can be static header name-value pairs; and/or pairs of "header name-environment variable name". You can specify the value of the header to be picked up from the environment variable.
Example: Let's say your remote GraphQL server needs a
X-Api-Keyas a header. As this value contains sensitive data (like API key in this example), you can configure the name of an environment variable which will hold the value. This environment variable needs to be present when you start the GraphQL engine. When Hasura sends requests to your remote server, it will pick up the value from this environment variable.
Head to the
Remote Schemas tab of the console and click on the
Add button on the left sidebar.
Add the required details and click on the
Add Remote Schema button to merge the remote schema.
To add a remote schema, edit the
remote_schemas.yaml file in the
metadata directory as in this example:
- name: my-remote-schema
Apply the metadata by running:
hasura metadata apply
You can add a remote schema by using the add_remote_schema metadata API:
POST /v1/metadata HTTP/1.1
If you are running Hasura using Docker, ensure that the Hasura Docker container can reach the server endpoint. See this page for Docker networking.
If you are adding the URL using env variable, then run the Hasura docker container with the env variable added during
docker run. Example
Using environment variables
If you are using environment variables in the remote schema configuration - either for URL or headers - the environment variables need to be present with valid values when adding the remote schema i.e. the GraphQL engine should be started with these environment variables.
Step 2: Make queries to the remote server from Hasura
- Via console
- Via API
Now you can head to the
API tab and make queries to your remote server from Hasura.
You can query your remote server by making requests to the Hasura GraphQL endpoint (
Points to remember
Remote schema fields nomenclature
- Top-level field names need to be unique across all merged schemas (case-sensitive match).
- Types with the exact same name and structure will be merged. But types with the same name but different structure will result in type conflicts.
A remote server's GraphQL schema is cached and refreshed only when user explicitly reloads the remote schema.
Remote Schemas -> [remote_schema_name] -> Details and click the
Make a request to the reload_remote_schema metadata API.
- Subscriptions on remote GraphQL servers are not supported.
Extending the auto-generated GraphQL schema fields
For some use cases, you may need to extend the GraphQL schema fields exposed by the Hasura GraphQL engine and not merely add new fields as we have done here.
To achieve this you can use community tooling to write your own client-facing GraphQL gateway which wraps around and interacts with the GraphQL engine API underneath.
Adding an additional layer on top of the Hasura GraphQL engine significantly impacts the performance provided by it out of the box (by as much as 4x). If you need any help with remodelling these kinds of use cases to use the built-in remote schemas feature, please get in touch with us on GitHub.
Data Federation with Hasura - Watch Webinar.