- Five Basic Types --> Also Called Scalars Which Are Already Primitive To GraphQL
- ID
- String
- Float
- Integer
- Boolean
Suppose In Our Database We Have A Collection Which Contains User Data. So We Define Its Type In GraphQL As Shown Below. So GraphQL Knows What User Is Composed Of.
type User {
id:ID!
name:String!
phoneNumber:Int!
isPremium:Boolean!
}
For Example Suppose A 'User' Has Field Called 'Friends' So What Would Field 'Friends' Composed Of. Well Each 'Friend' Is A 'User' So Technically Friends Should Be Of Type Of List Of 'Users'. But How Can We Use 'User' Type Inside Of Itself. In Graphql We Can Do That Each 'User' Has A List Of 'Users'.Using Square Bracket Notation And Type Inside Of It [Type].
We Can Use Basic Types (String,Boolean etc.) To Create Multiple Types (Example - User) And We Can Use Those Types To Nest In Each Other
friends:[User]
And When We Access 'Friends' Field We Also Has Access To 'User' Field(id,name,phoneNumber etc.)
GraphQL Field Can Have Either 'Value' Or It Can Be 'Null'. By Defining '!' We Telling It That Value We Recieve On That Particular Field Cant Be Null. And By Removing That '!' Mark We Can Let It Be Null.
type User {
id:ID!
name:String!
phoneNumber:Int!
isPremium:Boolean!
friends:[User]
}
We Can Also Create Another Type Which Is Different And Nest It Too. In The Example We Can See That 'User' Can Have List Of Posted 'Video'. And Each 'Video' Contains 'ID' , 'title' , 'description' Type.
We Can Also Say That Its Not Required To Have You List Of 'Friends'
friends:[User]
But If You Have 'Friends' Then 'User' Type Must Be Providedfriends:[User!]
.
type User {
id:ID!
name:String!
videosPosted:[Video]
}
type Video{
id:ID!
title:String!
description:String!
}
We Can Also Test GraphQL Queries And Mutation Too But With API Which We Are Working Must Support GraphQL Fetching Method. Either It Has To Be Rest Api Which Communicate With GraphQL Layer Between Frontend And The Backend. Or It Can Be GraphQL Api Which Is Made Completely Using GraphQL.
Link -> https://countries.trevorblades.com/
Every GraphQL Api Must Have Something Called 'Schema' And It Not Like Database Schema Because GraphQL Has Nothing To Do With Your Database. It Just The Schema That Defines Data That Exist Inside The API. And Every GraphQL Schema Has Root Type Called 'Query'. And Inside It Each Field Something Looks 'users' Which Returns List Of 'User'. And If Can Also Get 'User' By Providing Argument.
type Query{
users : [User]
user(id:ID,name:String): User
}
Their Are Two Ways To Pass Argument When Quering First Is Shown In Above Example
user(id:ID,name:String)
And Second Is Other Organised Way Using 'input' and Passing It As Argument.
input userAgumentInput{
id:ID
name:String
}
type Query {
user(input:userArgumentInput):User
}
In Schema There Is Type Called Query This Query Defined All Of The Api Endpoint Which We Can Request To.
Example
type User{
id:ID!
name:String!
phoneNumber:Int!
}
type Query{
users:[User]
user(name:String!):User
}
By Specifying The Root Query({}) We Are Telling That We Want To Query Something From Schema Which Is Defined In Above in
type Query
. Now Fromtype Query
Which Field You Want To Query So We Want To Query 'user' field With Argument 'name'. Now Its Gonna Return Us A Type 'User' Which Is Not GraphQL Native Type And Was Created By Us So We Have Specify What We Want From That Type.
# Querying
{
user(name:'Ashish'){
id
name
}
}
# Returned
{
"data":{
"user":{
"id":"23566465",
"name":"Ashish"
}
}
}
Another Example
In Below Example First We Openend Root Query({}) Then Suppose We Want 'user' Which Is Asking For 2 Argument 'name','email' After Providing Argument We Specify What We Want To Get So We Want 'name' and 'email' and 'address' But 'address' is A Whole Another Type Which Contains Field Like "city","state","country" So We Also Need To Specify That. Because 'address' Type Is Not GraphQL Native Type Instead Its Contains More Type.
type Address {
city:String!
state:String!
country:String!
}
type User {
id:ID!
name:String!
email:String!
address:Address
}
type Query{
users:[User]
user(name:String!,email:String!):User
}
#Querying
{
user(name:"KreidePrinz",email:"KreidePrinz@gmail.com"){
name
email
address{
city
state
country
}
}
}