Guides

Library Authors


Wrapping the API

If you are a library author building tools for GraphQL, it is recommended that you have nexus specified as a peer dependency rather than a direct dependency of your wrapping plugin, so duplicate copies of the library are not installed. The simplest way to build things for nexus is just to create higher-order functions which provide apis to abstract common functionality.

One example of this pattern would be for creating relay-style connections:

1export const UserConnectionTypes = connectionType('User')

Where connectionType is really just a wrapper creating two objectTypes:

1import { core, objectType } from '@nexus/schema'
2
3export function connectionType(name: core.AllOutputTypes) {
4 const Connection = objectType({
5 name: `${name}Connection`,
6 definition(t) {
7 t.field('edges', { type: `${name}Edge` })
8 },
9 })
10 const Edge = objectType({
11 name: `${name}Edge`,
12 definition(t) {
13 t.id('cursor', root => `${name}:${root.id}`)
14 t.field('node', { type: name })
15 },
16 })
17 const PageInfo = objectType({
18 name: `${name}PageInfo`,
19 definition(t) {
20 t.boolean('hasNextPage')
21 t.boolean('hasPreviousPage')
22 },
23 })
24 return { Connection, Edge, PageInfo }
25}

All internal apis are also exposed under a core namespace for you to use. Note that these may be subject to minor change, though we'll try to be considerate about this. At least there are type-safety guarantees (assuming you're using TypeScript).

Edit this page on Github