For those of you who haven’t checked it out, Nexus.js is a framework for composing a GraphQL API (types/queries/mutations) that is built to work nicely with Prisma. Nexus provides some nice features that I am enjoying:
- It provides utility functions for common CRUD operations that eliminates the need to manually write/generate queries, mutations, input types, resolvers, etc. It’s truly magical.
- It is built on TypeScript, so the code you do write is checked against your schema at compile time.
For the sake of comparison (and fun!) I decided to clone the Redwood Example Blog schema and expose it as a GraphQL API using Nexus.
Nexus allows you to define a query for posts with a single line of code. Here’s an example for the Post
type:
t.crud.posts({pagination: true, filtering: true, ordering: true})
This single line of code provisions all the necessary artifacts to provide rich queries for users, including:
- Pagination (first, last, skip arguments)
- Filtering (
Post
where input type) - Ordering (
Post
order by input type)
You can also define create, update and delete mutations easily:
t.crud.createOnePost()
Finally, it’s worth noting how robust these resolvers and input types are. For instance, that single line of code above made it possible for my to craft a mutation that created both a new Post
and a new Tag
at the same time:
mutation {
createOnePost(data: {
title: "Creating a Post and a Tag at the same time!"
slug: "creating-a-post-and-tag"
author: "Carter Rabasa"
body: "Lorem Ipsum"
tags: {
create: {
name: "A new Tag"
}
}
}){
id
tags{
id
name
}
}
}
I have gone ahead and deployed this to Heroku and you can check-out the GraphQL Playground here so folks can poke around and check-out (via the Docs) all the nice bits that Nexus provisions:
https://ancient-island-61569.herokuapp.com/
You can find the code for my deployed Nexus app at the link below.
https://github.com/crtr0/nexus-test
I’d love to hear from anyone else who’s checking out Nexus and hear what you think. I’d especially love to hear if anyone has gotten it to deploy to Netlify!