Skip to content
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
108 changes: 108 additions & 0 deletions examples/yoga/persisted/MyQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { getSchema } from "./../schema";
import { DocumentNode, execute } from "graphql";
const schema = getSchema();
const doc: DocumentNode = {
kind: "Document",
definitions: [
{
kind: "OperationDefinition",
operation: "query",
name: {
kind: "Name",
value: "MyQuery"
},
variableDefinitions: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "me"
},
arguments: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "groups"
},
arguments: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "name"
},
arguments: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "description"
},
arguments: [],
directives: [],
selectionSet: undefined
},
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "members"
},
arguments: [],
directives: [],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
alias: undefined,
name: {
kind: "Name",
value: "name"
},
arguments: [],
directives: [],
selectionSet: undefined
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
} as DocumentNode;
export function executeOperation(variables: any) {
return execute({ schema: schema, document: doc, variableValues: variables });
}
12 changes: 12 additions & 0 deletions examples/yoga/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query MyQuery {
me {
groups {
name {
description
members {
name
}
}
}
}
}
47 changes: 45 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { Location } from "graphql";
import { Location, parse } from "graphql";
import { getParsedTsConfig } from "./";
import {
SchemaAndDoc,
Expand All @@ -9,13 +9,15 @@ import {
} from "./lib";
import { Command } from "commander";
import { writeFileSync } from "fs";
import { resolve, dirname } from "path";
import { resolve, dirname, join } from "path";
import { version } from "../package.json";
import { locate } from "./Locate";
import { printGratsSDL, printExecutableSchema } from "./printSchema";
import * as ts from "typescript";
import { ReportableDiagnostics } from "./utils/DiagnosticError";
import { ConfigOptions, ParsedCommandLineGrats } from "./gratsConfig";
import * as fs from "fs";
import { queryCodegen } from "./queryCodegen";

const program = new Command();

Expand Down Expand Up @@ -63,6 +65,47 @@ program
console.log(formatLoc(loc.value));
});

program
.command("persist")
.argument("<OPERATION_TEXT>", "Text of the GraphQL operation to persist")
.option(
"--tsconfig <TSCONFIG>",
"Path to tsconfig.json. Defaults to auto-detecting based on the current working directory",
)
.action((operationText, { tsconfig }) => {
const { config, configPath } = getTsConfigOrReportAndExit(tsconfig);

const schemaAndDocResult = buildSchemaAndDocResult(config);
if (schemaAndDocResult.kind === "ERROR") {
console.error(
schemaAndDocResult.err.formatDiagnosticsWithColorAndContext(),
);
process.exit(1);
}

if (operationText === "-") {
operationText = fs.readFileSync(0, "utf-8");
}

const doc = parse(operationText, { noLocation: true });

if (doc.definitions.length !== 1) {
throw new Error("Expected exactly one definition in the document");
}
if (doc.definitions[0].kind !== "OperationDefinition") {
throw new Error("Expected the definition to be an operation");
}
const name = doc.definitions[0].name?.value;

const destDir = resolve(dirname(configPath), `./persisted`);
const dest = join(destDir, `${name}.ts`);
const result = queryCodegen(config.raw.grats, configPath, dest, doc);

fs.mkdirSync(destDir, { recursive: true });
writeFileSync(dest, result);
console.error(`Grats: Wrote TypeScript operation to \`${dest}\`.`);
});

program.parse();

/**
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function codegen(schema: GraphQLSchema, destination: string): string {
return codegen.print();
}

class Codegen {
export class Codegen {
_schema: GraphQLSchema;
_destination: string;
_imports: ts.Statement[] = [];
Expand Down
Loading