Log in to GraphQL Editor
GraphQL Nexus - code-first GraphQL server development
Tomek

Tomek Poniatowicz

8/25/2020

GraphQL Nexus - code-first GraphQL server development

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.

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.

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. After familiarizing with it the next step could be playing around with their official examples and the online Playground. Have fun!

Check out our other blogposts

GraphQL newbie tutorial - introduction
Robert Matyszewski
Robert Matyszewski
GraphQL newbie tutorial - introduction
5 min read
over 5 years ago
Apollo Kotlin - getting started with GraphQL on Android
Michał Tyszkiewicz
Michał Tyszkiewicz
Apollo Kotlin - getting started with GraphQL on Android
4 min read
about 2 years ago
Blog publish tools inside gatsby blog
Artur Czemiel
Artur Czemiel
Blog publish tools inside gatsby blog
3 min read
over 5 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor