Skip to Content

wundergraph.operations.ts Overview

Published: October 19, 2021

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 Operation
const 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 Operation
const 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 Operation
const 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 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,
}
}),
}
});
export default operations;

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