GraphQL Nexus - code-first GraphQL server development
Tags: graphql, webdev, node, typescript
Published: 2020-08-25 10:43:40



GraphQL schema is a set of rules describing the functionality available to the client, including specification of operations (queries and mutations) that can be executed to execute against your data graph. When building a GraphQL service, there is a choice that needs to be made whether you want to follow the code-first or schema-first path: 

- Schema-first - which prioritizes process of designing the schema which puts schema as your source of truth and forces your code to follow the definitions stored in your schema,

- Code-first (resolver-first) - is an approach where the GraphQL schema is implemented programmatically.

In either case, we will end up with a fully functional GraphQL service, but this choice will influence your project in terms of the amount of work you will need to put to introduce some features (but it's a topic that deserves to be covered in a separate post).


## Code-first framework for GraphQL Server development

The rapid growth of GraphQL's popularity generated the natural need for different tools, both schema-first and code-first oriented, facilitating GraphQL working experience. One of the tools representing the code-first approach is [GraphQL Nexus framerwork](https://nexus.js.org/).

GraphQL Nexus is a GraphQL framework for building your GraphQL Server, where the schema is defined and implemented programmatically. GraphQL Nexus relies on a Node.js and TypeScript thanks to which it can provide features such as:

- **Type-Safety** -  type-definitions are being generated as you proceed with the development process & inferred in your code, providing you with auto-completion and error catching,

- **Compatibility with GraphQL Ecosystem** - GraphQL Nexus relies heavily on graphql-js and works well with its existing types when constructing the schema which makes the auto-generated schema compatible with most popular tools like Apollo Server etc.,
 
- **Data-Agnostic** - GraphQL Nexus is a declarative syntax layered on the top of the graphql-js library which basically means that you can achieve with it all that you can do with graphql-js or apollo-tools.

Having figured out all the types you need for your schema all you need to do is simply use `makeSchema` function to create the schema instance that would be used as the foundation for your GraphQL server.

```tsx
const schema = makeSchema({
  // The programmatically defined building blocks of your GraphQL schema
  types: [User, Query, Mutation],

  // Specify where the generated TS typings and SDL should be located
  outputs: {
    typegen: __dirname + '/generated/typings.ts',
    schema: __dirname + '/generated/schema.graphql',
  },

  // All input arguments and return types are non-null by default
  nonNullDefaults: {
    input: true,
    output: true,
  },
})

// ... feed the `schema` into your GraphQL server (e.g. apollo-server or graphql-yoga)
```

## Getting started

As previously mentioned GraphQL Nexus relies heavily on `graphql-js` and it's also required for the installation:

```
npm install nexus
npm install graphql # required as a peer dependency
```
The best way to begin with GraphQL Nexus is of course the [official documentation](https://nexus.js.org/docs/getting-started). After familiarizing with it the next step could be playing around with their [official examples](https://github.com/prisma/nexus/tree/develop/examples) and the [online Playground](https://nexus.js.org/playground). Have fun!

---

## Speed up your GraphQL schema development

*GraphQL Editor* is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster. **[Try GraphQL Editor for free!](https://graphqleditor.com)**


[![GraphQL Editor](https://dev-to-uploads.s3.amazonaws.com/i/a7qwlawx8v594fs2bq0a.gif)](https://graphqleditor.com/)