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 "Scalar & Object Types" 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 describes what scalars are and the object types that exist in GraphQL.

Get Unlimited Access Now

Transcript from the "Scalar & Object Types" Lesson

[00:00:00]
>> Scott Moss: So hopping into what you were talking about Dave, which is gonna be the different types and graph scale. So there's basically, I mean, there's many times, but like as far as value types, there's really only two, there's gonna be scholars and object types. And basically what they both do is they describe resources that will be used in queries and mutations.

[00:00:18] So scholar types are like built in primitives and here are the built in ones. We have a string, we have an int, which is like integer, we have a float which is like a decimal, we have a boolean which is true or false and then you have ID which is literally the same thing as a string except it's meant to be unique.

[00:00:35] There's no validation on it. You almost never have the user ID but it's there just to know that this is a unique string. That's what ID is for, it's a unique thing. So yeah, you could use ID for ID but it's basically a string. So those are built in primitives, so those are free for you in GraphQL.

[00:00:54] And eventually, every single field that you make has to boil down to one of these at some point in your tree, if you have a cat that's referencing Owner and owner is referencing a parent, and a parent is referencing a grandparent. Eventually, it's gonna boil down to some field being a string integer or float or boolean or ID cuz those are where values are stored.

[00:01:17] Object types are custom shapes with fields that are either scholar types or other object types. So if we go back over to our example here, type cat is an object type cuz we used the key word type. We give the name cat, and these brackets, they have nothing to do with JavaScript.

[00:01:37] But you can think of them as there's brackets here, so this is is an object type. That helps you remember it and you can use that but that has nothing to do with JavaScript. But yes, this is a custom object type. String is a scholar type, int is a scholar type.

[00:01:50] And you can see, what I was saying is any field can be any type. I could come here to this cat and say best friend. Best friend is a cat, so like I said a cat has a best friend, which is also a cat, and that points to an object which is cat, which has a name and an age.

[00:02:13] So how would I resolve that? I would come down here, and I would say bestFriend.
>> Scott Moss: And guess what that needs to look like? This would look like the same thing that it's been hosted on, and it will just go forever and ever and ever cause its graph.

[00:02:40] You can see that it's a graph, it'll keep going, so I could resolve that, as long as I don't ask what the best friend's best friend is, this query should work cuz I don't feel like adding that, that's just too much work. So if you go back, we refresh this, and we say give me myCat.

[00:02:58] I want the name, and then I'm gonna get the bestFriend, and then I want to get the name of that best friend.
>> Scott Moss: Then I can query for that and you can see that's what I get back. So a field eventually has to come down to being some type of style type but it doesn't prevent you from having object types as fields as long as they boil down to some style type eventually.

[00:03:24]
>> Speaker 2: How does it handle a race situation where you point back to the same object?
>> Scott Moss: Like so what do you mean, handle it? [LAUGH]
>> Speaker 2: You could potentially create a race situation where the-
>> Scott Moss: A circular dependancy where it keeps going forever?
>> Speaker 2: Yes.
>> Scott Moss: It doesn't.

[00:03:41] So that's gonna be up to you, right? I don't wanna get too deep into it cuz we're gonna talk about it when we get to resolvers. But yeah, if I added a previous best friend here and I referenced back to the other one, it kept going back and forth.

[00:03:56] Yeah, it'll just go on forever if they keep asking for it, right? If someone keeps writing in here like this and they just keep going,
>> Scott Moss: And they just go on and on and on and on, yeah, there's nothing stopping them from doing this by default. If you just leave it that way, the resolvers will continue to keep resolving it.

[00:04:19] And if every single one of those is a database query, you could literally kill your server. Somebody could DDOS you super quick. So there's a lot of ways to get around this, and we could talk about those at the end of the day when you have more context about resolvers, cuz they're really advanced.

[00:04:33] But yeah, but basically, by default, there's nothing you can do. They just gonna do it, yeah. We ran into this problem a lot.