fetch graphql code example

Example 1: fetch graphql

const query = `
query {
    questions {
      id
      code
      content
    }
  }
`,
  url = "https://....com/graphql", //your url Endpoint
  datas= [],
  opts = {
  method: "POST",
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
body: JSON.stringify({ query })
};
fetch(url, opts)
  .then(r => r.json())
  .then(({ data }) => (document.getElementById('id_3').innerHTML = data.questions
    .map(r => `
      

your data ${r.data}

`) .join() ))

Example 2: graphq fetch query

const sampleQuery = `query {    countries {        id        name        cities {            id            name            population        }    }}`function fetchMe(sampleQuery) {    let results;    fetch('/graphQL', {        method: "POST",        body: JSON.stringify(sampleQuery)    })    .then(res => res.json())    .then(parsedRes => {      // use parsed results     });fetchMe(sampleQuery)

Tags: