- Enums Are Powerful Aspect Of The GraphQL Language.
- Because It Will Allow Us To Validate The Data.
Example
type User{
id:ID!
name:String!
email:String!
nationality:String!
}
type Query{
users:[User!]
}
- Here We Want To Validate The Nationality Because We Dont Want Requester To Enter Any Country Which Doesnt Exist So With Enums We Can Validate.
- One Way We Can Validate And Prevent Them Doing That. We Can Create Enum. And Give It A Name And Inside That We Can Put All The Nationality Which Can Exist.
- enum Values Can Be In Capital Letters For Best Practices. But Its Casing Also Match With The Data.
- Now Insted Of Saying 'nationality' Field Is Type Of String It Can Be Type of Enum 'Nationality'. And Now It Can Only Accept The Value Which Are Listed Inside The enum 'Nationality'.
type User{
id:ID!
name:String!
email:String!
nationality:Nationality!
}
type Query{
users:[User!]
}
enum Nationality {
USA
CANADA
CHILE
INDIA
}
- Now If We Add Some Nationality Inside Our
FakeData.js
Which Doesnt Match With The enum Nationality Values. Its Gonna Throw Us Error.
Inside The
type Query{}
We Can See We Have A Field Calleduser(id:ID!):User
Inside The Argument We Are Passingid
And After Querying We Returning The 'User'.
type User{
id:ID!
name:String!
email:String!
}
type Query{
users:[User!]
user(id:ID!):User
}
But For This To Work We Also Have To Setup Resolver. So Inside Our 'resolvers.js' File. Inside Our 'resolvers' Variable's 'Query' Object We Add Another Function. Which Resolve The 'user' With Argument.
So 'user' Function Here Accept Some Argument, The Second We Are Using Is Called 'args' Which Can Have Access To The
id
Which Requester is Passing.
We Also Using An Array Resolving Library Called Lodash Which Can Do Stuffs With Array. So We Import It.
_.find()
Method Accept 2 Argument First One IsUsersList
From Which We Gonna Find The User. And Second Is OurId
With Which Its Gonna Match.
Important Thing To Note Here Is Inside Our
UserList
Allid
Are In Number And GraphQL Argument Only Accept String And Number As Type OfID
. Even Though Requester Pass Argument Variable As Number Once It Reach Inside 'resolver' Its Gonna Be In String And Since We Can't Compare String With Number We Gonna Convert String Into Number So Lodash Function Can Handle It_.find(UsersList,{id:Number(id)})
.
const _ = require('lodash')
const {UsersList} = require('../FakeData.js)
const resolvers = {
Query: {
users:() => {
return UsersList;
},
user:(parent,args) => {
const id = args.id
const user = _.find(UsersList,{id:Number(id)})
return user
}
},
};
So Inside Our Apollo Client We Can Make Query Like This.
query userByID {
user(id:1){
id
name
email
}
}
In This Example We Create A New Type Which Is Not Really Related To The User But We Can Connect Both Of Them.
//FakeData.js
const MoviesList = [
{
id:1,
title:"Interstellar",
yearOfPublication:2013
},
{
id:2,
title:"Spider-Man:No Way Home",
yearOfPublication:2021
},
{
id:3,
title:"Enternals",
yearOfPublication:2021
},
]
module.exports = {MoviesList}
//type-defs.js
type User{
id:ID!
name:String!
email:String!
}
type Movie{
id:ID!
title:String!
yearOfPublication:Number!
}
type Query{
users:[User]
user(id:ID!):User!
movies:[Movie]
movie(title:String!):Movie!
}
//resolvers.js
const {MoviesList} = require('../FakeData.js')
const _ = require('lodash')
const resolvers = {
Query:{
movies:() => {
return MoviesList;
},
movie:(parent,args) => {
const title = args.title;
const movie =_.find(MoviesList,{title})
return movie;
}
}
}
//GraphQL Playground
query getMovie{
movie(title:"Interstellar"){
id
title
yearOfPublication
}
}
But Now Comes The Real Part Now We Will See How We Can Nest 'Movie' Type With 'User' Type.
We Comes To The 'User' Type And Can Add A Specific Field Called 'favoriteMovies' And We Are Returning The 'Movie' Type Which We Created And We Are Not Making It Required Because Not All User Has Fav Movie.
This Explain The We Included A New Type Inside The 'User' Type Which Include Information About 'Movie' Type. Thats Also Important For Many Reason Because First We Dont Want Directly Include
MoviesList
For EachUser
Inside TheUserList
. Because GraphQL Is Meant Like That When U Have A Type(User
) Which Include A Type(Movie
) Which Is Not One Of The Type Of GraphQL You Should Create A Resolver For That Field Explaining How You Gonna Get That Data. So For Example We Have 'favoriteMovies' As One Of The Types For Our User. So We Can Create Resolver For That.
type User{
id:ID!
name:String!
email:String!
favoriteMovies:[Movie]
}
type Movie{
id:ID!
title:String!
yearOfPublication:Number!
}
Lets Create A Resolver That Is Not From 'Query' Type. Lets Create Resolver For The 'User' Type Because Whenever We Wanna Query 'User' And Get Information About 'favoriteMovies' How We Gonna Know Which Movie Is Favorite For Each 'User'. So First Resolver We Wanna Have A Resolver Called 'favoriteMovies' Inside The 'User' Field. And We Can Return The
MoviesList
From That. And We Can Query It From GraphQL Playground.
//resolver.js
import {MoviesList} = require('../FakeData.js')
const resolvers = {
Query:{},
User:{
favoriteMovies:() => {
return MoviesList;
}
}
}
//GraphQL Playground
query getUser{
user(id:1){
name
favoriteMovies{
id
title
yearOfPublication
}
}
}