Skip to content

Commit

Permalink
feat: add slient mode with cli usage
Browse files Browse the repository at this point in the history
  • Loading branch information
clementlecorre committed Jun 28, 2021
1 parent 7226131 commit e46b5c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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 @@ -45,7 +45,7 @@ class GraphqurlCommand extends Command {
};

if (queryString === null) {
queryString = await executeQueryFromTerminalUI({
queryString = await executeQueryFromTerminalUI(this, {
endpoint: endpoint,
headers,
variables,
Expand All @@ -61,10 +61,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 @@ -249,6 +262,12 @@ GraphqurlCommand.flags = {
default: 'graphql',
options: ['json', 'graphql'],
}),

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

GraphqurlCommand.args = [
Expand Down
12 changes: 7 additions & 5 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,20 @@ 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,
});
const schemaResponse = await client.query({query: getIntrospectionQuery()}, null, errorCb);
cli.action.stop('done');

ctx.stop('done');

const r = schemaResponse.data;
// term.fullscreen(true);
schema = buildClientSchema(r);
Expand All @@ -169,9 +171,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

0 comments on commit e46b5c6

Please sign in to comment.