Skip to Content

Configure Operations

Published: September 09, 2021

Configuring your Operations is a crucial part of configuring your WunderGraph Application. Find below an annotated ConfigureOperations implementation.

You'll see that the underlying Interface needs to be imported from ./generated/operations. This is because it's tailord to your application. 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 {ConfigureOperations} from "./generated/operations";
const operations: ConfigureOperations = {
// 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 basis
required: true
}
},
// queries sets default settings for all Queries
queries: 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 here
caching: {
// disable caching by default for all Queries
enable: false,
// caching is still disabled but if one Query enables it, stale-while-revalidate will be 60 seconds
staleWhileRevalidate: 60,
// maxAge in seconds, again, by default still not enabled
maxAge: 60,
// allow public caches, e.g. Varnish, Fastly, or any other CDN to Cache a response
public: true
},
// liveQuery is a WunderGraph feature that allows you to turn any API into a Realtime API
// by using polling internally
liveQuery: {
// set to true to enable it for all Queries
// can be configured for each individual Operation as well
enable: true,
// defines the polling interval in seconds
pollingIntervalSeconds: 2,
}
}),
//
mutations: config => ({
...config,
authentication: {
// enable authentication for all Mutations
required: true
}
}),
subscriptions: config => ({
...config,
authentication: {
// disable authentication for all Subscriptions (not recommended, just as an example)
required: false
}
}),
custom: {
// custom config for the TopProducts Query
TopProducts: config => ({
// carry on defaults
...config,
caching: {
// carry on defaults
...config.caching,
// switch on caching
enable: true
},
liveQuery: {
// carry on defaults
...config.LiveQuery,
// set polling interval to 10 seconds
pollingIntervalSeconds: 10,
},
authentication: {
// require authentication
required: true,
}
}),
// custom config for the PriceUpdates Subscription
PriceUpdates: config => ({
// carry on defaults
...config,
authentication: {
// require auth
required: true,
}
}),
// configure Users Query
Users: config => ({
// carry on defaults
...config,
caching: {
...config.caching,
// enable caching (override defaults)
enable: true,
// set maxAge to 120 seconds
maxAge: 120,
}
}),
}
}

Don't repeat yourself#

You might find yourself in situations where you have to duplicate a lot of code because similar settings apply to multiple Operations. Luckily, we're in "Infrastructure as Code Land", so you can create some convenient helper functions to keep things DRY:

// enableCaching takes in a QueryConfiguration and switches caching on
const enableCaching = (config: QueryConfiguration) :QueryConfiguration => ({
...config,
caching: {
...config.caching,
enable: true,
}
});
// requireAuth also takes a QueryConfiguration and switches auth on
const requireAuth = (config: QueryConfiguration) : QueryConfiguration => ({
...config,
authentication: {
required: true,
}
})
const operations: ConfigureOperations = {
...
custom: {
Countries: enableCaching,
OasUsers: requireAuth,
}
}

As you can see, the custom Operation config is becoming a lot cleaner!


Product

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2021 WunderGraph