The Guild LogoThe Guild Monogram

Search docs

Search icon

Products by The Guild

Products

Hive logoHive blurred logo

Hive

Schema Registry for your GraphQL Workflows

Skip to main content

JSON Schema or Samples

image

This handler allows you to load any remote REST service, and describe it's request/response using the YAML config.

With this handler, you can easily customize and control the built GraphQL schema.

To get started, install the handler library from NPM:

$ yarn add @graphql-mesh/json-schema

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

sources:
- name: MyApi
handler:
jsonSchema:
baseUrl: https://some-service-url/endpoint-path/
operations:
- type: Query
field: users
path: /users
method: GET
responseSchema: ./json-schemas/users.json

Dynamic Values#

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

The expression inside dynamic values should be as in JS.

From Context (HTTP Header for mesh dev or mesh start)#

sources:
- name: MyGraphQLApi
handler:
jsonSchema:
baseUrl: https://some-service-url/endpoint-path/
operationHeaders:
# Please do not use capital letters while getting the headers
Authorization: Bearer {context.headers['x-my-api-token']}

And for mesh dev or mesh start, 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:
jsonSchema:
baseUrl: https://some-service-url/endpoint-path/
operationHeaders:
Authorization: Bearer {env.MY_API_TOKEN}
# You can also access to the cookies like below;
# Authorization: Bearer {context.cookies.myApiToken}

From Arguments#

Mesh automatically generates arguments for operations if needed;

sources:
- name: MyGraphQLApi
handler:
jsonSchema:
baseUrl: https://some-service-url/endpoint-path/
operations:
- type: Query
field: user
path: /user/{args.id}
method: GET
responseSchema: ./json-schemas/user.json

This example operation definition will generate a root field with id: ID argument, then Mesh will interpolate the expression in path to get id value from args.

Codesandbox Example#

You can check out our example that uses JSON Schema handler with mock data.

Config API Reference#

  • baseUrl (type: String)
  • operationHeaders (type: JSON)
  • schemaHeaders (type: JSON)
  • operations (type: Array of Object, required):
    • field (type: String, required)
    • path (type: String)
    • pubsubTopic (type: String)
    • description (type: String)
    • type (type: String (Query | Mutation | Subscription), required)
    • method (type: String (GET | HEAD | POST | PUT | DELETE | CONNECT | OPTIONS | TRACE | PATCH))
    • requestSchema (type: Any)
    • requestSample (type: Any)
    • requestTypeName (type: String)
    • responseSchema (type: Any)
    • responseSample (type: Any)
    • responseTypeName (type: String)
    • argTypeMap (type: JSON)
    • headers (type: JSON)
  • disableTimestampScalar (type: Boolean)
  • errorMessage (type: String) - Field name of your custom error object (default: 'message')