Skip to content

Commit 16b4f90

Browse files
committed
More progress
1 parent 8522730 commit 16b4f90

20 files changed

+80
-151
lines changed

babel.config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
"@babel/preset-env",
3+
["@babel/preset-env", { "targets": { "node": "12" } }],
44
"@babel/preset-react",
55
"@babel/preset-typescript"
66
],

packages/compiler/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"find-pkg-json-field-up": "^1.0.1",
2222
"fs-extra": "^9.0.0",
2323
"globby": "^11.0.0",
24-
"invariant": "^2.2.4"
24+
"invariant": "^2.2.4",
25+
"slash": "^3.0.0",
26+
"strip-ansi": "^6.0.0"
2527
},
2628
"peerDependencies": {
2729
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"

packages/compiler/src/get-generated-types.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from "fs-extra";
2-
import path from "path";
2+
import nodePath from "path";
33
import {
44
DocumentNode,
55
OperationDefinitionNode,
@@ -18,6 +18,8 @@ import {
1818
PluginObj,
1919
} from "@babel/core";
2020
import { TaggedTemplateExpression } from "@babel/types";
21+
import stripAnsi from "strip-ansi";
22+
import slash from "slash";
2123
import globby from "globby";
2224
import { cachedGenerateSchemaTypes } from "./schema-types";
2325
import { cachedGenerateOperationTypes } from "./operation-types";
@@ -34,8 +36,8 @@ export const getGeneratedTypes = async ({
3436
schema: GraphQLSchema;
3537
directory: string;
3638
}) => {
37-
let generatedDirectory = path.join(
38-
path.join(directory, "__generated__", "ts-gql")
39+
let generatedDirectory = nodePath.join(
40+
nodePath.join(directory, "__generated__", "ts-gql")
3941
);
4042

4143
const files = await globby(["**/*.{ts,tsx}"], {
@@ -46,6 +48,7 @@ export const getGeneratedTypes = async ({
4648
let nodeMap: Record<
4749
string,
4850
{
51+
filename: string;
4952
makeFrameError: (error: GraphQLError) => string;
5053
nodes:
5154
| readonly [OperationDefinitionNode, ...FragmentDefinitionNode[]]
@@ -99,6 +102,7 @@ export const getGeneratedTypes = async ({
99102
);
100103
}
101104
nodeMap[val] = {
105+
filename: slash(nodePath.relative(directory, file)),
102106
makeFrameError: (error) => {
103107
let loc = locFrom(path.node, error);
104108
if (loc) {
@@ -148,7 +152,7 @@ export const getGeneratedTypes = async ({
148152
if (nodeMap[name] === undefined) {
149153
fsOperations.push({
150154
type: "remove",
151-
filename: path.join(generatedDirectory, name + ".ts"),
155+
filename: nodePath.join(generatedDirectory, name + ".ts"),
152156
});
153157
}
154158
}
@@ -203,22 +207,26 @@ export const getGeneratedTypes = async ({
203207
nodes[0].kind === "OperationDefinition"
204208
? specifiedRules
205209
: fragmentDocumentRules
210+
).map((err) =>
211+
// TODO: make this better
212+
nodeMap[key].makeFrameError(err)
206213
);
207214

208215
if (gqlErrors.length) {
209-
// TODO: make this better
210-
errors.push(
211-
...gqlErrors.map((err) => nodeMap[key].makeFrameError(err))
212-
);
216+
errors.push(...gqlErrors);
213217
}
214218
let operation = await cachedGenerateOperationTypes(
215219
schema,
216220
document,
217221
nodes[0],
218-
path.join(generatedDirectory, `${nodes[0].name!.value}.ts`),
222+
nodePath.join(generatedDirectory, `${nodes[0].name!.value}.ts`),
219223
schemaHash,
220224
nodes[0].name!.value,
221-
gqlErrors.length === 0
225+
gqlErrors.length
226+
? `${nodeMap[key].filename}\nThere ${
227+
gqlErrors.length === 1 ? "is an error" : "are errors"
228+
} with ${nodes[0].name!.value}\n${stripAnsi(gqlErrors.join("\n"))}`
229+
: undefined
222230
);
223231
if (operation) fsOperations.push(operation);
224232
})

packages/compiler/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { getGeneratedTypes } from "./get-generated-types";
2+
export { watch } from "./watch";

packages/compiler/src/operation-types.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ async function generateOperationTypes(
1919
filename: string,
2020
operationHash: string,
2121
operationName: string,
22-
isValid: boolean
22+
error: string | undefined
2323
): Promise<FsOperation> {
24-
if (!isValid) {
24+
if (error) {
2525
return {
2626
type: "output",
2727
filename,
@@ -31,11 +31,11 @@ async function generateOperationTypes(
3131
},
3232
null,
3333
2
34-
)}\nts-gql-meta-end\n*/\n\n
35-
36-
export type type = never
37-
38-
throw new Error("There is an error in the types of ${operationName}")
34+
)}\nts-gql-meta-end\n*/
35+
36+
export type type = never;
37+
38+
throw new Error(${JSON.stringify(error)});
3939
`,
4040
};
4141
}
@@ -106,9 +106,11 @@ export async function cachedGenerateOperationTypes(
106106
filename: string,
107107
schemaHash: string,
108108
operationName: string,
109-
isValid: boolean
109+
error: string | undefined
110110
) {
111-
let operationHash = hashString(schemaHash + JSON.stringify(operation) + "v3");
111+
let operationHash = hashString(
112+
schemaHash + JSON.stringify(operation) + error || "" + "v3"
113+
);
112114
let types: string;
113115
try {
114116
types = await fs.readFile(filename, "utf8");
@@ -121,7 +123,7 @@ export async function cachedGenerateOperationTypes(
121123
filename,
122124
operationHash,
123125
operationName,
124-
isValid
126+
error
125127
);
126128
}
127129
throw err;
@@ -135,7 +137,7 @@ export async function cachedGenerateOperationTypes(
135137
filename,
136138
operationHash,
137139
operationName,
138-
isValid
140+
error
139141
);
140142
}
141143
}

packages/compiler/src/watch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import chokidar from "chokidar";
22
import { getRawConfig } from "./config";
33
import { createWatcher } from "./watcher";
4-
import { getGeneratedTypes } from "../dist/compiler.cjs";
4+
import { getGeneratedTypes } from "./get-generated-types";
55
import { getSchemaFromOptions } from "./get-schema";
6-
import { applyFsOperation, FsOperation } from "./fs-operations";
6+
import { applyFsOperation } from "./fs-operations";
77

88
// TODO: handle changes incrementally
99
export const watch = async (cwd: string) => {

packages/next/src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { watch } from "@ts-gql/compiler";
2+
3+
export const withTsGql = (internalConfig: any = {}) => (
4+
phase:
5+
| "phase-export"
6+
| "phase-production-build"
7+
| "phase-production-server"
8+
| "phase-development-server",
9+
thing: any
10+
) => {
11+
if (phase === "phase-development-server") {
12+
watch(process.cwd());
13+
}
14+
return typeof internalConfig === "function"
15+
? internalConfig(phase, thing)
16+
: internalConfig;
17+
};

pnpm-lock.yaml

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/__generated__/ts-gql/MyQueryApollo.ts

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/__generated__/ts-gql/MyQueryUrql.ts

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/__generated__/ts-gql/SomeMutationApollo.ts

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/__generated__/ts-gql/SomeMutationUrql.ts

+4-100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-app/__generated__/ts-gql/SomeQueryApollo.ts

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)