Introduction to GraphQL Introduction to GraphQL

GraphQL Exploration Review

Check out a free preview of the full Introduction to GraphQL course:
The "GraphQL Exploration Review" 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 demonstrates how to create a type that is type String instead of an Object.

Get Unlimited Access Now

Transcript from the "GraphQL Exploration Review" Lesson

[00:00:00]
>> Scott Moss: Awesome. So, yeah, let's continue with this example. So let's say I come here and I'm gonna make, I'm not gonna make a type, actually. I'm just gonna make another query. And I was gonna call this one hello. And I'm just gonna say it's return type is a string.

[00:00:17] I don't have to give it a type that I created, I can just give it any type. In this case, I'm gonna give it a string type. I'm gonna hit Save.
>> Scott Moss: Let this thing recompile. Go back. And if you've noticed, if you use this, the GraphQL Playground, you'll have to refresh this to get the latest schema.

[00:00:36] I think there used to be an option here to refresh the schema every single time. But you might have to refresh this every time your server refreshes to get the latest one. So now, we got myCat. So what I'm gonna do now is, without showing off too much what GraphQL can do, I'm just gonna do hello.

[00:00:54] And because hello is not an object type, I don't have to put object things here and try to get fields. In fact, it'll break if I try to do that because hello is just a string type. So all I have to do is that. And I get back null because I didn't write a resolver for it.

[00:01:10]
>> Scott Moss: So as you all were walking through that exercise, did anybody observe anything, learn anything, or just got really confused? Looks like everybody was able to do it. So I'm guessing you all are learning. I saw people who were trying to enhance it with stuff. Yeah, Kevin.
>> Speaker 2: One question, I guess, I had is do you have to define what the type is?

[00:01:33] So does it have to be string? Do you have to spell that out while you're typing it out?
>> Scott Moss: You mean as in for the name field, putting String here, do I have to put the String here?
>> Speaker 2: Correct, yeah.
>> Scott Moss: Yeah, on your schema, you have to put that there.

[00:01:48]
>> Speaker 2: Okay.
>> Scott Moss: Every field eventually resolves down to a scholar, and we'll talk about scholars next. We'll get into that, but yeah, you absolutely have to put that. Not on the query, though. On the query side, it's the opposite. You don't put the types, you just ask for the fields and then they come back.

[00:02:05] For the schema side, yes.
>> Scott Moss: Anything else, any other questions?
>> Scott Moss: Nope, all right. That was just an exploratory exercise. I'm sorry, Dave.
>> Speaker 2: So what would your resolver look like for hello?
>> Scott Moss: Good question, yeah.
>> Speaker 2: Just return a string?
>> Scott Moss: Yeah, so for hello, I come down here to Query, I write hello, and I'll just return a string.

[00:02:31] I'll just say hello, just like that. And as long as it's a string, it'll be a fine. So if I go back, refresh this hello, you can see I get back, hello.