Skip to content

Commit 11a1172

Browse files
committed
Explore persisting query
1 parent 0777cde commit 11a1172

File tree

4 files changed

+375
-2
lines changed

4 files changed

+375
-2
lines changed

examples/yoga/persisted/MyQuery.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { getSchema } from "./../schema";
2+
import { DocumentNode, execute } from "graphql";
3+
const schema = getSchema();
4+
const doc: DocumentNode = {
5+
kind: "Document",
6+
definitions: [
7+
{
8+
kind: "OperationDefinition",
9+
operation: "query",
10+
name: {
11+
kind: "Name",
12+
value: "MyQuery"
13+
},
14+
variableDefinitions: [],
15+
directives: [],
16+
selectionSet: {
17+
kind: "SelectionSet",
18+
selections: [
19+
{
20+
kind: "Field",
21+
alias: undefined,
22+
name: {
23+
kind: "Name",
24+
value: "me"
25+
},
26+
arguments: [],
27+
directives: [],
28+
selectionSet: {
29+
kind: "SelectionSet",
30+
selections: [
31+
{
32+
kind: "Field",
33+
alias: undefined,
34+
name: {
35+
kind: "Name",
36+
value: "groups"
37+
},
38+
arguments: [],
39+
directives: [],
40+
selectionSet: {
41+
kind: "SelectionSet",
42+
selections: [
43+
{
44+
kind: "Field",
45+
alias: undefined,
46+
name: {
47+
kind: "Name",
48+
value: "name"
49+
},
50+
arguments: [],
51+
directives: [],
52+
selectionSet: {
53+
kind: "SelectionSet",
54+
selections: [
55+
{
56+
kind: "Field",
57+
alias: undefined,
58+
name: {
59+
kind: "Name",
60+
value: "description"
61+
},
62+
arguments: [],
63+
directives: [],
64+
selectionSet: undefined
65+
},
66+
{
67+
kind: "Field",
68+
alias: undefined,
69+
name: {
70+
kind: "Name",
71+
value: "members"
72+
},
73+
arguments: [],
74+
directives: [],
75+
selectionSet: {
76+
kind: "SelectionSet",
77+
selections: [
78+
{
79+
kind: "Field",
80+
alias: undefined,
81+
name: {
82+
kind: "Name",
83+
value: "name"
84+
},
85+
arguments: [],
86+
directives: [],
87+
selectionSet: undefined
88+
}
89+
]
90+
}
91+
}
92+
]
93+
}
94+
}
95+
]
96+
}
97+
}
98+
]
99+
}
100+
}
101+
]
102+
}
103+
}
104+
]
105+
} as DocumentNode;
106+
export function executeOperation() {
107+
return execute({ schema: schema, document: doc });
108+
}

examples/yoga/query.graphql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
query MyQuery {
2+
me {
3+
groups {
4+
name {
5+
description
6+
members {
7+
name
8+
}
9+
}
10+
}
11+
}
12+
}

src/cli.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env node
22

3-
import { GraphQLSchema, Location, lexicographicSortSchema } from "graphql";
3+
import {
4+
GraphQLSchema,
5+
Location,
6+
lexicographicSortSchema,
7+
parse,
8+
} from "graphql";
49
import { getParsedTsConfig } from "./";
510
import {
611
ConfigOptions,
@@ -10,12 +15,14 @@ import {
1015
} from "./lib";
1116
import { Command } from "commander";
1217
import { writeFileSync } from "fs";
13-
import { resolve, dirname } from "path";
18+
import { resolve, dirname, join } from "path";
1419
import { version } from "../package.json";
1520
import { locate } from "./Locate";
1621
import { printGratsSDL, printExecutableSchema } from "./printSchema";
1722
import * as ts from "typescript";
1823
import { ReportableDiagnostics } from "./utils/DiagnosticError";
24+
import * as fs from "fs";
25+
import { queryCodegen } from "./queryCodegen";
1926

2027
const program = new Command();
2128

@@ -60,6 +67,45 @@ program
6067
console.log(formatLoc(loc.value));
6168
});
6269

70+
program
71+
.command("persist")
72+
.argument("<OPERATION_TEXT>", "Text of the GraphQL operation to persist")
73+
.option(
74+
"--tsconfig <TSCONFIG>",
75+
"Path to tsconfig.json. Defaults to auto-detecting based on the current working directory",
76+
)
77+
.action((operationText, { tsconfig }) => {
78+
const { config, configPath } = getTsConfigOrReportAndExit(tsconfig);
79+
80+
const schemaResult = buildSchemaResult(config);
81+
if (schemaResult.kind === "ERROR") {
82+
console.error(schemaResult.err.formatDiagnosticsWithColorAndContext());
83+
process.exit(1);
84+
}
85+
86+
if (operationText === "-") {
87+
operationText = fs.readFileSync(process.stdin.fd, "utf-8");
88+
}
89+
90+
const doc = parse(operationText, { noLocation: true });
91+
92+
if (doc.definitions.length !== 1) {
93+
throw new Error("Expected exactly one definition in the document");
94+
}
95+
if (doc.definitions[0].kind !== "OperationDefinition") {
96+
throw new Error("Expected the definition to be an operation");
97+
}
98+
const name = doc.definitions[0].name?.value;
99+
100+
const destDir = resolve(dirname(configPath), `./persisted`);
101+
const dest = join(destDir, `${name}.ts`);
102+
const result = queryCodegen(config.raw.grats, configPath, dest, doc);
103+
104+
fs.mkdirSync(destDir, { recursive: true });
105+
writeFileSync(dest, result);
106+
console.error(`Grats: Wrote TypeScript operation to \`${dest}\`.`);
107+
});
108+
63109
program.parse();
64110

65111
/**

0 commit comments

Comments
 (0)