Empower yourself as a developer by leveraging the simplicity and efficiency of WunderGraph Cosmo Schema Contracts. Schema Contracts allow you to build versatile, multi-audience graphs, simplifying development and ensuring the maintainability of your graph.
Term | Definition |
---|---|
Schema Contract | A Contract Graph or Contract is the filtered version of a Source Graph. |
Source Graph | A federated graph or monograph whose schema is to be filtered out. |
Router Schema | The schema used internally by the router to plan operations contains all fields, even those marked @inaccessible , which is necessary for accurate query planning. |
Client Schema | A Client Schema is derived from a Router Schema. It excludes all elements marked for removal in the Router Schema. A Client Schema is accessible to clients and can be exposed via introspection. |
@tags
and leveraging the Router Schema, Schema Contracts ensure that only the required data is exposed while maintaining the integrity and security of the source data. This allows you to create customized views for different clients or tenants without duplicating schemas or risking cross-tenant data leaks.
Move
and Monograph Publish
commands, the WunderGraph Cosmo CLI commands work the same for Schema Contracts as they would otherwise.@tags
to your subgraph schemas to define filtering rules. Second, publish the updated subgraph schemas to WunderGraph Cosmo using the WunderGraph Cosmo CLI. Third, create a Schema Contract using the WunderGraph Cosmo CLI, specifying the @tag
-based filtering rules you defined in the first step. Thereafter, you will be ready to deploy your Contract Schema to a WunderGraph Cosmo Router and start serving traffic.
Follow along with the steps below as we create a Schema Contract for an existing Federated Graph presently in use at a fictional, professional racing series to learn how you can do the same in your organization.
No Account? No Problem. Head on over to the WunderGraph Cosmo Studio and signup for a free account—the process is fast and no credit card is required!
npm
). This is currently the primary method available for obtaining the CLI. Here are the steps:
npm
comes bundled with NodeJS.npm
to Install the CLI:wgc
commands against your WunderGraph account.
Welcome Back! Upon returning to the CLI, you may be prompted to select an organization with which to work if your WunderGraph account is linked to multiple organizations—if you’re only in one organization, choose that one. Should the above workflow not function as expected, check the CLI’s output for additional context and further instructions.
@tags
to Subgraph Schemas to Define Filtering Rules@tags
in your subgraph schemas. Consider the following schema for a Driver
subgraph: Drivers Subgraph
@tag
directive to the field definition in the schema:
getDriverById
query is tagged as “public”, meaning it could be included in a Schema Contract for public-facing APIs. On the other hand, the getDriverBySSN
query is tagged as “private”, meaning it could be excluded from public-facing APIs and only included in Schema Contracts for internal use.
@tags
, we can publish the updated schemas to WunderGraph. First, let’s run a Schema Check to ensure that our new schema is valid:
✔ Schema check passed.
message in the terminal. If not, the CLI will provide a detailed error message to help us identify and resolve any issues. Once we’re confident that our new subgraph schema is ready to go, we can publish it to WunderGraph Cosmo.
✔ Subgraph published successfully
message in the terminal.
Note that the changes will be reflected in the WunderGraph Cosmo Studio when we publish our updated subgraph schema. However, at this point, we have only added @tags to our schema and published the updated schema. We haven’t yet created a Schema Contract that uses these tags to filter the schema.
Therefore, while we can see the @tags
in the schema in the Studio schema, there will be no functional changes to our API. The tags are merely annotations and do not have any effect until they are used in a Schema Contract. The actual filtering of the schema based on the @tags will only happen when we create a Schema Contract and specify which tags to filter. The functional changes will occur once the Schema Contract is created and deployed, and our API will start serving the filtered schema. Let’s do it!
@tags
to our subgraph schemas and published the updated schemas to WunderGraph Cosmo, we’re ready to create a Schema Contract! We create a Schema Contract for our organization using the WunderGraph Cosmo CLI contract create
command. When creating the Schema Contract, we specify which tags to filter. WunderGraph Cosmo then generates a Schema Contract that filters the tagged queries accordingly.
To create Schema Contracts for our drivers
subgraph, we can use the following command:
public
tag as the name of the Schema Contract, contracts-sandbox
as the Source Graph, and sensitive
and private
as the tags to exclude from the Schema Contract. This command tells WunderGraph Cosmo to create a Schema Contract named public
that filters out any fields tagged as sensitive
or private
from the Source Graph `contracts-sandbox`
. Furthermore, we specify the routing URL for the Schema Contract, which is the URL where the Schema Contract will be deployed.
If all goes well, we’ll see a ✔ Schema Contract created successfully
message in the terminal. Our Schema Contract is now ready to be deployed to a WunderGraph Cosmo Router and start serving traffic.
sensitive
or private
in the Source Graph.