When (Not) To Create A GraphQL Server

Rate this content
Bookmark

Every (frontend) developer is asking for a GraphQL API but often lacks the backend knowledge to create a performant server. There are many different flavors in creating the perfect GraphQL server, ranging from schema-first to code-first or even auto generated solutions. Let me show you the pros and cons of each solution so, after this talk, you'll know which solution would best fit your team.

8 min
09 Dec, 2021

Video Summary and Transcription

Building a scalable GraphQL server can be challenging. There are multiple reasons to build a GraphQL server, such as optimizing client requests and wrapping microservices. The SDL first GraphQL server allows you to write your schema separately from your resolvers, giving you full control. You can create a GraphQL API automatically from a schema, simplifying the process and eliminating the need for manual deployments. The schema can link data sources and third-party APIs, allowing for easy integration and customization.

Available in Español

1. Introduction to GraphQL Server

Short description:

Building a scalable GraphQL server can be challenging. There are multiple reasons to build a GraphQL server, such as optimizing client requests and wrapping microservices. There are different ways and patterns to create a GraphQL server, with the code-first approach being one of them.

Hey, everyone. My name is Roy and welcome to my talk, when not to create a GraphQL server. So who's this for? So it's very easy to build a GraphQL server, but it's really hard to make a GraphQL server that's scalable. So scalability will sometime kick you in the ass whenever you start writing a GraphQL server. So there are many libraries to build a GraphQL API and there are many different patterns to build a GraphQL API and we'll be exploring them all in this talk. We'll also be showing a way to create a GraphQL server automatically based on your existing back ends.

Little bit about myself. So my name is Roy. I'm a software engineer and public speaker. You can find me on Twitter with the handle, AdGetHackTeam and I wrote a lot of books about react and also GraphQL, the latest one is full stack GraphQL.

So before diving into how to build a GraphQL server, also let's have a look at why you want to build a GraphQL server in the beginning. So there are many reasons to build a GraphQL server and I think the very first one is through optimizing client request. So you have a client, it's consuming an API, but somehow your REST API or maybe different sort of API calls aren't that efficient. You want to have that efficiency and you also want to give some autonomy to the user. So GraphQL isn't only there to solve the M plus one problem where you might be over and under fetching too many GraphQL, too many REST end points, but instead GraphQL also gives autonomy to the user. So as a client, you've more control about the types, about the data that's flowing into your application. Optimizing client request isn't the only way or the only reason to create a GraphQL server. It could also be to wrap your microservices, so GraphQL is perfectly fine to bring together multiple microservices that can be GraphQL microservices, but also microservices built with something else. And it's also very good in wrapping legacy services with that GraphQL data lane. So there are multiple reasons to build a GraphQL server and these are two perfectly fine reasons to build one.

There are multiple ways to create a GraphQL server. So as I mentioned in the beginning, there is no correct way. There are many different ways and many different patterns and libraries to help you with it. So the first pattern we have here is a code first GraphQL server. So with a code first GraphQL server, you always start by writing resolvers. And these resolvers, they usually use classes or if you're using TypeScript, they probably use a TypeScript type system in order to define the results returns of these resolvers. And based on that results, a code first GraphQL server will generate a schema based on the resolvers. And then, of course, create a server for you. And it is one major downside there's an upside, you can do everything programmatically. But the downside, it's very hard to have separation of concern.

2. Creating a GraphQL API from a Schema

Short description:

The SDL first GraphQL server allows you to write your schema separately from your resolvers, giving you full control. However, this approach requires writing both resolvers and schemas, which can be time-consuming. Alternatively, you can create a GraphQL API automatically from a schema using step 10. This simplifies the process by generating resolvers in the cloud and connecting to various data sources, such as databases, REST APIs, or other GraphQL APIs. The resulting serverless GraphQL API handles scalability and performance, eliminating the need for manual deployments.

So literally, there's no separation of concern, because your schema gets created from a resolvers, and resolvers is the only code you're out. So you don't have any control about how the schema actually will look like in the end. So there was a solution for this, which is SDL first GraphQL server, also known as schema first GraphQL server.

So the idea here is that you write your schema, you can either do it in GraphQL SDL, hence the SDL first server, or you can do it with some external libraries. So there is the GraphQL JS library, which lets you write schema first GraphQL servers, but not using SDL, but using their own methods instead. But the idea is simple, you create your schema, you create your resolvers based on this schema, and this will create a server for you. So you have a very nice separation of concern, you have your schemas separated from a resolvers, and you are in full control of everything. But this, of course, has the major downsides and writing both resolvers and schemas is a lot of work.

So luckily, there's a solution. You can also create your GraphQL API automatically from a schema. And this you can be done with step 10, an example. So let's have a look about how this works. So creating a graph API with step 10 is very straightforward. You just write your schema. You can define all the types that you have. And these types will be linked to a data source they're coming from. And this can be any back end as we'll see later on. And our CLI will then generate resolvers in the server automatically for you, right in the cloud. So it will be deployed serverless, you don't have to worry about any deployments or about scalability of your resolvers. So steps and you can connect any data source.

So it can be databases, either SQL or Postgres. It can be REST APIs, it can be GraphQL APIs, you can literally connect any other data source. And then based on this data source, a fully performance serverless GraphQL API will be created for you. So no longer do we have to worry about scalability, about performance, about deployments, it will all be done for you. And this uses just GraphQL schema design language. And here are two examples for MySQL Postgres on the left and REST on the right. As you can see, it's using custom directives in order to link to data source. So right from in your schema using just GraphQL schema design language, you can create connections with any back end or any data source. And next to Postgres than REST, you can also use any other GraphQL API as your data source, as we'll see in this example. So what we have here is a schema.

3. Linking Data Sources and Third-Party APIs

Short description:

The schema will link the data sources for you and create a server in the cloud. Additional types like language can be added and linked to countries. A custom directive will be used to connect country language information. The latest schema version is always available on steps. Third-party and SaaS APIs can also be integrated using Step Send GraphQL Studio, an interactive ID in your browser. Create your own API by selecting schemas, running queries, and deploying on the cloud.

And in the schema, we'll be linking the data sources for you. As you can see, you can just run step send start. It will automatically create a server for us. So it's deploying to step send, which will mean it'll be deployed in the cloud and then an endpoint becomes available.

And then, of course, we can also add more types here like a language. And this language can be linked to the country. So a country can have a language. For this, we'll be using another custom directive and will link it to an existing query. So to get country language will also be arrested the IPL that connects the information for a country. And then as soon as we start saving this, it'll automatically be redeployed again. So you always have the latest version of your schema on steps.

And you can also do this with third party or SaaS APIs. So quite recently, we launched a new product called Step Send GraphQL Studio. So you can find it on graphql.stepsend.com. And this is an interactive ID right in your browser where you can play with third party APIs, such as step.to GitHub or Twitter, you can link them all together, either separately, or by finding some combinations we made for you. And directly from the browser, you can create your own API by selecting some schemas, running some queries, and also deployed on the cloud. So if you would scan this QR code or to go to graphql.stepsend.com, you can already find this right there for you.

So that's mostly it. We had a short time to cover everything. My name is Roy. You can find me on Twitter using Edgate Hack Team. You can find Stepsend on stepsend.com. And you can also find our product GraphQL Studio on graphql.stepsend.com.

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

GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
Remix is a web framework that gives you the simple mental model of a Multi-Page App (MPA) but the power and capabilities of a Single-Page App (SPA). One of the big challenges of SPAs is network management resulting in a great deal of indirection and buggy code. This is especially noticeable in application state which Remix completely eliminates, but it's also an issue in individual components that communicate with a single-purpose backend endpoint (like a combobox search for example).
In this talk, Kent will demonstrate how Remix enables you to build complex UI components that are connected to a backend in the simplest and most powerful way you've ever seen. Leaving you time to chill with your family or whatever else you do for fun.
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
React Summit Remote Edition 2021React Summit Remote Edition 2021
43 min
RedwoodJS: The Full-Stack React App Framework of Your Dreams
Top Content
Tired of rebuilding your React-based web framework from scratch for every new project? You're in luck! RedwoodJS is a full-stack web application framework (think Rails but for JS/TS devs) based on React, Apollo GraphQL, and Prisma 2. We do the heavy integration work so you don't have to. We also beautifully integrate Jest and Storybook, and offer built-in solutions for declarative data fetching, authentication, pre-rendering, logging, a11y, and tons more. Deploy to Netlify, Vercel, or go oldschool on AWS or bare metal. In this talk you'll learn about the RedwoodJS architecture, see core features in action, and walk away with a sense of wonder and awe in your heart.

Workshops on related topic

GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
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
JSNation 2023JSNation 2023
174 min
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
Featured WorkshopFree
This SvelteKit workshop explores the integration of 3rd party services, such as Storyblok, in a SvelteKit project. Participants will learn how to create a SvelteKit project, leverage Svelte components, and connect to external APIs. The workshop covers important concepts including SSR, CSR, static site generation, and deploying the application using adapters. By the end of the workshop, attendees will have a solid understanding of building SvelteKit applications with API integrations and be prepared for deployment.
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
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 Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
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.
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
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.
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
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