Supergraph Previews for every Pull Request - WunderGraph
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
When developing new features, it is crucial to test them in a production-like environment. By generating a preview for each pull request (PR), developers can ensure that their changes are thoroughly tested in such an environment before being merged. By utilizing Graph Feature Flags, the continuous integration (CI) pipeline can deploy a new feature subgraph and corresponding feature flag for each PR, enabling one composition per PR without deploying all subgraphs. The only requirement is a staging environment deployed from the main branches of your subgraphs. Graph feature flags allow you to “override” the subgraphs for a specific pull request.
Usage
Prerequisites
Before using the Cosmo Previews GitHub Action, ensure that the following prerequisites are met:
Setup
To use this action in your repository:
- Create a
.github/cosmo.yamlfile that defines the configuration for your feature subgraphs and feature flags.
version: '0.0.1'
namespace: '<your-namespace>'
feature_flags:
- name: '<your-feature-flag>'
labels:
- '<label-1>'
- '<label-2>'
subgraphs:
- name: '<subgraph-1-name>'
schema_path: '<path-to-schema-1>'
routing_url: '<routing-url-1>'
- name: '<subgraph-2-name>'
schema_path: '<path-to-schema-2>'
routing_url: '<routing-url-2>'
- The paths to schema files should be relative to the root of your repository.
- The
routing_urlcan include the placeholder string ’${PR_NUMBER}’, which will be replaced with the actual PR number. This enables deployment of the subgraph to a unique URL for each pull request. - Feature flag labels must match the federated graph for which the preview is created.
- Ensure that the subgraphs mentioned in cosmo.yaml are a part of a federated graph.
- Add the following GitHub Action workflow to your repository.
Example Workflow
name: Cosmo Previews
on:
pull_request:
branches:
- main
types:
- opened
- reopened
- closed
- synchronize
jobs:
create:
runs-on: ubuntu-latest
if: github.event.action == 'opened' || github.event.action == 'reopened'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.10.0
cache: npm
- name: Install wgc
run: npm i -g wgc@latest
- name: Create Cosmo Previews
uses: wundergraph/cosmo-previews@v0.1.0
with:
config_path: .github/cosmo.yaml
create: true
cosmo_api_key: ${{ secrets.COSMO_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
update:
runs-on: ubuntu-latest
if: github.event.action == 'synchronize'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.10.0
cache: npm
- name: Install wgc
run: npm i -g wgc@latest
- name: Update Cosmo Previews
uses: wundergraph/cosmo-previews@v0.1.0
with:
config_path: .github/cosmo.yaml
update: true
cosmo_api_key: ${{ secrets.COSMO_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
destroy:
runs-on: ubuntu-latest
if: github.event.action == 'closed'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.10.0
cache: npm
- name: Install wgc
run: npm i -g wgc@latest
- name: Destroy Cosmo Previews
uses: wundergraph/cosmo-previews@v0.1.0
with:
config_path: .github/cosmo.yaml
destroy: true
cosmo_api_key: ${{ secrets.COSMO_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
Action Parameters
config_path: The path to yourcosmo.yamlconfiguration file.create: Set totrueto create the feature flag and subgraphs.update: Set totrueto update the feature subgraphs.destroy: Set totrueto destroy the feature flag and subgraphs.cosmo_api_key: Your Cosmo API key stored in GitHub secrets.github_token: Your GitHub token, typically${{ secrets.GITHUB_TOKEN }}.
Jobs
Create
The create job is triggered when a pull request is opened or reopened. It performs the following actions:
- Checks out the repository.
- Sets up Node.js using the version specified.
- Installs the latest version of the
wgcCLI tool. - Creates feature subgraphs for all the GraphQL files modified in the pull request, provided that their corresponding subgraph configuration is specified in the
cosmo.yamlfile. - Creates all the feature flags specified in the
cosmo.yamlfile.
Update
The update job is triggered when a pull request is synchronized. It performs the following actions:
- Checks out the repository.
- Sets up Node.js with the specified version.
- Installs the latest version of the
wgcCLI tool. - Creates/Updates the feature subgraphs for all the newly modified GraphQL files in the pull request, provided that their corresponding subgraph configuration is specified in the
cosmo.yamlfile. - Updates all the feature flags specified in the
cosmo.yamlfile.
Destroy
The destroy job is triggered when a pull request is closed. It performs the following actions:
- Checks out the repository.
- Sets up Node.js using the version specified.
- Installs the latest version of the
wgcCLI tool. - Destroys all the feature flags and feature subgraphs created for the pull request.
Outputs
feature_subgraphs_to_deploy: A list of feature subgraphs to deploy. This output is provided in the create and update jobs.feature_subgraphs_to_destroy: A list of feature subgraphs to destroy. This output is provided in the update and destroy jobs.
Limitations
- The cosmo.yaml file should not be changed after the pull request is opened. If changes are to be made, the pull request should be closed and reopened. Make sure that the destroy action is completed before reopening the pull request.