Skip to content

Instantly share code, notes, and snippets.

@kurtisdunn
Created January 7, 2019 01:48
Show Gist options
  • Save kurtisdunn/bc87df7f82de49893823ea6fae8c516a to your computer and use it in GitHub Desktop.
Save kurtisdunn/bc87df7f82de49893823ea6fae8c516a to your computer and use it in GitHub Desktop.
Simple client side GraphQL query with vars

Simple client side GraphQL query with vars

var dice = 3;
var sides = 6;
var query = `query RollDice($dice: Int!, $sides: Int) {
  rollDice(numDice: $dice, numSides: $sides)
}`;

fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query,
    variables: { dice, sides },
  })
})
  .then(r => r.json())
  .then(data => console.log('data returned:', data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment