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 "Create a Schema Exercise" Lesson is part of the full, Introduction to GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to create a schema that passes several tests that check for fields, queries, and mutations.

Get Unlimited Access Now

Transcript from the "Create a Schema Exercise" Lesson

>> Scott Moss: So with what I just gave you, and then if you want, definitely have that cheat sheet open. What you're gonna do now is you're gonna be creating a GraphQL Schema based on the mongoose models that we already created. So if you checkout to the lesson-2 branch, basically what you're gonna do is you're gonna create a type for the product resource.

[00:00:21] So if we go to our schema, make sure you check out lesson two. If you go look at, make sure I checkout.
>> Scott Moss: If you go look at Source > Types > Product, there's a product.gql file in here. This is where you're gonna be doing your work. gql is short for GraphQL.

[00:00:45] You can also do .graphql if you want. I just did the shorthand, .gql. So you'll see some stuff in here called enums. If you've never heard of enums, if you look at that cheat sheet, it'll talk about them. But basically they're just like any other language. They're just hard coded values that cannot change.

[00:01:01] They're enums. These strings cannot change. And you can use them in places where strings will be. They're just hard coded strings. So these are here for you. But there's gonna be some tests that you need to run. So if you run npm run test -schema or yarn test -schema,

>> Scott Moss: It's gonna try to execute some tests here and they're all gonna fail. You'll get eight failed tests. And it's gonna be looking for some types. So you can see right here it's looking for a product type. It's looking for a new product input with the correct fields, an update product input, a product query, a products query, a new product mutation, an update product mutation, and a remove product mutation.

[00:01:48] It's looking for all of those and it's testing the fields on them, too, so it'll tell you what fields you're missing. So it's gonna be your job to come in here and write that inside the GraphQL file. Writing stuff inside a GraphQL file is no different from what I just did in the string.

[00:02:01] It's just in a GraphQL file and you just get really nice syntax highlighting. So you can still come in here and write a type, and it's exactly the same thing. You don't have to export anything. You don't have to import anything. You've just got to write your stuff and then the test will run.

[00:02:15] So once you get all that to run and pass, you should be good to go. But also, if you get stuck, like I said, there's that cheat sheet. But you can also just look at what I have in the coupon and the user. So as I go look at coupon.gql, you can see how I'm doing inputs in here, how I'm doing types in here, how I'm doing queries in here, and so forth.

>> Scott Moss: The only other thing that you're gonna have to do is when you make your queries and mutations, just prefix them with the extend keyword here. You can only have one type. We already have a type called Query and Mutation in the root schema. If you try to make it again, like this, it'll say type Query is already created.

[00:02:54] So this is saying I want to extend the type Query that's already there. So you have to do that. That allows us to spread our schemas across different files. If you don't do that, it will try to recreate the query type and it will break. So this allows us to extend the type that's already there.

[00:03:07] Apollo allows us to do this. A lot of other servers don't allow you to do this. So remember, get those tests to pass. When you write your queries and mutations, prefix them with extend. And you're just going to be writing some types and some input types, some queries, and some mutations.