Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "GraphQL Schemas" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

GraphQL APIs use schemas to definte the types, queries and mutations that can be queried on the API endpoint.

Get Unlimited Access Now

Transcript from the "GraphQL Schemas" Lesson

[00:00:01]
>> Scott Moss: So we're gonna hop right into schemas, I'm gonna show some examples and then we're gonna do some exercises. So more schemas, I know, we built some schemas yesterday with Mongoose. So it's like, why do we need more schemas? Yeah, we're gonna have more schemas sitting on top of our Mongoose schemas.

[00:00:16] And in our example today, they're almost gonna seem redundant, because they're gonna look almost identical. At first, they are, eventually we'll explore and expand on them, but they're gonna look almost identical. But yes, we do need more schemas and that's because GraphQL cannot work unless there's a schema.

[00:00:31] So you have a schema here on your server. You have a query that's coming in. It's gonna take that query. It's gonna match it against the schema, and if the types and shapes and field names don't match up, it's just going to break. It's not even going to go to your resolver, it's just not even going to try to get data, it's just going to be like, no.

[00:00:48] That is not a string, not going to let you do it, so we need those schemas. They solve the same problem as mongoose did yesterday, but on a higher level. Those Mongoose schemas were for interacting with our database. The GraphQL are schemas are interacted with the API. It's before the database.

[00:01:04] It's like, you send a request that hits graphql, checks the schemas, that goes to the resolvers, the resolvers use the Mongoose schemas to get the data. Then they respond back to graphql, graphql validates it again and then it responds back to the client. So it's little trippy how that works, but luckily you don't have to set a lot of that up.

[00:01:22] It's mostly free for us. So inside those schemas, what we'll be doing is defining types, queries, and mutations. You can think of types as just like the shapes. Think of the types as when we made, when we made models yesterday with Mongoose, when we made those object models, and here's a user model.

[00:01:41] Here is a song model, here is a Play this model, those are types inside of graphQL. Those are just like object shapes. Queries are functions we can use to query those types, like to effect those types. We do a query as like a git request. If you did a git request and rest, that's a query.

[00:01:58] Any time you wanna retrieve information from a GraphQL server, that's called a query. A mutation is like any time you want to mutate something on the server. So that includes creating something, deleting something, and updating something. So you can think of it as delete, put, and post, that's a mutation.

[00:02:16] So if we compare that to our recipe off yesterday, we had basically five different things we could do. We could get one and get all, those are queries and then we could put, post and delete. Those are mutations. Does that make sense? So that's what those what the queries and mutations and the types and then some like some helper utilities that we'll find out like inputs fragments interfaces, all that types of stuff you'll see likes pretty simple to build a graph a schema.

[00:02:45] Yeah, so the schemas like I said are needed to check client queries against. Can be written in quite a few ways, like there's so many ways to write a graphql schema. When graphql first came out everybody took like the JavaScript way where they use like the constructors and did it that way.

[00:03:01] Now they say it's more flexible to write it separately in like a string syntax or use in a graph QO file, which is what we're going to do. We're gonna write all of our schemas in a graph QO file and then combine them at the end versus writing them in JavaScript, which I think is way more overhead and a lot more confusing.

[00:03:19] So, we'll just write it as like, a pure string basically. You'll see can be generated dynamically. So yes, you can generate schemas dynamically. We will not be doing that today because it's really advanced. But yeah, you can totally generate schema dynamically depending on whatever your AST is. Depending on what your needs are.

[00:03:37] Given this object I want to generate a schema here and run it against this query that's coming in. So you could literally have a dynamic. API that changes depending on the user's roles, and authentication, and needs, or IP address. Whatever you want you can create different schemas that have dynamic qualities, like dynamic resolvers.

[00:03:57] Pretty much anything about GraphiQL can be dynamic, and it can be changed on-the-fly, which makes it extremely powerful. But also easy to mess up, so you gotta be careful with that. I ran into that problem before. Don't wanna be giving somebody else's data on a multi-tenant system.
>> Scott Moss: It is very strict, I said, it's like type script.

[00:04:17] It's very strict if your queries don't match up exactly the way the schema says it needs to be it just won't work. And they are composable, as you'll find out. Depending on what tools you use on a server you can compose your schemas together easily or difficult. We're gonna use something that allows it to work it very easily.