Minimal GraphQL client with UMD-support, written in plain javascript (ES5).
Inspired by graphql-request
(with almost identical interface).
See changes in Version 3 here
- Very simple and lightweight GraphQL client (986 B gzipped)
- No dependencies
- AMD-, CommonJS and global-support (Universal Module Definition)
- Promise-based API (works with
async
/await
) - Supports HTTP methods POST and GET
- You have to provide polyfills for
fetch
andPromise
to use it in IE9+
npm install grafico-ql
Send a GraphQL query with few lines of code.
<script src="node_modules/grafico-ql/dist/grafico-ql.min.js"></script>
<script>
var query = '{country(code:"IT") {name}}';
GraficoQL.request('https://countries.trevorblades.com', query)
.then(function (data) { console.log(data); });
</script>
// Run GraphQL queries/mutations using a static function
GraficoQL.request(endpoint, query, variables)
.then(function (data) { console.log(data); });
// ... or create a GraphQL client instance to send requests
var client = GraficoQL.create(endpoint, { headers: {} });
client.request(query, variables).then(function (data) { console.log(data); });
Additional options to the fetch
-call, can only defined during instantiating a GraphQL-client. So, if you want to use the method GET instead of POST, you have to create a client:
await GraficoQL.create(endpoint, {method: "GET"}).request(query);
Again, for passing additional options to fetch
, you have to create a client:
<script src="node_modules/requirejs/require.js"></script>
<script>
require.config({baseUrl: 'node_modules'});
require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
var endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr';
var query = '{Movie(title: "Inception") {releaseDate, actors {name}}}';
var graphQLClient = GQL.create(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN'
}
});
graphQLClient.request(query)
.then(function (data) {
console.log(JSON.stringify(data, undefined, 2));
})
.catch(function (error) {
console.error(error);
});
});
</script>
<script src="node_modules/requirejs/require.js"></script>
<script>
require.config({baseUrl: 'node_modules'});
require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
main(GQL).catch(error => console.error(error));
});
async function main(GraphQLClient) {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
`
const graphQLClient = GraphQLClient.create(endpoint, {
credentials: 'include',
mode: 'cors',
})
const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))
}
</script>
<script src="node_modules/requirejs/require.js"></script>
<script>
require.config({baseUrl: 'node_modules'});
require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
var endpoint = 'https://countries.trevorblades.com';
var variables = {
cc: 'IT'
};
var query = 'query getCountry($cc: ID!) {'
+ 'country(code:$cc) {name}'
+ '}';
GQL.request(endpoint, query, variables)
.then(function (data) {
console.log(JSON.stringify(data, undefined, 2));
})
.catch(function (error) {
console.error(error);
});
});
</script>
For better debugging purposes, in case the status code of the response has not the value 200 (OK), the promise will be rejected and the resulting data will be in format: {response: {...}, request: {...}}
.
<script src="node_modules/grafico-ql/dist/grafico-ql.min.js"></script>
<script>
var endpoint = 'https://countries.trevorblades.com'
var query = /* GraphQL */ '\
{\
country(code: "IT") {\
name\
notAvailableField\
}\
}\
';
GraficoQL.request(endpoint, query)
.then(function (data) {
console.log(data);
return data;
})
.catch(function (err) {
console.error(err);
return Promise.reject(err)
});
</script>
The request method will return the data, errors and/or extensions key from the response. If you need to access any additional (non-standard) keys, you can use the rawRequest method:
import { rawRequest } from 'grafico-ql'
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
`
const { data, errors, extensions, headers, status } = await rawRequest(
endpoint,
query
)
console.log(
JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)
)
}
main().catch(error => console.error(error))
If you want to distribute your changes in the 'dist'-directory, you can use npm:
$ npm run build
The MIT License (MIT)
Copyright (c) 2018 Daniel Moritz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.