fetch data using graphql queries code example

Example 1: 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));

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:

Misc Example