Edit in GitHubLog an issue

JsonSchema handlers

The JsonSchema handler allows you to load a single remote REST endpoint and define the request and response structures using predefined JSON schema files.

Copied to your clipboard
{
"meshConfig": {
"sources": [
{
"name": "carts",
"handler": {
"JsonSchema": {
"baseUrl": "<your_Commerce_url>",
"operations": [
{
"type": "Query",
"field": "data",
"path": "/cart",
"method": "GET",
"responseSchema": "./carts-response-schema.json"
}
]
}
}
}
]
},
}

If your REST service's request or response format is modified, you must update your mesh configuration file with the modified request or response. Then update your mesh to allow API Mesh to cache any changes.

If your source handler's schema is modified, you must update your mesh to allow API Mesh to cache any changes.

For more information on creating JSON schemas, refer to this JSON schema tutorial.

The JSON Schema handler uses the following format:

Copied to your clipboard
{
"sources": [
{
"name": "MyApi",
"handler": {
"JsonSchema": {
"baseUrl": "https://your-service-url/endpoint/",
"operations": [
{
"type": "Query",
"field": "users",
"path": "/users",
"method": "GET",
"responseSchema": "https://json-schema/schema.json"
}
]
}
}
}
]
}

JSON Schema handlers can also use local sources, see Reference local file handlers for more information.

The following example returns your header values, so you can confirm your headers are functioning properly.

Copied to your clipboard
{
"meshConfig": {
"sources": [
{
"name": "headersData",
"handler": {
"JsonSchema": {
"baseUrl": "<your baseUrl>",
"operationHeaders": {
"sample-operation-header": "sample operation value"
},
"schemaHeaders": {
"sample-schema-header": "sample schema value"
},
"operations": [
{
"type": "Query",
"field": "data",
"path": "/getHeadersData",
"method": "GET",
"responseSchema": "./getHeadersSchema.json"
}
],
"ignoreErrorResponses": false
}
}
}
],
"files": [
{
"path": "./getHeadersSchema.json",
"content": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"required\":[\"headerKeys\",\"headerValues\",\"headers\"],\"properties\":{\"headerKeys\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"headerValues\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"headers\":{\"type\":\"object\"}}}"
}
]
}
}

Headers from context

Copied to your clipboard
{
"sources": [
{
"name": "MyGraphQLApi",
"handler": {
"JsonSchema": {
"baseUrl": "https://some-service-url/endpoint-path/",
"operationHeaders": {
"Authorization": "Bearer {context.headers['x-my-api-token']}"
}
}
}
}
]
}

Query Parameters

There are multiple methods for defining query parameters. Select the method that fits your needs, or combine multiple methods:

Automatically declare query parameters

API Mesh automatically generates arguments for operations (if needed). Arguments are generated as nullable strings by default.

Copied to your clipboard
{
"sources": [
{
"name": "MyGraphQLApi",
"handler": {
"JsonSchema": {
"baseUrl": "https://your-service/endpoint/",
"operations": [
{
"type": "Query",
"field": "user",
"path": "/user?id={args.id}",
"method": "GET",
"responseSchema": "./json-schemas/user.json"
}
]
}
}
}
]
}

Manually declare query parameters

You can define the operation's arguments by modifying the argTypeMap config field according to the JSON Schema spec.

In this example, we declare a page argument as an object with limit and offset properties:

Copied to your clipboard
{
"argTypeMap": {
"page": {
"type": "object",
"properties": {
"limit": {
"type": "number"
},
"offset": {
"type": "number"
}
}
}
}
}

Arguments should be added to the path using the queryParamArgMap config field, especially for non-primitive types.

Here we add the page argument to the query parameters:

Copied to your clipboard
{
"sources": [
{
"name": "MyGraphQLApi",
"handler": {
"JsonSchema": {
"baseUrl": "https://your-service-/endpoint/",
"operations": [
{
"type": "Query",
"field": "users",
"path": "/getUsers",
"method": "GET",
"responseSample": "./jsons/MyField.response.json",
"responseTypeName": "MyResponseName",
"argTypeMap": {
"page": {
"type": "object",
"properties": {
"limit": {
"type": "number"
},
"offset": {
"type": "number"
}
}
}
},
"queryParamArgMap": {
"page": "page"
}
}
]
}
}
}
]
}

Config API reference

  • baseUrl (type: String) - URL or file path for your JSON schema.
  • schemaHeaders (type: JSON) - JSON object for adding headers to API calls for runtime schema introspection
  • operationHeaders (type: JSON) - JSON object for adding headers to API calls for runtime operation execution
  • operations - (required) Array of:
    • object:
      • field (type: String, required)
        • Cannot contain hyphens.
      • description (type: String)
      • type (type: String (Query | Mutation | Subscription), required)
      • requestSchema (type: Any)
      • requestSample (type: Any)
      • requestTypeName (type: String)
      • responseSchema (type: Any)
        • Remote files and URLs are not supported. You must provide a local path.
      • responseSample (type: Any)
        • Remote files and URLs are not supported. You must provide a local path.
      • responseTypeName (type: String)
      • argTypeMap (type: JSON)
  • ignoreErrorResponses (type: Boolean) - Flag for ignoring errors in the response
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.