hasura / graphqurl
- суббота, 11 августа 2018 г. в 00:15:45
JavaScript
curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a JS library
Made by the team at hasura.io, graphqurl
is a curl like CLI for GraphQL:
npm install -g graphqurl
npm install --save graphqurl
gq https://my-graphql-endpoint/graphql \
-H 'Authorization: token <token>' \
-q 'query { table { column } }'
GraphQURL can auto-complete queries using schema introspection. Execute the command without providing a query string:
$ gq <endpoint> [-H <header:value>]
Enter the query, use TAB to auto-complete, Ctrl+Q to execute, Ctrl+C to cancel
gql>
You can use TAB
to trigger auto-complete. Ctrl+C
to cancel the input and
Ctrl+Q
/Enter
to execute the query.
Open GraphiQL with a given endpoint:
gq <endpoint> -i
This is a custom GraphiQL where you can specify request headers.
Mutations with variables can be executed by providing the variables with -v
flag.
gq <endpoint> \
-v 'name=hasura' \
-q 'mutation ($name: String) { table (objects: [{ name: $name }]) }'
Subscriptions can be executed and the response is streamed on to stdout.
gq <endpoint> \
-q 'subscription { table { column } }'
$ gq ENDPOINT [-q QUERY]
ENDPOINT
: graphql endpoint (can be also set as GRAPHQURL_ENDPOINT
env var)-q, --query=query
: graphql query to exxecute-H, --header="key:value"
: request header-v, --variable="key=value"
: variables used in the query-n, --name=name
: name of the graphql definition to execute, use only if there are multiple definitions--queryFile=/path/to/queryfile
: file to read the query from--variablesFile=/path/to/variablefile
: file to read the query variables from-i, --graphiql
: open graphiql with the given endpoint, headers, query and variables-p, --graphiqlPort=graphiqlPort
: [default: 4500] port to use for graphiql-a, --graphiqlAddress=graphiqlAddress
: [default: localhost] address to use for graphiql-l, --singleLine
: show output in a single line, do not prettify--version
: show CLI version-h, --help
: show CLI helpconst { query } = require('graphqurl');
function successCallback(response, queryType, parsedQuery) {
if (queryType === 'subscription') {
// handle subscription response
} else {
// handle query/mutation response
}
}
function errorCallback(error, queryType, parsedQuery) {
console.error(error);
}
query(
{
query: 'query { table { column } }',
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
}
},
successCalllback,
errorCallback
);
For queries and mutations,
const { query } = require('graphqurl');
query(
{
query: 'query { table { column } }',
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
}
}
).then((response) => console.log(response))
.catch((error) => console.error(error));
For subscriptions,
const { query } = require('graphqurl');
query(
{
query: 'subscription { table { column } }',
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
}
}
).then((observable) => {
observable.subscribe(
(event) => {
console.log('Event received: ', event);
// handle event
},
(error) => {
console.log('Error: ', error);
// handle error
}
)
})
.catch((error) => console.error(error));
Subscriptions are not supported in browsers yet.
{}
query
string contains multiple operations.query
, mutation
, subcription
]query
, mutation
, subcription
]successCallback
and errorCallback
are not provided, this function returns the response wrapped in a promise.
queries
and mutations
. However, if you make a subscription, it returns an observable that you can later subscribe to. Check this example to see how to subscribe to observables.Query example with variables
const { query } = require('graphqurl');
query(
{
query: `
query ($name: String) {
table(where: { column: $name }) {
id
column
}
}
`,
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
},
variables: {
name: 'Alice'
}
}
).then((response) => console.log(response))
.catch((error) => console.error(error));
const { query } = require('graphqurl');
query(
{
query: `
mutation ($id_insert_input: String!, $column_insert_input: String!) {
insert_to_table (
id: $id_insert_input,
column: $column_insert_input
) {
affected_rows
}
}
`,
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
},
variables: {
id_insert_input: 'id_ak23sdfkjk2',
column_insert_input: 'Bob'
}
}
).then((response) => console.log(response))
.catch((error) => console.error(error));
Using promises,
const { query } = require('graphqurl');
const eventCallback = (event) => {
console.log('Event received:', event);
// handle event
};
const errorCallback = (error) => {
console.log('Error:', error)
};
query(
{
query: 'subscription { table { column } }',
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer Andkw23kj=Kjsdk2902ksdjfkd'
}
},
).then((observable) => {
observable.subscribe(
(event) => {
console.log('Event received', event);
// handle event
},
(error) => {
console.log('Error', error);
// handle error
}
)
}).catch(errorCallback);
Lets do the above subscription using callbacks,
const { query } = require('graphqurl');
function eventCallback(event) {
console.log('Event received:', event);
// handle event
}
function errorCallback(error) {
console.log('Error:', error)
}
query(
{
query: 'subscription { table { column } }',
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer Andkw23kj=Kjsdk2902ksdjfkd'
}
},
eventCallback,
errorCallback
);
Generic example:
gq \
https://my-graphql-endpoint/graphql \
-H 'Authorization: token <token>' \
-H 'X-Another-Header: another-header-value' \
-v 'variable1=value1' \
-v 'variable2=value2' \
-q 'query { table { column } }'
Reading the query and variables from a file:
gq \
https://my-graphql-endpoint/graphql \
-H 'Authorization: token <token>' \
-H 'X-Another-Header: another-header-value' \
--variableFile='./queryVariables.json' \
--queryFile='./query.gql
Executing only a particular named query from a file that contains many queries:
gq <endpoint> --queryFile ./queries.gql --name getItems
Maintained with ♡ by Hasura