CLUI GraphQL
@replit/clui-gql
is a utility libray for building CLUI commands from GraphQL introspection data.
Install
npm install @replit/clui-gql
Usage
To create a tree of CLUI commands call toCommand
and then call forEach
to defined a run function for each command.
import { toCommand, forEach } from '@replit/clui-gql';
import { introspectionFromSchema } from 'graphql';
import schema from './your-graphql-schema';
const introspection = introspectionFromSchema(schema);
const introspection = makeNetworkRequestForData();
const root = toCommand({
operation: 'query',
rootTypeName: 'CluiCommands'
mountPath: ['cli', 'admin'],
introspectionSchema: introspection.__schema,
output: () => ({
fields: '...Output',
fragments: `
fragment Output on YourOutputTypes {
...on SuccessOutput {
message
}
...on ErrorOutput {
error
}
}`,
}),
});
forEach(root, ({ command }) => {
if (command.outputType !== 'YourOutputTypes') {
By omitting the run function the command acts as a namespace for sub-commands.
return;
}
command.run = (options) => {
return <OutputView command={command} options={options} />
}
}
'parseArgs' is a helper for working with args
import { parse } from 'graphql';
import { parseArgs } from '@replit/clui-gql';
const OutputView = (props) => {
const { command } = props;
const { args } = props.options;
const parsed = parseArgs({ command, args });
if (parsed.missing.required) {
return <HandleMissingArgs />;
}
if (parsed.missing.optional) {
return <PotentiallyShowOptinalInputs />;
}
if (command.query) {
graphQLClient.query(parse(command.query), { variables: parsed.variables })
} else if (command.mutation) {
graphQLClient.mutate(parse(command.mutation), { variables: parsed.variables })
}
}