Nexus Documentation


Welcome!

Robust, composable type definition for GraphQL in TypeScript/JavaScript.

Note: The documentation is very new and may contain some gaps, please help us fill them in by opening issues or better yet, pull-requests when you think something could be explained better. The examples are a great place to look to better understand how the library can be used.

Nexus aims to combine the simplicity and ease of development of SDL development approaches like graphql-tools with the long-term maintainability of programmatic construction, as seen in graphene-python, graphql-ruby, or graphql-js.

Nexus builds upon the primitives of graphql-js, and attempts to take the simplicity of the SDL schema-first approach and pair it with the power of having the full language runtime at your disposal.

Nexus was designed with TypeScript/JavaScript intellisense in mind, and combines TypeScript generics, conditional types, and type merging to provide full auto-generated type coverage out of the box.

Check out the example projects to get some ideas of what this looks like in practice, or try it out in the playground to see what we mean!

Installation

1npm add nexus
2npm add graphql # required as a peer dependency

Example

As documented in the API reference GraphQL Nexus provides a consistent, scalable approach to defining GraphQL types in code.

1import {
2 objectType,
3 interfaceType,
4 queryType,
5 stringArg,
6 enumType,
7 intArg,
8 arg,
9 makeSchema,
10} from '@nexus/schema'
11
12const Node = interfaceType({
13 name: 'Node',
14 definition(t) {
15 t.id('id', { description: 'Unique identifier for the resource' })
16 t.resolveType(() => null)
17 },
18})
19
20const Account = objectType({
21 name: 'Account',
22 definition(t) {
23 t.implements(Node) // or t.implements("Node")
24 t.string('username')
25 t.string('email')
26 },
27})
28
29const StatusEnum = enumType({
30 name: 'StatusEnum',
31 members: ['ACTIVE', 'DISABLED'],
32})
33
34const Query = queryType({
35 definition(t) {
36 t.field('account', {
37 type: Account, // or "Account"
38 args: {
39 name: stringArg(),
40 status: arg({ type: 'StatusEnum' }),
41 },
42 })
43 t.list.field('accountsById', {
44 type: Account, // or "Account"
45 args: {
46 ids: intArg({ list: true }),
47 },
48 })
49 },
50})
51
52// Recursively traverses the value passed to types looking for
53// any valid Nexus or graphql-js objects to add to the schema,
54// so you can be pretty flexible with how you import types here.
55const schema = makeSchema({
56 types: [Account, Node, Query, StatusEnum],
57 // or types: { Account, Node, Query }
58 // or types: [Account, [Node], { Query }]
59})
Edit this page on Github