Introduction to GraphQL

Introduction to GraphQL Schema Definition Language


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 "Schema Definition Language" 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 explains why a Schema Definition Language, or SDL, is the best way to create schemas.

Get Unlimited Access Now

Transcript from the "Schema Definition Language" Lesson

>> Scott Moss: So now we're gonna talk about creating schemas with SDL, and we'll talk about what that is. But there's basically many ways to create GraphQL schemas, SDL is the best. So what is SDL? It's basically short for Schema Definition Language. So if we go back to our code, you're already doing that.

[00:00:17] This string right here that you were writing in, that's SDL. That's the Schema Definition Language. You are literally creating a schema inside of this string right here. And this is the preferred way to write a schema. And I'm not saying preferred as in this is what I prefer, this is what the community prefers.

[00:00:32] This is what the documentation uses now. This was about a year and a half ago. The way you would have written schemas in GraphQL was using objects. So you would use GraphQL.js which still has these objects. And you would use objects and functions to create schemas which offer some benefits as in they're pragmatic and you can do execution and stuff like that.

[00:00:55] But they're really hard to look at creating schemas with functions versus just writing them purely as this. So this is the Schema Definition Language. And right now we're writing as a string but we'll be writing in the file later.
>> Speaker 2: So those types are SDL types?
>> Scott Moss: Yes.

>> Speaker 2: String and ints and so forth.
>> Scott Moss: Yep, this entire string right here is a valid GraphQL Schema. And using the words like type and schema in query, these are constants or reserve names in the Schema Definition Language.
>> Speaker 2: Like name String, so the string is an SDL type?

>> Scott Moss: This type right here?
>> Speaker 2: GraphQL specific.
>> Scott Moss: Yeah, we'll get to these types, yes. Yeah, these are what we call scholar types, yeah. So we'll get to the scholar types in a minute, good question.
>> Scott Moss: So like I said, yeah, instead of using functions, we use the string syntax cuz it's just way better.

[00:01:54] And the sweet thing about using the string syntax is that you can transform it to be whatever you want later on, because GraphQL is a language, basically. You can convert it down to an AST using the tools that are provided, so you can do whatever you want. This is why the tooling is so legit, cuz if you have an AST that's representing a schema, I mean, I could take this and I could probably generate some front-end components if I wanted to.

[00:02:17] I could generate some forms, I can generate all types of stuff based off this, if I had an AST. So transforming this to something other than a string is very trivial in GraphQL. And like I said, it's much easier to read. It can be composable and that's what we're going to be doing.

[00:02:35] We're gonna be having multiple schema files and we can pose them together. And we'll see some different techniques on how to do that. That way we're not putting our scheme in just one place and one file, which can be kind of gross. And we could co-locate them depending on where they need to be for database models and stuff like that.

[00:02:51] And so far the Schema Definition Language is supported by pretty much every tool that I've every used in GraphQL. That's just the preferred way now because the JavaScript implementation of GraphQL which is GraphQL.js just has amazing support for transformations across GraphQL now. And it's pretty much supported everywhere.