In this video Emmanuel Henri introduces what the basic types and fields are in GraphQL, and then adds an array of emails into the main friend type.
- [Instructor] If you're familiar with type checking with libraries like TypeScript or Flow, exploring types in GraphQL will be familiar to you. Everything in GraphQL is defined by types as we've done already in our schema. So basically when you are defining a type, you are defining the shape of how this data will be and what type of data it expects. For example, in your current type friend, we are expecting the data to contain an ID, first name, last name, setup. So this is your object type for the friend.
And if you put an exclamation point, that means that this is mandatory. So that means there needs to be something in that particular field in here. So if we don't have any exclamation point, that means that this particular field can be left empty when we first create it. So let's create the type email like so. And basically what we're expecting is an email in a string. Let's make sure that we don't have the same type. So this would be emails and not email.
Like so. And then all you had to do at this point is change the actual resolver here. So we can accept multiple emails. Otherwise it's not going to work. So let's go ahead and change that very quickly by adding an S here. And then this takes an array. So let's change this here as an array. And then for each item we take an object here. And then let's add another one like so. So let's just do that on the second line here like so.
And then add the second one just below. And we want to make sure that we enter the type for that particular email. So if you go back to the schema, we take an array of an object email here. So each object needs to contain the actual property here, the field, and then the actual value. So we need to make sure that we have that here. And then we have the actual values. So we need to repeat that for the second one as well. And then email@example.com.
So there you have it. So let's save that. Let's go back into GraphQL. And then this time if you take a look at the documentation again, friend. Let's refresh before we do that. Because right now it's showing the old way. So if you take a look at query then friend, this is expecting a string of emails. So let's say for example, we are calling the emails. Then when we return it, we'll get the array of emails like so.
So the first email, the second email. So as we explore GraphQL you'll learn about many other types and we'll go through each of them. So let's move on.
- Basic GraphQL schema
- Object types and fields
- Role of the resolver in GraphQL
- Scalar and enumeration types
- Using GraphQL tools
- Setting up persistence
- Adding new items with mutations
- Updating and deleting items with mutations
- Queries with arguments, aliases, and fragments