Created
April 26, 2017 09:01
-
-
Save bastianallgeier/3733bbec13cc635d4c9d7a9afa34f144 to your computer and use it in GitHub Desktop.
GraphQL micro client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
photo { | |
url | |
size | |
mime | |
} | |
} | |
} | |
`, | |
{ | |
id: 1234 | |
}).then((result) => { | |
console.log(result.user) | |
}).catch((error) => { | |
console.log(error) | |
}) |
@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
Why POST, for a query ?