Server-Side GraphQL in Next.js

Scott Moss

Scott Moss

Superfilter AI
3 hours, 32 minutes CC
Server-Side GraphQL in Next.js

Course Description

Create GraphQL APIs from scratch with Next.js. Learn GraphQL fundamentals, including type definitions, type modifiers, relationships, and directives. Create schemas, build queries, and use mutations to implement standard CRUD operations. Configure your server and add authentication so your API is production-ready.

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: June 27, 2024

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 14 minutes
  • Introduction
    Scott introduces the course and provides an overview of what will be covered, including building a GraphQL API using Next.js, working with databases, and enabling authentication. Scott also explains the format of the course, which will involve a combination of explanations, live demos, and hands-on practice.
  • Course Setup
    Scott provides instructions on how to set up the development environment for the course. He explains how to fork the repository, install the necessary dependencies using npm, and create a Turso account for the hosted SQLite database. He also guides students through creating a .env file to store secret variables and pushing the database schema to the Turso database.

GraphQL Fundamentals

Section Duration: 1 hour, 39 minutes
  • What is GraphQL
    Scott introduces GraphQL and explains its two main components: the query language and the server component. He discusses how GraphQL allows clients to query data sources and how the server component fulfills those queries. Scott also highlights the benefits of using GraphQL, such as tying into multiple data sources and organizing APIs in a standardized way.
  • Schema Type Definition
    Scott introduces the concept of the schema definition language (SDL) in GraphQL. He explains that SDL is used to define the structure and types of a GraphQL schema, similar to how a database schema defines the structure of a database. Scott demonstrates how to create a simple GraphQL schema using SDL and shows how to explore and test the schema using Apollo Studio.
  • Interface Types
    Scott explains that an interface is like an abstract type that can be implemented by other object types. He demonstrates how to define an interface in the schema and how to implement it in different object types. He also shows how to query for common fields shared by objects that implement the interface, as well as specific fields for each object type.
  • Union Types
    Scott explains the concept of unions in GraphQL and provides an example of a search result where different types of objects can be returned. Scott demonstrates how to define a union type and incorporate it into the query object. He also discusses the syntax for querying fields on union types and how to determine the type of a union object using the "__typename" field.
  • Schema Relationships
    Scott discusses relationships in GraphQL and demonstrates how to define a one-to-one relationship between a profile and a person, as well as a cyclic relationship where a person has a profile and a profile has a person. Scott also mentions the potential issues with cyclic relationships and the need for additional tools or plugins to prevent abuse or performance problems.
  • Directives & Enums
    Scott explains that directives are like middleware for fields or entities in a GraphQL schema. He demonstrates how to use the built-in "deprecated" directive to mark a field as deprecated and show a deprecation message in the GraphQL Explorer. He also mentions that custom directives can be created for various purposes, such as authentication.
  • Queries & Mutations
    Scott discusses the difference between queries and mutations, with queries being used to retrieve data and mutations being used to modify or create data. He also demonstrates how to pass arguments to queries and mutations, and how to use input types for more complex arguments. Scott also briefly touches on custom directives, walking through an example of how to create and use a directive called "uppercase" to modify the behavior of a field.
  • Resolvers
    Scott explains that resolvers are functions that generate responses for GraphQL queries. He demonstrates how to create resolvers for different types and fields in the schema, and explains the arguments that resolvers receive, such as the parent object, arguments, context, and info. Scott also shows how resolvers can be used to override the default resolution of fields and return custom data.
  • Resolving Enums & Unions
    Scott discusses how to resolve enums, which involves mapping the enum values to different values if needed. Then, he moves on to resolving unions, which requires determining the type of the object being resolved based on its properties. Scott also provides examples and code snippets to demonstrate the process of resolving enums and unions.
  • Resolving Nested Objects
    Scott demonstrates how to create a resolver for a nested object, such as a person's pets, and explains the importance of only querying the database for the nested object when it is requested in the GraphQL query. Scott also clarifies the concept of the parent object in resolvers and mentions that the order of resolvers within an object does not matter, as long as the names match.

GraphQL Server Authentication

Section Duration: 41 minutes
  • Configuring the API Server
    Scott guides students through creating schema and resolver files and configuring the server in the route.ts file. He explains the purpose of each step and provides code examples for students to follow along.
  • Authentication Schema
    Scott discusses the necessary components for authentication, such as the user type, query to get the user, and input for signing in. He mentions the importance of not including sensitive information, like passwords, in the GraphQL schema. He also provides examples of how to define the user type, auth input, and mutations for signing in.
  • Authentication Resolver
    Scott walks through creating resolvers and explains the purpose of the imports and the different utility functions used in the resolvers. He also demonstrates creating a resolver for the "me" query and the signin and signup mutations.
  • Testing the Authentication Routes
    Scott explains how to configure the global context to retrieve the current user if an authorization header is set. He demonstrates how to add a context property to the Apollo server configuration and writes a function that retrieves the user from the authorization header.

CRUD Operations

Section Duration: 51 minutes
  • Creating Issues
    Scott guides students through the process of creating a "create issue" mutation in GraphQL. He defines the issue type, creates an input type for creating an issue, and implements the mutation resolver.
  • Retrieving & Filtering Issues
    Scott explains how to create a GraphQL query for retrieving issues and their associated users. He demonstrates how to add an "issues" field to the User type in the schema, create a resolver for the "issues" field, and implement the necessary database queries and filters to fetch the issues.
  • Resolvers Q&A
    Scott answers a student's question regarding how to pass additional data to the context object in GraphQL resolvers. He also discusses the concept of the parent argument in resolver functions and explains how to determine the parent based on the query structure.
  • Editing & Deleting Issues
    Scott discusses the input arguments required for editing an issue, such as the issue ID and optional fields like name, content, and status. He also demonstrates how to create the resolver for the edit issue mutation, which includes authentication checks and updating the issue in the database. The delete functionality is left as an extra credit task for both the server-side and client-side courses.

Wrapping Up

Section Duration: 6 minutes
  • Wrapping Up
    Scott wraps up the course by providing recommendations for using GraphQL going forward. He suggests figuring out where to install GraphQL, such as on the edge or as an API gateway, and choosing a framework like Apollo or GraphQL Yoga.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now