wundergraph.operations.ts Overview
Published: 2022-05-23
Configuring your Operations is a crucial part of configuring your WunderGraph Application. Find below an annotated implementation with lots of hints on how to properly configure everything.
You'll see that the underlying Interface needs to be imported from ./generated/wundergraphOperations.ts
.
This file as generated and specific to your application to make the configuration as straight forward as possible.
Depending on your GraphQL Schema and the Operations you've created, it gives you different options for configuration. All this helps you write your Configuration in a type-safe way. There's no guessing required, TypeScript is your friend.
import {configureWunderGraphOperations,MutationConfiguration,QueryConfiguration, SubscriptionConfiguration} from "./generated/wundergraph.operations.configuration";// disableAuth is a generic function to easily disable authentication for a specific Operationconst disableAuth = <Configs extends QueryConfiguration | MutationConfiguration | SubscriptionConfiguration>(config: Configs) :Configs => {return {...config,authentication: {required: false}}}// enableAuth is a generic function to easily enable authentication for a specific Operationconst enableAuth = <Configs extends QueryConfiguration | MutationConfiguration | SubscriptionConfiguration>(config: Configs) :Configs => {return {...config,authentication: {required: true}}}// enableCaching is a generic function to easily enable caching for an Operationconst enableCaching = (config: QueryConfiguration): QueryConfiguration =>({...config, caching: {...config.caching, enable: true}});const operations = configureWunderGraphOperations({// defaultConfig is the default base config for all Operations you create// the settings you choose here are automatically applied to all Operations// default configuration options can always be overwritten by the Operation config (below)defaultConfig: {authentication: {// in this case, we're requiring authentication for all operations by default// this means, there are no public endpoints, users must always login// this can be overwritten on a per-operation basisrequired: true}},// queries sets default settings for all Queriesqueries: config => ({// it's an arrow function that contains the default config// pass it along and override to not lose any settings...config,// WunderGraph comes with built-in caching which can be configured herecaching: {// disable caching by default for all Queriesenable: false,// caching is still disabled but if one Query enables it, stale-while-revalidate will be 60 secondsstaleWhileRevalidate: 60,// maxAge in seconds, again, by default still not enabledmaxAge: 60,// allow public caches, e.g. Varnish, Fastly, or any other CDN to Cache a responsepublic: true},// liveQuery is a WunderGraph feature that allows you to turn any API into a Realtime API// by using polling internallyliveQuery: {// set to true to enable it for all Queries// can be configured for each individual Operation as wellenable: true,// defines the polling interval in secondspollingIntervalSeconds: 2,}}),//mutations: config => ({...config,authentication: {// enable authentication for all Mutationsrequired: true}}),subscriptions: config => ({...config,authentication: {// disable authentication for all Subscriptions (not recommended, just as an example)required: false}}),custom: {// custom config for the TopProducts QueryTopProducts: config => ({// carry on defaults...config,caching: {// carry on defaults...config.caching,// switch on cachingenable: true},liveQuery: {// carry on defaults...config.LiveQuery,// set polling interval to 10 secondspollingIntervalSeconds: 10,},authentication: {// require authenticationrequired: true,}}),// custom config for the PriceUpdates SubscriptionPriceUpdates: config => ({// carry on defaults...config,authentication: {// require authrequired: true,}}),// configure Users QueryUsers: config => ({// carry on defaults...config,caching: {...config.caching,// enable caching (override defaults)enable: true,// set maxAge to 120 secondsmaxAge: 120,}}),}});export default operations;