在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):hasura/graphqurl开源软件地址(OpenSource Url):https://github.com/hasura/graphqurl开源编程语言(OpenSource Language):JavaScript 52.1%开源软件介绍(OpenSource Introduction):graphqurl
Made with Table of contentsInstallationSteps to Install CLInpm install -g graphqurl Steps to Install Node Librarynpm install --save graphqurl UsageCLIQuerygq https://my-graphql-endpoint/graphql \
-H 'Authorization: Bearer <token>' \
-q 'query { table { column } }' Auto-completeGraphqurl 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 GraphiQLOpen GraphiQL with a given endpoint: gq <endpoint> -i
SubscriptionSubscriptions can be executed and the response is streamed on to stdout. gq <endpoint> \
-q 'subscription { table { column } }' Export schemaExport GraphQL schema to GraphQL or JSON format: gq <endpoint> --introspect > schema.graphql
# json
gq <endpoint> --introspect --format json > schema.json Command$ gq ENDPOINT [-q QUERY] Args
Flag Reference
Node LibraryUsing callbacks:const { createClient } = require('graphqurl');
const client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer <token>'
}
});
function successCallback(response, queryType, parsedQuery) {
if (queryType === 'subscription') {
// handle subscription response
} else {
// handle query/mutation response
}
}
function errorCallback(error, queryType, parsedQuery) {
console.error(error);
}
client.query(
{
query: 'query ($id: Int) { table_by_id (id: $id) { column } }',
variables: { id: 24 }
},
successCallback,
errorCallback
); Using Promises:For queries and mutations, const { createClient } = require('graphqurl');
const client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer <token>'
}
});
client.query(
{
query: 'query ($id: Int) { table_by_id (id: $id) { column } }',
variables: { id: 24 }
}
).then((response) => console.log(response))
.catch((error) => console.error(error)); For subscriptions, const { createClient } = require('graphqurl');
const client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer <token>'
},
websocket: {
endpoint: 'wss://my-graphql-endpoint/graphql',
onConnectionSuccess: () => console.log('Connected'),
onConnectionError: () => console.log('Connection Error'),
}
});
client.subscribe(
{
subscription: 'subscription { table { column } }',
},
(event) => {
console.log('Event received: ', event);
// handle event
},
(error) => {
console.log('Error: ', error);
// handle error
}
) APIcreateClientThe
Clientconst client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql'
}); The graphqurl client exposeses the following methods:
More ExamplesNode LibraryQueries and MutationsQuery example with variables const { createClient } = require('graphqurl');
const client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'x-access-key': 'mysecretxxx',
},
});
client.query(
{
query: `
query ($name: String) {
table(where: { column: $name }) {
id
column
}
}
`,
variables: {
name: 'Alice'
}
}
).then((response) => console.log(response))
.catch((error) => console.error(error)); SubscriptionsUsing promises, const { createClient } = require('graphqurl');
const client = createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
'Authorization': 'Bearer Andkw23kj=Kjsdk2902ksdjfkd'
}
websocket: {
endpoint: 'wss://my-graphql-endpoint/graphql',
}
})
const eventCallback = (event) => {
console.log('Event received:', event);
// handle event
};
const errorCallback = (error) => {
console.log('Error:', error)
};
client.subscribe(
{
query: 'subscription { table { column } }',
},
eventCallback,
errorCallback
) CLIGeneric example: gq \
https://my-graphql-endpoint/graphql \
-H 'Authorization: Bearer <token>' \
-H 'X-Another-Header: another-header-value' \
-v 'variable1=value1' \
-v 'variable2=value2' \
-q 'query { table { column } }' Maintained with |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论