This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.

Check out a free preview of the full Introduction to GraphQL course:
The "GraphQL Schemas" Lesson is part of the full, Introduction to GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott contrasts the GraphQL and database schemas.

Get Unlimited Access Now

Transcript from the "GraphQL Schemas" Lesson

[00:00:00]
>> Scott Moss: Creating schemas. So you kinda just created one but now we're really about to create a schema, and get into the nitty gritty of things. Okay, so we already have a database schema. So, why don't we just use that? Like, why do we need another schema if we already have a database schema?

[00:00:17] It kind of doesn't really make any sense to have two different schemas. Well, basically graphical schema strictly defines what resources, how they're relayed, and how a client can consume them. So, when you compare that to a day to day schema, a day to day schema is like I have no idea, I don't even know what API I'm being used on, if I'm even being used on an API, this is just how you can save data in a database.

[00:00:39] Graphical schema, is more about how a client can retrieve data, how the data relates to each other.
>> Scott Moss: So like I said, DB Schema is keeping data consistent when entering our database, whereas the GraphQL schema is for defining what resources are available for querying, how they relate and then how you can query them.

[00:01:00] Both schemas can be the same or not. Basically what I'm saying is, if you already have some database models that you created, and you try to create a graph to a server on top of that. Yeah, database schemas and models are a really good starting point for your GraphQL schemas, as far as modeling your data and what they look like, you can totally do that they are really good starting point, but that doesn't mean it's going to be one for one exactly the same.

[00:01:24] And you'll find out why, because one GraphQL is super powerful and allows you to virtualize a lot of different things that you may or may not be able to do in your database or maybe you don't want to. And then two, there is probably some private stuff that you just don't want exposed to your GraphQL.

[00:01:37] So, you can update into what ever you want but they are a good starting point as far as modeling your GraphQL schemas, because you can use your day to day schemas.
>> Scott Moss: So the GraphQL schema sits in front of your database queries, and it validates the incoming request.

[00:01:52] So like if we have the schemas that we just created and a query comes in, that query gets validated against the schema that you created before your database even runs. Before your resolvers even run, before anything even runs. If that query is not valid against that schema, nothing will execute.

[00:02:11] That's what GraphQL does. So, that's why it's a little different than schema, because the validation is gonna happen before the resolvers even execute. If the resolvers are executing, that means that the query is valid. So, if you're getting an error before your resolver is even happening, it's because it's an invalid query and GraphQL will tell you where it's invalid, why it's invalid.

[00:02:30] And most of the time, it's because it's just not formatted for the schema that it was created.
>> Scott Moss: There are a bunch of GraphQL tools out there, that literally create like full GraphQL APIs, based off your database schemas. So literally, if you already have a database out there, a postgres database, a Mongo database that's got some schemas, there are tools out there that will take those and generate a full API.

[00:02:56] That's exactly like the schemas that you have. And there's tools that do the opposite. You give it a GraphQL schema and it will create database models for you. So, there's this tons of stuff out there that do that, because of how similar that they are. So, if you just wanna spin up something quick, at the end of the course I can show you what some of those tools are.

[00:03:15] But we're going to be doing everything manual in this course the hard way.