Schemas Everywhere: Understanding GraphQL, Databases & Prisma

Rate this content
Bookmark

In a world run by data, we as developers have turned to schemas to help describe and organize that data. But what happens when you have a ton of schemas to keep track of? In this talk you will learn the role of all the different schemas in a GraphQL API.

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

FAQ

A database schema defines the structure of data stored in a database, while a GraphQL schema specifies the structure of data that an API exposes, which may include additional or restricted data not present in the database for security reasons.

Developers are responsible for managing data by understanding its structure and purpose, transforming it, and ensuring it interacts correctly with different parts of an application.

Data modeling is challenging because it involves understanding complex data flows, adapting models to evolving requirements, and often dealing with data that was not originally modeled by the developer.

A schema is a structured representation of a data model that helps in clearly defining how data is organized within a database or system. It acts as a source of truth for the data's structure.

The Prisma schema language simplifies modeling database schemas by using a syntax similar to GraphQL, making it more accessible for developers and aiding in generating type-safe client interactions with the database.

Developers can achieve type safety by using tools like Prisma for database-API interactions and GraphQL Code Generator for front-end types, ensuring consistency across the different layers of the application.

Multiple schemas can lead to confusion about the source of truth and the specific role of each schema, especially when they describe data differently or overlap in their data definitions.

Prisma bridges the gap between the database and API by generating migrations to update the database schema and creating mappings that ensure type-safe interactions between the database and the API.

Sabin Adams
Sabin Adams
9 min
08 Dec, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Welcome to the talk! As developers, we manage and understand the data that the world runs on. Each individual schema in your infrastructure defines your data in the context of its own domain. The Prisma schema is used to generate migrations and create a mapping between the database and API, enabling type-safe interactions. The GraphQL schema allows clients to safely query the database via the API. By using Prisma and GraphQL Code Generator, you can achieve an end-to-end type-safe environment.

1. Introduction to Data Management and Modeling

Short description:

Welcome to the talk! As developers, we manage and understand the data that the world runs on. Data modeling can be challenging, especially when dealing with data from various sources. Schemas provide a way to represent data models, but the proliferation of schemas has led to the question of the source of truth.

Welcome, everybody. Thank you so much for joining me for this talk. I am very excited to be giving this in a lightning talk format. I've given the same talk before in a full length format and I've condensed it down into just the necessary pieces. So, looking forward to see how this will go. If you have any questions about the talk after I've given it, feel free to shoot me a message on Twitter and I'll be happy to answer any questions you might have.

But before we get into the meat and bones of this talk, let's talk about the bigger picture concept that the world itself runs on data, whether it's your cell phone, whether you're using Facebook, or maybe your refrigerator or whatever you have that's connected to the internet, everything runs on data and as developers, it's actually our job to manage this. So it's a heavy load to put on our shoulders, but that's what we signed up for when we became developers is to actually take this data, do something with it, and spit it out in a format that other pieces of software can use.

So the TLDR of all this is that in order to manage a set of data, you have to have some sort of knowledge about its structure and its purpose. You have to know why you're dealing with your data and why you're doing what you're doing in your application's code to your data. So to revise this original statement, not only is it our job to manage this data that the world runs on, but it's also our job to at least to some degree understand it.

And this is hard because in general, data is hard to model. As technical people, we have a lot going on. We're doing a lot of technical things. We're developing applications. We have a lot of this knowledge to keep in our heads. There's not a whole lot of room to understand the whole data domain of whatever industry you're working in at the time. So a couple of other reasons though why your data is hard to model, as your data flows through different areas of your application, you have to know how to interact with this data. So it needs to be modeled in a way that with different pieces of your application. Your data model may change as your application evolves. So as new requirements come up in your industry, you may have to evolve your model a bit and doing that in a way that's safe for your application can be difficult at times. Another one, and this is a big one, is that your data may not have been modeled by you. And I would also revise this to say that your data probably wasn't modeled by you. You're probably consuming data from someone else and using it within your own application.

So for all of these reasons, we as developers came up with this idea of schemas, which is a way to clearly and concisely represent your data model. But there's still a problem, even with schemas. Schemas are now everywhere, so we've solved this problem of being able to model out our data in a way that makes sense. But now that we found a good solution, we're using it everywhere, and the original intent for the schema is now lost. So what the schema is supposed to be is a source of truth for what your data looks like. But as you start adding different schemas everywhere, it begs the question, now what is the source of truth? So this causes the problem that you now have multiple perceived sources of truth, and each schema may describe your data a little bit differently, which is what probably causes the question, what is the source of truth? And also, each schema has a different role.

2. Data Management and Schema Definition

Short description:

Each individual schema in your infrastructure defines your data in the context of its own domain. We'll look at the database schema, which is written in a data description language. Prisma has its own language, the Prisma schema language, that allows for easier modeling of the database schema. The GraphQL schema is different from the database schema as it defines what the API exposes, not the data itself.

And if the data, if the schemas look similar, it's kind of hard to determine what the role is of each individual schema. So to put this shortly, each individual schema in your infrastructure defines your data in the context of its own domain. So whether it's your database, your API, or something else, the schema is describing the data for that individual piece of your application.

So we're going to look at a stack that has a database graph QL API and Prisma thrown in there as well. So that we can look at the individual schemas and how they relate.

So to start off, we'll look at the database schema, which you see on the right is the code that you would need to write to create a basic database schema. This is written in DDL or a data description language. And it's typically a database specific language here. So this is a little bit complicated. These languages tend to be a little bit harder to learn than something like, um, something like JavaScript or something a little bit easier to look at on the eyes.

And for this reason, we at Prisma created our own language here called the Prisma schema language. And this allows you to model out your database schema within a Prisma schema file. The only difference is that it's written in this language that's a little bit more like graph QL. That's a little bit easier to read. This is important here because now as new developers come into your application, or maybe someone who's non-technical come in, they can actually look at this model and sort of see what's going on. This is also important because it's in a simple enough model that we can actually use this within Prisma to generate a type safe Prisma client that interacts with your database in a way that's safe and ensures that the data you're accessing is actually available.

And then finally we have the graph QL schema here. So this looks similar to the Prisma schema. However, there is a big difference. This is where a lot of people end up getting confused with schemas. The graph QL schema tends to look very similar to your database schema because it's exposing data from your database. But the problem here is that it should probably not look exactly like your database schema. This isn't defining your data here. This is defining what your API exposes. So, for example to elaborate on this, you may be exposing data from your graph QL API that's not even in your database. Or you may be not exposing certain fields from your database for security reasons. And because of these things your graph QL schema is a completely separate thing from your database schema. It may feel a little bit like code duplication as you're actually writing them. But if you understand this difference between the two different schemas here, it starts to make a little bit more sense. So, just to recap, we've got our database schema which is the schema running on your database server that defines your data shape.

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