The user wants to create a Continuous Integration (CI) pipeline that checks for schema differences (diff) and ensures backward compatibility for two specific technologies: GraphQL and Protocol Buffers (protobuf).
Context: CI (Continuous Integration), likely using GitHub Actions or a similar platform (GitLab CI, CircleCI), but GitHub Actions is the safest default assumption or a platform-agnostic approach.
2. GraphQL Strategy:
Tooling: The industry standard for this is graphql-inspector (by Kamil Kisiela/The Guild). It handles diffing, validation, and breaking change detection. Another option is Apollo Studio, but that's more of a SaaS platform. graphql-inspector is CLI-friendly and perfect for CI.
Workflow:
Install dependencies.
Get the old schema (from the base branch, e.g., main).
buf (Buf Connect): This is the modern, best-in-class tool for Protobuf linting and breaking change detection. It's the de facto standard now.
protoc with plugins: Old school, messy, hard to configure compatibility checks manually.
Decision: Use buf.
Workflow:
Install buf.
Lint (optional but recommended).
Breaking change detection against a specific branch (e.g., main or a git commit).
4. CI Platform Selection:
GitHub Actions is the most common and easiest to demonstrate. I will structure the solution around GitHub Actions but mention that the commands are portable to other CI systems.
5. Structuring the Response:
Introduction: Briefly explain the tools (graphql-inspector and buf).
Section 1: GraphQL CI Setup:
Pre-requisites (schema generation).
The GitHub Action configuration.
Command explanation.
Section 2: Protobuf CI Setup:
Pre-requisites (config file).
The GitHub Action configuration.
Command explanation.
Section 3: Combined Workflow: How to put them together in one file.
Section 4: Compatibility Rules (The "Why"): Explain what counts as a breaking change for both.
Action: There is a pre-made Action graphql-inspector/github-action, but writing the raw commands is often more transparent and flexible. I'll stick to the CLI approach via npm for clarity, or use the specific Action if it's well-maintained. Actually, the CLI approach is more robust against version drift. Let's use npx @graphql-inspector/cli.
Scenario: Usually schemas are built from code (e.g., code-first). I should assume the user needs to generate the schema file first or has a file ready. I'll assume a schema.graphql file exists or is generated.
7. Drafting the Content - Protobuf:
Tool:bufbuild/buf-setup-action and bufbuild/buf-breaking-action.
If code-first (NestJS, TypeORM, Go), the schema must be generated before the diff step. I should add a "Schema Generation" step.
Monorepos: I'll keep it simple (single repo) but mention paths filtering.
9. Final Polish:
Make the YAML valid.
Add comments explaining each step.
Provide a clear list of "Breaking Changes" examples.
Self-Correction during drafting:
Initial thought: Use protoc with a python script for proto.
Correction: No, that's painful. buf is the answer. It handles the "Against" logic (comparing against a git branch) seamlessly.
Initial thought: Use Apollo Rover.
Correction: Apollo requires a registry (GraphOS). While powerful, it's a vendor lock-in step for a general request. graphql-inspector works locally against files/git, which is simpler for a generic "CI for diff" request.
10. Constructing the Output:
Title: Robust Schema Diff CI.
Part 1: GitHub Actions YAML. Combine both into one workflow file for convenience.
Part 2: GraphQL Details. (Install inspector, compare git vs local).
Part 3: Protobuf Details. (Buf setup, buf breaking).
Part 4: Configuration files. (Package.json or buf.yaml).
Let's write the code.
GraphQL Step:npx @graphql-inspector/cli diff 'git:origin/main:path/to/schema.graphql' './schema.graphql'
Wait, does graphql-inspector support git targets out of the box? Yes, via graphql-inspector-cli commands. Actually, standard practice is often checking out the previous commit or comparing against a remote file.
Better approach: Use actions/checkout to get the current code. Then use the graphql-inspector action or CLI to compare against origin/main.
Let's use the CLI approach:
Checkout PR branch.
Fetch main history.
Run command: graphql-inspector diff git:origin/main:schema.graphql schema.graphql
Protobuf Step:
Setup Buf.
Run buf breaking proto --against '.git#branch=main'.
This looks solid. I will present the solution using GitHub Actions as the platform, as it is the most common standard for this type of request.
To ensure API stability and prevent breaking changes, the industry standard approach involves using GraphQL Inspector for GraphQL and Buf for Protocol Buffers (Protobuf).
Here is a complete guide and CI workflow (GitHub Actions) to implement schema diffing and compatibility checks.
Prerequisites
GraphQL: You have a .graphql schema file (or your server can introspect).
Protobuf: You have .proto files.
1. The CI Workflow (GitHub Actions)
Create a file named .github/workflows/schema-check.yml.
This workflow runs on Pull Requests. It compares the schema in your PR branch against the schema in your main branch.
yaml
name: Schema Compatibility Check
on:
pull_request:
branches: [ main ]
jobs:
schema-check:
runs-on: ubuntu-latest
steps:
- name: Checkout Branch
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to compare history
# --- GraphQL Setup & Check ---
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install GraphQL Inspector
run: npm install -g @graphql-inspector/cli @graphql-inspector/commands
- name: Validate GraphQL Schema
# Compare 'schema.graphql' in current PR vs 'origin/main'
run: |
echo "Checking GraphQL breaking changes..."
graphql-inspector diff \
'./schema.graphql' \
'git:origin/main:./schema.graphql' \
--rule safe-unwrapper \
--rule remove-type \
--rule remove-field \
--rule remove-enum-value
# Note: If using code-first (e.g. NestJS), run a build step first to generate the schema file.
# --- Protobuf Setup & Check ---
- name: Setup Buf (Protobuf Tool)
uses: bufbuild/buf-setup-action@v1
with:
version: '1.28.1'
- name: Buf Lint (Protobuf)
uses: bufbuild/buf-lint-action@v1
# Optional: input: 'path/to/proto/root'
- name: Buf Breaking Change Check
uses: bufbuild/buf-breaking-action@v1
with:
# The 'against' branch defines the baseline
against: 'buf.yaml'
input: '.'
# If you don't use the BSR (Buf Schema Registry), compare against git:
# against: 'git:origin/main#subdir=.'
2. Tool Configuration
GraphQL Configuration
The graphql-inspector CLI works out of the box with standard schema files.
If you need to ignore specific breaking changes (e.g., you allow removing deprecated fields), you can create a .graphql-inspector.yaml file:
yaml
# .graphql-inspector.yaml
diff:
# Ignore specific rules
ignore:
- "MyType.deprecatedField"
# Fail only on breaking changes, not dangerous ones
failOnBreaking: true
Protobuf Configuration (Buf)
Buf requires a buf.yaml configuration file in the root of your Protobuf definitions.