fetch

GraphQL Tutorial #7 - GraphQL Schema



Sharing buttons:

so then we know we have to make a schema

to describe how the date on our graph

will look so let's make that schema now

inside server I'll make a new folder and

I'm gonna call this said schema and

inside that will create a file and we're

also going to call this schema Jas all

right so the first thing we need to do

inside this file is require graph QL

that's the main graph QL package we

installed in the last video so we'll say

Const graph QL is equal to require and

then graph queue up all right

so in this file is where we're going to

define our schema remember our schema

will describe the data on this kind of

graph it describes the object types the

relations between those object types and

it describes how we can reach into the

graph to interact with that data whether

that be to query it and retrieve data or

to mutate or change the data so either

way we're jumping into this graph to

interact with it right so we'll start

off by inside this schema describing the

object types we want on our graph so the

graph right here is going to have two

object types on it books and authors so

ultimately we need to describe these two

object types in the schema file and the

relationship between them and also how

to query these object types to get data

buck so we're just going to start off

with one object type to begin with the

book so the way we define object types

in graph QL is a little bit unusual at

first what we need to do is we need to

grab something from this graph cue our

package to do it so I'm gonna say Const

and I'm going to use a bit of es6

destructor in here which allows us to

grab a variables out of something else

so I'm going to require or not require

destructor graph QL object type and pay

attention to the capitalization here

capital

ql o and c alright that's really

important so I'll set that equal their

graph QL so this D structure and what

it's going to do is grab this variable

of this function for us from this thing

right here from this package we're

taking it out and storing it now so we

can use this inside this file alright so

once we've done that we can now define a

new type so we'll call this type a book

type and we'll set this equal to a new

grass ql object type alright so that's

the thing we've just grabbed right here

so this right here is a function which

takes in a object and this object is

going to define what this book type is

all about so first of all we'll give

this a name I'm going to call it book

and then we need to also define the

fields on this object type so remember

the fields are going to be things like a

name property genre an ID that kind of

thing so this fields property is

actually going to be a function so we'll

do our parentheses and then an arrow

then another parenthesis and then an

object inside that now the reason this

needs to be a function is because later

on when we have multiple types and they

have references to one another then

unless we wrap those fields in a

function one type might not necessarily

know what I know the type is does that

make sense

it might not a minute but later on when

we introduce all the types as well I'll

explain this a little bit more and why

we need it and I'll show you what

happens if we don't wrap it in a

function all right so this right here

this function is going to turn this

object and we need to define our

different fields inside here so first of

all the ID the ID is going to be maybe a

string of random numbers or something

like that so when we're defining these

fields we need to say what type is is it

a string is he an integer or some other

type so we'll open up an object and

we're going to say the type is going to

be something now we can't just do string

again this is one of the nuances of

graph QL we have to use a special string

a graph QL string in order for it to

understand the type so again we're going

to grab that from graph QL by doing a

comma and then saying graph QL string so

again with destructor in here and we're

grabbing this thing from this package as

well so now we have access to this in

this file so the type of this ID is

going to be a graph QL string all right

so a string of seemingly random numbers

or something like that so that's one

field that we're gonna have on each book

what is another field the name of the

book so again we need to define a type

for this and it will be a string again

so we'll say graph keep our string for

this and then finally we'll have a genre

property and again this will be of type

string so we'll say graph QL string for

that one as well so right now what we've

done is we've defined our first object

type on this graph and it's a book type

it's called book and it has these fields

right here an ID name and genre and that

all of type graph QR string this is

graph QL saying okay I'm expecting a

string for this type and this type and

this type okay so all three of them and

we've wrapped all of those fields inside

a function and es6 function which is

returning this object and again that's

because when we have multiple types

later on this is going to help overcome

any kind of reference errors all right

and we'll explore that a little bit more

when we have multiple types so this book

type here is a first step in defining

our our graph we'll look we've said that

on a graph we're going to have this

object type which is a book right but

this is just one part of our schema

based on this diagram down here remember

we have other things in here an author

type we also have these connections

between the different objects and we

have these entry points as well where we

jump into the graph so this thing right

here is a good first step but it is just

the first step in defining our whole ski

remember ultimately we want to pass all

of this schema into this thing right

here all right

so we've started to define this in the

next tutorial what we're going to do is

we're going to look at our entry points

into the graph so that we can query this

data these are called rip queries and

we'll take a look at those in the very

next tutorial