Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Created April 26, 2017 09:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastianallgeier/3733bbec13cc635d4c9d7a9afa34f144 to your computer and use it in GitHub Desktop.
Save bastianallgeier/3733bbec13cc635d4c9d7a9afa34f144 to your computer and use it in GitHub Desktop.
GraphQL micro client
// Client
const query = (query, data, headers) => {
return fetch('/api', {
method: 'POST',
headers: headers || {},
body: JSON.stringify({
query: query,
variables: data
})
}).then((response) => {
return response.json()
}).then((json) => {
return json.data
})
}
// Example
query(`
query($id: Int) {
user(id: $id) {
username
email
photo {
url
size
mime
}
}
}
`,
{
id: 1234
}).then((result) => {
console.log(result.user)
}).catch((error) => {
console.log(error)
})
@deanrad
Copy link

deanrad commented Apr 26, 2017

Why POST, for a query ?

@bastianallgeier
Copy link
Author

@deanius the request body contains the query and variables as json encoded string. It wouldn't be useful to send this over as get request. You should not think too much in a RESTful way with Graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment