Remote Schemas in GrAMPS

Graphql has the ability to load and proxy other graphql servers using the remote schema functionality. This documentation will go into detail about how remote schemas work with GrAMPS.

Adding a remote schema into your GrAMPS set up it quite simple! For remote schemas you don’t need to create a connector, model, resolvers, or schema. That’s because the remote graphql server should already have the proper hook ups. Instead you just need to provide GrAMPS with the proper route to the remote graphql server. Let’s take a look at the most basic setup:

// index.js
export default {
namespace: 'Cool',
remoteSchema: {
url: "https://mycoolgraphqlserver.com/graphql", // URL to the remote graphql server
},
};

Remote schemas only need a single file. The index.js file. This needs to include a namespace so GrAMPS can keep track of the data source, and object called remoteSchema that defines the location of the remote server. In order for GrAMPS to pull in the remote schema the remote server needs to have the introspection option set to true.

Additionaly by default the schema pulled from the remote server will automatically be prepended with the name space. For example if you have a query, getId it will be converted to CoolGetId. This is to help prevent duplicate query, mutation, and subscriptions across multiple data sources.

There are a few more advanced configuration options. Let’s take a look at a fully fleshed out config and what each option is used for.

// index.js
export default {
namespace: 'Cool',
remoteSchema: {
url: "https://mycoolgraphqlserver.com/graphql", // URL to the remote graphql server
prefix: 'CL', // Highly recommended, prefix prepends each typename with the provided string. This helps prevent duplicate types across data sources.
exitOnRemoteFail: true, // If set to false the GrAMPS server will still build even if it can't access a remote schema. By default set to true.
setContextCallback: (req, previousContext) => { // A callback function to add any express middleware to each request.
return {
headers: { // This example shows a session token being attached to the request header for authentication.
Authorization: previousContext.req.session.token
}
}
}
},
};