Code Organization
The Grafbase SDK has a define
export which allows you to define types, interfaces, unions, queries, mutations, inputs, enums and references outside of the context of a graph.Standalone()
instance.
This allows you to split your definitions into multiple files without requiring the original graph.Standalone()
instance to be available (or to share the same definitions between schemas).
The scalar
export allows access to scalars like string()
which would normally be on the graph.Standalone()
instance.
// resolvers.ts
import { define, scalar } from '@grafbase/sdk'
const hello = define.query('hello', {
args: { name: scalar.string().optional() },
returns: scalar.string(),
resolver: 'hello',
})
const goodbye = define.query('goodbye', {
args: { name: scalar.string().optional() },
returns: scalar.string(),
resolver: 'goodbye',
})
export const resolvers = [hello, goodbye]
Standalone graphs have an .add
function that accepts any number of arguments of types, interfaces, unions, queries, mutations, inputs or enums defined using define
:
// resolvers.ts
import { define, scalar } from '@grafbase/sdk'
const greeting = define.type('Greeting', {
adressedTo: scalar.string(),
formula: scalar.string(),
})
const hello = define.query('hello', {
args: { name: scalar.string().optional() },
returns: define.ref(greeting),
resolver: 'hello',
})
const goodbye = define.query('goodbye', {
args: { name: scalar.string().optional() },
returns: scalar.string(),
resolver: 'goodbye',
})
// NOTE: not only resolvers, but types used by resolvers must be added
// explicitly. Our schema will be missing the `Greeting` type if we do not add it
// to this array.
export const resolvers = [hello, goodbye, greeting]
// grafbase.config.ts
import { config, graph } from '@grafbase/sdk'
import { enums } from './enums'
import { resolvers } from './resolvers'
const g = graph.Standalone()
g.add(...enums, ...resolvers)
export default config({
graph: g,
})