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:

Solving The Node.js Console.Time Is Not A Function Error

Solving the Node.js console.time is not a function error

Explore the two variants of the `console.time is not a function` error, their possible causes, and how to debug.

Joseph Mawa
Nov 1, 2024 â‹… 6 min read
Why jQuery 4 Is A Good Reminder To Stop Using jQuery

Why jQuery 4 is a good reminder to stop using jQuery

jQuery 4 proves that jQuery’s time is over for web developers. Here are some ways to avoid jQuery and decrease your web bundle size.

Shalitha Suranga
Oct 31, 2024 â‹… 11 min read
How to Create a Multilevel Dropdown Menu in React

How to create a dropdown menu in React

See how to implement a single and multilevel dropdown menu in your React project to make your nav bars more dynamic and user-friendly.

Ibadehin Mojeed
Oct 30, 2024 â‹… 12 min read
Purple background with different connections between icons for an article about building Node.js modules with Rust and NAPI-RS.

Building Node.js modules in Rust with NAPI-RS

NAPI-RS is a great module-building tool for image resizing, cryptography, and more. Learn how to use it with Rust and Node.js.

Rahul Padalkar
Oct 30, 2024 â‹… 7 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