Edit and preview content side-by-side with our new Live Preview
Hygraph
Docs

Remote Source configuration

#Add a remote source

Remote Sources - Select a typeRemote Sources - Select a type

The first step to adding a remote source to your project is selecting the type. You can create a custom source or use a pre-built one. Hygraph offers pre-built remote sources for CommerceTools and CommerceLayer.

  1. Navigate to the Schema Builder.
  2. In the left sidebar, find the Remote Sources section at the bottom of the list, then click +Add.
  3. Use the selection boxes at the top of the screen to select a remote source type:
    • Custom
    • CommerceTools
    • CommerceLayer

#Add a custom source

Add custom Remote SourceAdd custom Remote Source

After selecting a custom remote source, you can complete the following fields:

FieldInput
Display NameEnter a display name here. This name will appear in Hygraph.
PrefixThe prefix is auto-generated and will be added to all types that are created for this Remote Source to avoid name clashes.
DescriptionYou can optionally add a description of the Remote Source here.
Enable debugging checkboxYou can enable debugging for this Remote Source, which will provide more information in case of errors. Make sure to disable this once you finish setting up the Remote Source to avoid leaking sensitive data.
TypeUse the radio buttons to set the type to either REST or GraphQL, depending on the type of API that this remote source will connect to. More fields will display as a result of your selection. Check out this document's “Select a type” section to learn more about configuring REST and GraphQL types.

#Add a commercetools source

Add CommerceTools Remote SourceAdd CommerceTools Remote Source

After selecting a CommerceTools Remote Source, you will find that the screen now contains two distinct sections for you to complete. The first section is the OAuth configuration for CommerceTools:

FieldInput
Authorization URLEnter your CommerceTools authorization URL here. This is the Auth URL in the API client details view of your commercetools account. Instead of copying it directly from there, copy the URL on the first line of the cURL below. Paste that in the Authorization URL field in Hygraph.
ScopesProvide one or more scopes and click +Add. This is the Scope in the API client details view of your commercetools account. Click here to learn more about Commercetools scopes.
Client IDEnter your CommerceTools Client ID here. This is the client_id key in the API client details view of your commercetools account. Copy it and paste it in Hygraph. This is like a username.
Client SecretEnter your CommerceTools Client Secret here. This is the secret key in the API client details view of your commercetools account. Copy it and paste it in Hygraph. This is like a password.

Once you've completed this, you can move on to the second section, where you will configure the details of your Remote Source:

FieldInput
Display NameEnter a display name here. This name will appear in Hygraph.
PrefixThe prefix is auto-generated and will be added to all types that are created for this Remote Source to avoid name clashes.
DescriptionYou can optionally add a description of the remote source here.
Enable debugging checkboxYou can enable debugging for this remote source, which will provide more information in case of errors. Make sure to disable this once you finish setting up the Remote Source to avoid leaking sensitive data.
TypeUse the radio buttons to set the type to either REST or GraphQL, depending on the type of API that this remote source will connect to. More fields will display as a result of your selection. Check out this document's “Select a type” section to learn more about configuring REST and GraphQL types.

#Add a Commercelayer source

Add CommerceLayer Remote SourceAdd CommerceLayer Remote Source

After selecting a CommerceLayer remote source, you will find that the screen now contains two distinct sections for you to complete. The first section is the OAuth configuration for CommerceLayer:

FieldInput
Authorization URLEnter your CommerceLayer authorization URL here. Copy the Base endpoint in your Commercelayer credentials screen, paste it in Hygraph, and add /oauth/token at the end, like this: https://<YOUR_BASE_ENDPOINT>/oauth/token.
ScopesProvide one or more scopes and click +Add. This field is optional. Click here to learn more about Commercelayer scopes.
Client IDEnter your CommerceLayer Client ID here. Copy the Client ID in your Commercelayer credentials screen and paste it here.
Client SecretYou can optionally enter your CommerceLayer Client Secret here. This field is optional.

Once you've completed this, you can move on to the second section, where you will configure the details of your Remote Source:

FieldInput
Display NameEnter a display name here. This name will appear in Hygraph.
PrefixThe prefix is auto-generated and will be added to all types that are created for this Remote Source to avoid name clashes.
DescriptionYou can optionally add a description of the remote source here.
Enable debugging checkboxYou can enable debugging for this remote source, which will provide more information in case of errors. Make sure to disable this once you finish setting up the Remote Source to avoid leaking sensitive data.
TypeUse the radio buttons to set the type to either REST or GraphQL, depending on the type of API that this remote source will connect to. More fields will display as a result of your selection. Check out this document's “Select a type” section to learn more about configuring REST and GraphQL types.

#Select a type

#REST

Remote Source - RESTRemote Source - REST

The following fields display when you select the REST type:

FieldInput
Base URLEnter a base URL here. All Remote Fields that connect to this Remote Source will use that base URL.
Commercetools Base URL: To form the base URL, you will use the API URL and the project_key from the API client details view of your commercetools account, like this: https://<COMMERCETOOLS_API_URL>/<PROJECT_KEY>/graphql.
Commercelayer Base URL: Copy the Base endpoint in your Commercelayer credentials screen and paste it here.
HeadersOptionally, you can include HTTP headers that will be added to all API requests for all Remote Fields connected to this Remote Source. Example use cases include authorization and accepted media types.
Custom type definitionsYou can now define your custom types here. These provide the mapping from the API responses to the GraphQL schema. Custom input types can optionally also be defined here.

#GraphQL

Remote Source - GraphQLRemote Source - GraphQL

The following fields display when you select the GraphQL type:

FieldInput
Base URLEnter a base URL here. All Remote Fields that connect to this Remote Source will use that base URL.
Commercetools Base URL: To form the base URL, you will use the API URL and the project_key from the API client details view of your commercetools account, like this: https://<COMMERCETOOLS_API_URL>/<PROJECT_KEY>/graphql.
Commercelayer Base URL: Copy the Base endpoint in your Commercelayer credentials screen and paste it here.
HeadersOptionally, you can include HTTP headers that will be added to all API requests for all Remote Fields connected to this Remote Source. Example use cases include authorization and accepted media types.
Custom input type definitionYou can optionally provide an alternative introspection URL and custom headers to send to the introspection endpoint. If no separate introspection URL is provided, the base URL will be used (the default behavior for most GraphQL APIs is to allow querying and introspection on the same URL). Custom input types can optionally also be defined here.
Introspection methodSelect GET or POST.
If GET, we do a GET request and include the introspection query in the URL as a query parameter.
If POST, we do a POST request and include the introspection query in the body.
Introspection URLIf your introspection URL is different from the Remote Source URL, you can add it here.
Introspection headersHere, you can add the headers to pass along with the request to the remote source.
Simply add a key and value pair and click + Add header.

#Create a custom type definition

When connecting to a remote REST API you will have the option to define a Custom Type Definition, which allows you to specify the shape of the response coming from the API. It will allow you to query the REST API as if it were native GraphQL. These Custom Type Definitions use GraphQL SDL (Schema Definition Language).

In order to create such a definition, you can follow these steps:

  1. While adding a new Remote Source or editing an existing one, scroll down to Custom Type Definitions and click +Add.

  2. You can use our JSON to SDL converter tool to transform the JSON response from your API into valid GraphQL SDL. You might need to make a few tweaks to the generated SDL, especially changing the JSON type to Json (note the difference in casing).

    Here's an example of what that would look like:

  3. Add the SDL for your custom type to the input field. It's possible to add multiple types in a single input field, after saving the types will be moved to separate fields.

  4. Click Create or Save on the top right corner.

Custom types also support nesting, so they can make use of another type, as shown below:

type Product {
name: String
metaInfo: MetaInfo
slug: String
}
type MetaInfo {
createdAt: DateTime
createdBy: String
currency: String
}

In this example, the custom Product type makes use of another custom type MetaInfo.

#Create a custom input type definition

Remote Fields also allow you to pass along input parameters to your Remote Source endpoint. This can be useful if the identifiable information for the remote data isn't kept in Hygraph, but defined on a request basis. This is relevant for both the REST and GraphQL Remote Sources.

To create such a definition and to use it in a query, you will need to follow these steps:

  1. While adding a new Remote Source, or editing an existing one, scroll down to Custom Input Type Definition and click on +Add.

  2. Similar to the Custom Types, you need to use the GraphQL Schema Definition Language (SDL) to define what the input parameter will look like. A tool that could be used here is JSON2SDL, which allows translating a JSON object to a valid SDL. Keep in mind that we are not defining a type here, but an input.

    Let's take an example of passing a productId to our remote API, that will be used as an input argument. The SDL would look like this:

    input productInput {
    productId: String!
    }