Build Your GraphQL APIs Faster with Nexus Schema

Rate this content
Bookmark

Developing a real-world full-stack app often involves tedious threading of data across multiple layers of the stack. This is particularly undesirable during prototyping phases where the main goal may be just to demonstrate an idea or design. It’s also risky when going to production since data inconsistencies between the layers can lead to bugs.

Nexus Schema is a library for building code-first, type-safe GraphQL APIs and can greatly help with this velocity and type-safety dilemma. In this talk, we'll look at how to build GraphQL APIs faster and with the benefits of type safety using Nexus Schema.

This talk has been presented at GraphQL Galaxy 2020, check out the latest edition of this Tech Conference.

FAQ

The code-first approach involves defining the GraphQL schema directly within the codebase using programming languages like TypeScript, rather than using the schema definition language (SDL) approach. This method integrates schema creation and resolver implementation in one place, reducing the need for context switching and simplifying the development process.

Nexus Schema offers several benefits for GraphQL API development, including simplified project structure by allowing schema and resolvers to be defined in the same file, easy modularization without additional tooling, and automatic generation of schema definition language and TypeScript types that enhance the development experience and ensure type safety.

Prisma enhances GraphQL API development by providing type-safe database access, simplifying database operations through its ORM, and integrating seamlessly with tools like Nexus Schema. It supports various databases and helps in managing data models and migrations effectively, making database operations more reliable and developer-friendly.

Using the SDL-first approach can lead to challenges such as managing separate files for schema and resolvers, which increases complexity in large projects. It also necessitates additional tooling for code highlighting and modularization, and can lead to frequent context switching between different types of code during development.

Integrating Prisma with a Nexus Schema-powered GraphQL API involves initializing Prisma in your project, defining your data models in the Prisma schema, and using Prisma Client to perform database operations. This setup allows leveraging Prisma's type-safe database client within the Nexus Schema environment, facilitating smooth data management and API operations.

When using Nexus Schema for GraphQL API development, the generated artifacts include a schema definition language (SDL) file and TypeScript types. These artifacts are automatically created based on the code-first schema definitions, providing benefits such as enhanced editor support and improved type safety across the application.

Ryan Chenkie
Ryan Chenkie
25 min
02 Jul, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Building a GraphQL server typically involves using a schema definition language (SDL) and resolvers, but this can be cumbersome. Nexus Schema offers a code-first approach, allowing developers to define their GraphQL schema directly in code, which simplifies development and reduces context switching. The integration with Prisma enhances this by providing type-safe database access. This video explores how Nexus Schema and Prisma streamline building GraphQL APIs. It discusses the benefits of having everything in one place, eliminating the need for separate schema and resolver files. The video also covers how to set up a basic Nexus Schema-powered GraphQL API using Apollo Server, and how Prisma helps manage databases efficiently. Overall, this approach offers a better developer experience through simplified project structures and automatic generation of schema and TypeScript types.

1. Introduction to Code-First GraphQL APIs

Short description:

Hey, I'm Ryan. Today I want to talk about a different approach to building GraphQL servers. I'll discuss code-first GraphQL APIs with Nexus Schema. As a GraphQL developer advocate at Prisma, we make it easy to work with databases and provide type-safe database access. Find me on Twitter @ryanchenke.

Hey, I'm Ryan. And today I want to talk to you about a way to build GraphQL servers that looks a little bit different than maybe the typical approach that people might take. The approach of building a schema-first GraphQL API.

I'm going to talk about code-first GraphQL APIs, specifically with Nexus Schema. So my name is Ryan Chenke. I am a GraphQL developer advocate at Prisma. And at Prisma, we deal with databases. We make it really easy to get started with a database to have a really nice ORM around your database. We give you type-safe database access, which is very nice to work with, especially if you're working with TypeScript in Node.js. You can find me there on Twitter. I'm at ryanchenke on Twitter.

2. Building GraphQL APIs with Schema-First Approach

Short description:

When building a GraphQL API, we often start with a schema-first approach using the schema definition language. However, this approach can present challenges and complexities that may be better addressed with a different approach.

So let's talk about building GraphQL APIs. When we come to build a GraphQL API, and typically when we're first learning how to build a GraphQL API, we take the approach of what we call schema-first, and we use what's called the schema definition language approach to build a GraphQL server.

And so many of us have seen this if we've gone through the initial kind of getting started phases, or even if we've built full-out GraphQL APIs, where we start with a schema definition language, and we define our types and our input types and all that, and then we have a set of resolvers that map up to it. But this isn't the only way to do it. Even though it's easy to get going with, it does kind of present some challenges that we'll have to deal with along the way.

Most examples show how to do this schema definition language first approach, especially if you follow the Apollo tutorials, for example. You're going to see this STL-first way of building a GraphQL server, and it's easy to get started with, but ultimately it's going to lead to maybe some complexities that are better dealt with in a different approach. This is what it looks like. You would have your schema definition language. Here we've got a type of post and three fields. We've got the ID, title, and body. You may be familiar with this. This might feel quite comfortable. We've then got our root query type, where we are returning a list of posts.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced 2022React Advanced 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura