GraphQL
Going without dedicated GraphQL clients like Apollo in JS/TS
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 node-fetch.
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. (Stack Overflow)
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.
More on this here: GraphQL Schema - Lists and Non-Null
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.
REF
Last updated