Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Run Custom Schema Checks

Extend GraphOS schema checks with custom validations


This feature is only available with a GraphOS Enterprise plan.
You can test it out by signing up for a free Enterprise trial.

Your organization's validation needs might extend beyond default . For example, you might want to enforce schema standards in addition to default GraphOS linter rules. You might require validations that depend on external data, such as ensuring schema changes are only submitted from a specific branch in version control. Custom schema checks let you supplement default checks by executing custom validations using your own services.

How custom checks work

Once you configure custom checks, sends schema change information to an endpoint you specify. Your endpoint performs validations and reports potential issues back to GraphOS via the Platform API. displays the issues you report along with other check result details.

Schema check run showing custom check errors and warnings in GraphOS Studio

Custom checks run alongside other check types as part of every schema check run. Like , linter, and other check types, custom checks only run after a build check has successfully completed.

Prerequisites

Custom checks require using Rover CLI version 0.27.0 or later when you run schema checks.

Configuration

Custom checks require you to set up an HTTPS endpoint that's accessible via the public internet. You can set secret tokens to ensure that your endpoint only responds to requests sent by GraphOS. Once you register your endpoint in GraphOS Studio, GraphOS sends it webhook notifications as POST requests.

You can only register one endpoint for custom checks, but that endpoint can perform as many different schema validations as you want. You can write and deploy your validation service using any language or framework as long as the endpoint conforms to the expectations listed below.

Set up a validation endpoint

Your validation endpoint should:

  1. Be able to receive and process an HTTP POST request with a payload with the specified JSON format.
  2. Send a 200 response to the request.
    • When GraphOS dispatches a webhook with check information, it has a 30-second timeout to receive a 200 response.
    • If the request times out or doesn't receive a 200, it will retry up to five times, with an exponential backoff delay starting at 5 seconds and going up to 60 seconds.
  3. Submit validation check results to the GraphOS Platform API.

Outside of these requirements, you can implement any validations you need.

Webhook format

On every schema run, GraphOS sends your registered endpoint a POST request. The request has a JSON payload with the following shape:

{
// Type of event, indicating a custom schema check in Apollo GraphOS
"eventType": "APOLLO_CUSTOM_CHECK",
// The unique identifier of the event
"eventId": "UUID",
// Version of the payload shape
"version": "1",
"checkStep": {
// The unique identifier of the graph being validated
"graphId": "string",
// The variant of the graph (e.g., "current", "staging")
"graphVariant": "string",
// A unique identifier for the specific custom check task
"taskId": "UUID",
// A unique identifier for the workflow that initiated this check
"workflowId": "UUID",
"gitContext": {
// The branch from which the schema change is being submitted
"branch": "string",
// The commit hash associated with the schema change
"commit": "string",
// The name or email of the person who committed the change
"committer": "string",
// The commit message describing the schema change
"message": "string",
// The URL of the remote Git repository (e.g., GitHub or GitLab)
"remoteUrl": "string"
}
},
"baseSchema": {
// The hash of the current base schema
"hash": "string",
// A list of subgraphs that make up the base schema (if using Apollo Federation)
"subgraphs": [
{
// The hash of the subgraph schema
"hash": "string",
// The name of the subgraph
"name": "string"
}
]
},
"proposedSchema": {
// The hash of the proposed schema
"hash": "string",
// A list of subgraphs in the proposed schema (if using Apollo Federation)
"subgraphs": [
{
// The hash of the subgraph schema
"hash": "string",
// The name of the subgraph
"name": "string"
}
]
}
}

NOTE

The version attribute is included because payloads may include additional properties in the future.

Because schemas can be large, the payload provides schema hashes rather than full-text schemas. Your endpoint can request the full schema contents using the GraphOS Platform API. Refer to Platform API docs for instructions on making and authenticating requests. See the examples below for fetching schema contents by hash.

Using the schema contents and payload information, your endpoint can perform whatever validations you need.

Fetch a single schema

To fetch a single or contents by hash, use the following on the Platform API:

query schema($graphId: ID!, $hash: SHA256) {
graph(id: $graphId) {
doc(hash: $hash) {
source
}
}
}

Fetch all schemas

To request all and in one request, use the following GraphQL query on the Platform API:

query schemas($hashes: [SHA256!]!, $graphId: ID!) {
graph(id: $graphId) {
docs(hashes: $hashes) {
hash
source
}
}
}

Submitting a check result

Once your endpoint has completed its validation, it should submit the results back to the triggering schema check run using the Platform API. The triggering schema check run waits for a response for ten minutes and doesn't complete until it receives it. If ten minutes pass without a response, the schema check run is marked as failed.

The results should include a list of violations, each with the following:

  • level: Violation severity level, either ERROR, WARN, or IGNORE
  • rule: Name of the violation type
  • message: Error message

Example check results mutation

You can use the following GraphQL on the Platform API to submit custom check results:

mutation SchemaCheckCallback(
$input: CustomCheckCallbackInput!
$name: String!
$graphId: ID!
) {
graph(id: $graphId) {
variant(name: $name) {
customCheckCallback(input: $input) {
__typename
... on CustomCheckResult {
violations {
level
message
rule
}
}
... on PermissionError {
message
}
... on TaskError {
message
}
... on ValidationError {
message
}
}
}
}
}

Enable custom checks in Studio

Once your endpoint is ready, you need to register it in GraphOS Studio.

  1. In GraphOS Studio, go to your 's Checks page.

  2. Select Configuration in the upper right to open the checks configuration page.

  3. From the checks configuration page, open the Custom Checks section.

  4. Toggle on the switch next to Enable Custom Checks.

  5. Click the Edit button next to Registered webhook and enter the Endpoint URL.

  6. Optionally, enter a Secret Token.

    If you enter a token, each notification HTTP request includes an x-apollo-signature header whose value is a Hash Message Authentication Code (HMAC) generated using the token, the request body as the message, and the SHA256 hash function. The x-apollo-signature header has the format sha256=<hmac-value>.

    Refer to this guide from Okta to learn more about implementation and see additional resources.

  7. You should now see your configured webhook on the checks configuration page. Click Send test notification to test your endpoint.

From now on, whenever you run schema checks, the check run will include a Custom Check task.

Custom check run in GraphOS Studio

Example validation service implementation

Refer to the example implementation for a custom check validation service that enforces GraphQL ESLint rules.

Previous
Run Schema Checks
Next
Check Configurations
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company