Easy Code-First GraphQL Node Server — Nexus
Use GraphQL Nexus with Prisma to create type-safe, scalable, maintainable GraphQL servers

Introduction
When asked to choose technologies for use in a backend server in 2021, 9 times out of 10 I’m recommending GraphQL Node with Nexus typegen.
Working on RESTful applications in Ruby on Rails (bad) and ExpressJS (a little better) most of my programming career, I encountered plenty of headaches creating proper scalable APIs. Overfetching, underfetching, documenting, multiple requests… the list goes on. Since switching to GraphQL for greenfield servers, we at Knit have reaped the innumerable benefits a single queryable server-defined endpoint provides.
What’s challenging about a GraphQL schema?
Hand writing a proper GraphQL schema takes a lot of work. Your schema should suit the underlying data models and cover sensible permutations of data requests (list, filter, authentication, etc.).
Schema definition files consist, primarily, of type definitions. You must define types using the GraphQL SDL (schema definition language) for objects (data models), queries (data reads), and mutations (data creates /updates/deletes). Query and mutation definitions include expected input params (e.g. page number, sort-by, update fields).
The schema file is simply a contract for the structure of your GraphQL server. Once you have these definitions specified, you add TypeScript resolver functions for queries and mutations to handle the logic of returning objects from your data source.
Nexus with Prisma
With Nexus, we generate GraphQL type definitions from code rather than writing a schema file first. We define both the schema.graphql file and resolvers with a single API in a TypeScript file.
Prisma is a TypeScript ORM (layer between your data persistence and business logic) with tons of useful features. In fact, Nexus is a supplemental project maintained by the folks at Prisma.
In the snippet below, a Prisma instance has been injected into the GraphQL context. It demonstrates how to create resolvers to support a wide array of useful requests for DataModel data (pagination, filtering, sorting, nested query).
A separate GenericInputTypes.ts file supplies generic types useful across data models, which can be modified to your liking.
Note: Pre-1.0 Nexus versions featured experimental CRUD auto-generation with generic types. The Nexus team expects to reintroduce these features in future releases.
Yes, it’s a lot of code, especially with the requirements of doing everything by hand in the current release of Nexus. But it doesn’t take much time, trust me!
Other Notes
- Use Apollo-Server-Express to easily configure your GraphQL server on top of an Express server with sensible defaults
- Nexus works great with GraphQL Shield for a permissions layer on top of your queries and mutations
Final Thoughts
Nexus is still in early stages and its documentation can be difficult to read through. However, the best part is you can always migrate away from this package without changing much of your code. All you have to do is keep the latest schema.graphql file that was generated, and extract the functional resolvers from Nexus-declared TypeScript files.
Check out our guide that walks you through how to setup a new Nexus & Prisma project.