2020-03-13
1120
#graphql
Adhithi Ravichandran
15608
Mar 13, 2020 â‹… 4 min read

GraphQL variables in simple terms

Adhithi Ravichandran Software consultant, Pluralsight author, speaker, React Native/React/GraphQL dev, and Indian classical musician. You can find me online at adhithiravichandran.com.

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
View all posts

3 Replies to "GraphQL variables in simple terms"

  1. Testing variables in some application is pretty straightforward. However, how do you use variables in a standard fetch call using code?

    1. In the body of the request stringify your graphql query like this:- hope it helps

      fetch(‘https://api.hashnode.com’, {
      method: ‘POST’,
      headers: {
      ‘Content-Type’: ‘application/json’,
      Authorization: ”,
      },
      body: JSON.stringify({
      query:
      ‘mutation createStory($input: CreateStoryInput!){ createStory(input: $input){ code success message } }’,
      variables: {
      input: {
      title: ‘What are the e2e testing libraries you use ?’,
      contentMarkdown: ‘# You can put Markdown here.\n***\n’,
      tags: [
      {
      _id: ‘56744723958ef13879b9549b’,
      slug: ‘testing’,
      name: ‘Testing’,
      },
      ],
      coverImageURL:
      ‘https://codybontecou.com/images/header-meta-component.png’,
      },
      },
      }),
      })
      .then(res => res.json())
      .then(res => console.log(JSON.stringify(res)))

  2. Thank you! After endless hours trying to use string interpolation to inject my $token into the GraphQL request and having to deal with needing to escape quotation mark characters, this helped me correctly use variables for my request.

Leave a Reply