graphql client code example

Example 1: graphql client side

fetch('/graphql', {  method: 'POST',  headers: {    'Content-Type': 'application/json',    'Accept': 'application/json',  },  body: JSON.stringify({query: "{ hello }"})})  .then(r => r.json())  .then(data => console.log('data returned:', data));

Example 2: 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 => `
      <p>your data ${r.data}</p>
    `)
    .join()
  ))

Tags:

Misc Example