Answers for "react fetch with graphql api"

2

fetch request to GraphQL

fetch('https://www.learnwithjason.dev/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    query: `
        query GetLearnWithJasonEpisodes($now: DateTime!) {
          allEpisode(limit: 10, sort: {date: ASC}, where: {date: {gte: $now}}) {
            date
            title
            guest {
              name
              twitter
            }
            description
          }
        }
      `,
    variables: {
      now: new Date().toISOString(),
    },
  }),
})
  .then((res) => res.json())
  .then((result) => console.log(result));
Posted by: Guest on May-17-2021
0

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()
  ))
Posted by: Guest on October-08-2020

Browse Popular Code Answers by Language