grafico-ql
v3.0.8
Published
A minimal graphql client.
Downloads
16
Maintainers
Readme
GraficoQL
Minimal GraphQL client with UMD-support, written in plain javascript (ES5).
Inspired by graphql-request
(with almost identical interface).
Migration of version 2 to 3
See changes in Version 3 here
Features and limitations
- Very simple and lightweight GraphQL client (~ 1.2 kB 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+
Install
npm install grafico-ql
Quickstart
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>
Usage
// 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); });
API documentation
Examples
Use method GET instead of POST
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);
Authentication via HTTP header
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>
Passing more options to fetch ...
<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>
Using variables
<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>
Error handling
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>
Receiving a raw response
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))
Development
If you want to distribute your changes in the 'dist'-directory, you can use npm:
$ npm run build
License
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.