GraphQL
Last updated
Was this helpful?
Last updated
Was this helpful?
You can use fetch for making requests to GraphQL servers. Since GraphQL runs on the HTTP protocol, you can use 'POST
' for them.
Here, I'm using .
First, create a GQL query "string" with a template literal.
The leading dollar sign ($) means it's a variable.
The trailing exclamation mark (!) means variable id
is non-nullable. ()
What if you want to make a query with a list?
Wrap the variable type with square brackets []. Now it's a List type.
Combine Non-Null and List modifiers and you get a list of non-null type. -- [ID!]!
It means the List is non-null and the List can't contain null values.
Next, let's make a function to fetch the response and define the response body type.
Let's test it out with Jest!
And Voila, you're now the master of GQL with fetch API.