Let's Talk GraphQL With Your Services

Rate this content
Bookmark

GraphQL is a query language for APIs, but what if your API doesn't support GraphQL? There's a lot of information on how you can use GraphQL to make your APIs more readable or even more content on using GraphQL as a data layer for your existing APIs. This all makes perfect sense, but what if you actually want to know how to transform existing services to GraphQL? In this talk I'll demonstrate how you can use your existing services to create GraphQL schemas using Abstract Syntax Trees and TypeScript.

This is not a talk to show how to wrap services with GraphQL, in this talk I'll show data models to use an AST to transform existing services to GraphQL. Also, thinking patterns and best class approaches will be shown.

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

FAQ

Roy's talk at GraphQL Galaxy is about integrating GraphQL with existing services and legacy code.

Roy currently works for a renewable energy company called Vanderbrand.

Roy has worked for various companies including the city of Amsterdam, where he contributed to open source projects.

You can find Roy on Twitter, YouTube, and GitHub.

Roy's most recent book is about Fullstack GraphQL, covering how to build GraphQL servers and clients with React, Node.js, and TypeScript.

Common problems include multiple requests for different pieces of data, which can lead to inefficiencies and performance issues, especially for mobile internet users.

GraphQL Mesh is a library that allows you to use GraphQL on top of existing services without custom code. It was developed by Uri Goldstein.

Roy suggests creating a data layer or data access layer that uses GraphQL to interact with legacy code without modifying it.

The two main approaches are code-first or resolver-first, and schema-first. The choice depends on whether you start with existing code or a predefined schema.

Common challenges include handling authentication and caching to ensure data consistency and recency.

Roy Derks
Roy Derks
28 min
02 Jul, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The video explores using GraphQL with existing services and legacy code, focusing on an e-commerce platform that uses JavaScript, React, a REST API, and a database. It discusses the challenges of using REST and the benefits of using GraphQL, such as API optimization and reduced data transmission. The speaker introduces GraphQL Mesh as a tool for integrating GraphQL with legacy code without modification. They also discuss creating a custom data layer for better control and flexibility. The importance of authentication and caching in legacy systems is highlighted, along with the choice between code-first and schema-first approaches. The video emphasizes using the source of truth for data and mapping endpoints to a schema manually or with libraries like GraphQL JS. Connecting disparate systems and improving developer experience are key themes.

1. Introduction to GraphQL and Existing Services

Short description:

Hi, welcome everyone. Today, I will present use cases for GraphQL with existing services and legacy code. We'll explore how to talk GraphQL with existing services, specifically in the context of an e-commerce platform. The platform uses JavaScript, React, a REST API, and a database. We'll discuss the challenges of using REST and the benefits of using GraphQL to connect all the components together.

Hi, welcome everyone. So this is Roy at GraphQL Galaxy with Let's Talk GraphQL with your services. So what does this talk about? So during today, I will present you some use cases for GraphQL also with existing services. So let's have a look at legacy codes. It might look like this, right? Or is this actually PASTA? I'm not sure if you know, but what was the last time you tried talking with PASTA? For me it was never. So let's assume we're going to be looking at existing services as legacy code.

So a little bit about myself. So my name is Roy. You can find me on Twitter with Github team. Currently, I work for a renewable energy company called Vanderbrand, previously I worked for a lot of different companies, including the city of Amsterdam, most lately, where we created open source projects for the city. And also, you can find me online on YouTube either with conference videos from previous times or some of my books about React or React Native. And in the topic of today, my most current book about Fullstack GraphQL. So this is a book about how you can build GraphQL servers and clients with React, Node.js and TypeScript. So if you have any questions afterwards, make sure to get it.

So for today, we're going to be looking at how you can talk GraphQL with existing services. So suppose you start working on this great project, you would just get hired at a new job, or maybe a new project starts at your current company. And this one is called the e-commerce platform of the future. Probably you've been approached by recruiters that had something similar like this, like hey, we're going to build the e-commerce platform of the future, we want you to be part of it. Well, let's assume you fell into this trick and you started working at a company that is going to build this platform. So this might be the stack, right? So we have, on the left, you can see an example UI and on the right, you can see technologies. So it's using JavaScript and React, which all seems fine. And then it's using a REST API with a database and a database as a back-end. So, maybe after seeing this, you're thinking it's an e-commerce platform, but it might not be the e-commerce platform of the future. If you think by now, that's okay. If you think it's still the e-commerce platform of the future, that's also okay, because we might be able to find another way to use this platform and make it future-proof. Or you might also think the REST API and the database together are like the pretty bowl of spaghetti we saw before, which is the legacy code that we're going to be interacting with. So still, we have some legacy code back end, we have JavaScript and React, and we somehow want to connect all this together with GraphQL, because if it's using REST, it's going to lead to several problems, right? Because this might be the REST API, you have three different requests, or maybe even more, and all these requests will be called by your UI. So every single bits and pieces of our UI is using a separate REST API call, which in the end is going to be troublesome for people that are using mobile internet. And these often are directly correlated to a database. So we have a REST API and we have database, and those are closely linked together.

2. Using GraphQL with Existing Services

Short description:

So every table in your database will have a separate API call that returns JSON. We want to prevent separate requests, parsing, and normalization in the UI. Let's find a way to use GraphQL with the backend without touching the legacy code. One option is to use existing libraries like GraphQL Mesh. Another approach is to create our own data layer, providing simplified access to legacy code using GraphQL. This helps with caching, monitoring, and prevents mistakes in legacy code changes.

So every table in your database will have a separate API call that will work like this in the UI. And all these API calls will also return some JSON, that might be a lot of JSON, and you're actually using small pieces of this JSON. So once you've seen this, you're probably thinking we should start using GraphQL for this, and that's exactly what we're going to be looking at today. So we don't want to do this, having all these separate requests that return a lot of JSON, it all needs to be going into our UI, that needs to be parsed, it needs to be normalized. That's something you absolutely want to prevent.

So this can be done more efficiently, like we already saw, and this is of course with GraphQL. But the service isn't using GraphQL already. So let's try and find a way to have this backend service interact using GraphQL with our frontend UI. And let's do this in a way that we don't have to touch the legacy code or the PASTA or Spaghetti or whatsoever that's living there on the back. So what we want to prevent is having to touch anything that has been built by previous developers.

So you have multiple options to do this. And I believe one of the best options is using existing libraries, because it's going to get rid of a lot of stuff. It's going to prevent you from making mistakes, it's going to prevent you from having to maintain another service. And for this, I would definitely advise you to use GraphQL Mesh. I believe Uri Goldstein is also a speaker today, or maybe he already presented. I'm not sure. At least Uri will be probably will be talking a bit about GraphQL Mesh and how you can use it. And if you want to stay away from making any custom code, it's definitely a way to go. But for today, we're going to be looking at a different approach. And this is by creating your own data layer or data access layer. And using this layer, you'll be able to use GraphQL for your backend services without having to change those services. And it's a similar to what GraphQL Mesh does, except we're going to be owning the entire data flow. We're going to be owning all the logic in there. And in case you're not familiar, a data access layer, or data layer is a layer of a computer program that provides simplified access to the data of that surface.

So what we'll be doing, we'll be building a data layer that provides simplified access using GraphQL over our legacy code. So this will help us do a lot of different things like caching, like monitoring, but also it's going to prevent us from making mistakes while making changes to legacy code that runs, but somehow nobody knows how it runs, which often is the case with legacy code, of course. So this is the question you might ask yourself, why would I be creating a layer on top of a surface that already exists, that runs perfectly fine? Why should we do that? Because is my API already a data layer? Because probably your legacy code already provides an API with an interface that's going to give you all the data that you need. But we're going to be creating a data layer on top of a data layer. You might feel like inception right now because somehow we're creating something on top of something, or maybe it's already inside something. So something to think about for the rest of this talk.

QnA

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