Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add slient mode with cli usage #83

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const querySuccessCb = (ctx, response, queryType) => {
if (queryType === 'subscription') {
cli.action.stop('event received');
ctx.log(out);
cli.action.start('Waiting');
ctx.action.start('Waiting');
} else {
if (ctx.flags.introspect) {
if (ctx.flags.format === 'graphql') {
Expand All @@ -24,6 +24,7 @@ const querySuccessCb = (ctx, response, queryType) => {

const queryErrorCb = (ctx, queryError, queryType) => {
cli.action.stop('error');

if (!queryType) {
handleGraphQLError(queryError);
} else if (queryType === 'subscription') {
Expand Down
23 changes: 21 additions & 2 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GraphqurlCommand extends Command {

if (queryString === null) {
try {
queryString = await executeQueryFromTerminalUI({
queryString = await executeQueryFromTerminalUI(this, {
endpoint: endpoint,
headers,
variables,
Expand All @@ -71,10 +71,23 @@ class GraphqurlCommand extends Command {
name: flags.name,
};

cli.action.start('Executing query');
this.start('Executing query');

await query(queryOptions, successCallback, errorCallback);
}

start(message) {
if (!this.flags.silent) {
cli.action.start(message);
}
}

stop(message) {
if (!this.flags.silent) {
cli.action.stop(message);
}
}

parseHeaders(headersArray) {
let headerObject = {};
if (headersArray) {
Expand Down Expand Up @@ -290,6 +303,12 @@ GraphqurlCommand.flags = {
default: 'graphql',
options: ['json', 'graphql'],
}),

silent: flags.boolean({
char: 's',
default: false,
description: 'silent mode',
}),
};

GraphqurlCommand.args = [
Expand Down
13 changes: 6 additions & 7 deletions src/ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const tk = require('terminal-kit');
const {getIntrospectionQuery, buildClientSchema} = require('graphql/utilities');
const {parse} = require('graphql/language');
const {cli} = require('cli-ux');
const {validateQuery, getAutocompleteSuggestions} = require('graphql-language-service-interface');
const {Position} = require('graphql-language-service-utils');
const makeClient = require('./client');
Expand Down Expand Up @@ -149,12 +148,12 @@ const getQueryFromTerminalUI = () => {
});
};

const executeQueryFromTerminalUI = async (queryOptions, successCb, errorCb) => {
const executeQueryFromTerminalUI = async (ctx, queryOptions, successCb, errorCb) => {
const {
endpoint,
headers,
} = queryOptions;
cli.action.start('Introspecting schema');
ctx.start('Introspecting schema');
let client = makeClient({
endpoint,
headers,
Expand All @@ -163,10 +162,10 @@ const executeQueryFromTerminalUI = async (queryOptions, successCb, errorCb) =>
try {
schemaResponse = await client.query({query: getIntrospectionQuery()}, null, errorCb);
} catch (e) {
cli.action.stop('error');
ctx.stop('error');
throw new Error('unable to introspect graphql schema at the given endpoint');
}
cli.action.stop('done');
ctx.stop('done');
const r = schemaResponse.data;
// term.fullscreen(true);
schema = buildClientSchema(r);
Expand All @@ -176,9 +175,9 @@ const executeQueryFromTerminalUI = async (queryOptions, successCb, errorCb) =>
while (!exit) {
/* eslint-disable-next-line no-await-in-loop */
const queryString = await getQueryFromTerminalUI();
cli.action.start('Waiting');
ctx.start('Waiting');
await query({query: queryString, endpoint, headers}, successCb, errorCb);
cli.action.stop('done');
ctx.stop('done');
}
};

Expand Down