Example of Using Fetch API for GraphQL request

  • assumes that your server accepts GraphQL requests over HTTP.
const limit = 5;
const query = `query author($limit: Int!) {
author(limit: $limit) {
id
name
}
}`;
fetch('/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query,
variables: { limit },
})
})
.then(r => r.json())
.then(data => console.log('data returned:', data));