Skip to content

Commit

Permalink
Support graphql@16
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Apr 22, 2022
1 parent b7a7c81 commit 0e3e2f5
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 1,186 deletions.
10 changes: 10 additions & 0 deletions .changeset/orange-dodos-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@ts-gql/apollo": patch
"@ts-gql/compiler": patch
"@ts-gql/config": patch
"@ts-gql/eslint-plugin": patch
"@ts-gql/next": patch
"@ts-gql/tag": patch
---

Support `graphql@16`
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"babel-jest": "^25.4.0",
"babel-plugin-macros": "^2.8.0",
"eslint": "^6.8.0",
"graphql": "^15.7.2",
"graphql-tag": "^2.12.5",
"graphql": "^16.3.0",
"jest": "^25.4.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@apollo/client": "^3.1.2",
"graphql": "^15.7.2",
"graphql": "^16.3.0",
"react": "^16.14.0"
},
"repository": "https://github.com/Thinkmill/ts-gql/tree/main/packages/apollo"
Expand Down
12 changes: 6 additions & 6 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"@babel/parser": "^7.9.6",
"@babel/runtime": "^7.9.2",
"@babel/types": "^7.9.6",
"@graphql-codegen/plugin-helpers": "^1.18.8",
"@graphql-codegen/typescript": "^1.23.0",
"@graphql-codegen/typescript-operations": "^1.18.4",
"@graphql-codegen/plugin-helpers": "^2.4.2",
"@graphql-codegen/typescript": "^2.4.8",
"@graphql-codegen/typescript-operations": "^2.3.5",
"@nodelib/fs.walk": "^1.2.4",
"@ts-gql/config": "^0.9.0",
"@types/babel__code-frame": "^7.0.1",
Expand All @@ -33,12 +33,12 @@
"strip-ansi": "^6.0.0"
},
"peerDependencies": {
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || 14 || 15 || 16"
},
"devDependencies": {
"@ts-gql/tag": "*",
"fixturez": "^1.1.0",
"graphql": "^15.7.2",
"graphql": "^16.3.0",
"lazy-require.macro": "^0.1.0"
},
"preconstruct": {
Expand All @@ -48,4 +48,4 @@
]
},
"repository": "https://github.com/Thinkmill/ts-gql/tree/main/packages/compiler"
}
}
80 changes: 61 additions & 19 deletions packages/compiler/src/get-generated-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from "./fs";
import nodePath from "path";
import type { FragmentDefinitionNode } from "graphql";
import { GraphQLError } from "graphql/error/GraphQLError";
import { visit } from "graphql/language/visitor";
import { lazyRequire } from "lazy-require.macro";
Expand All @@ -19,14 +18,15 @@ import { Config } from "@ts-gql/config";
import { promisify } from "util";
import { CompilerError, TSGQLDocument } from "./types";
import { cachedGenerateIntrospectionResult } from "./introspection-result";
import { locFromSourceAndGraphQLError, hashString } from "./utils";
import { locFromSourceAndGraphQLError, hashString, resolvable } from "./utils";
import { getDocuments } from "./get-documents";
import {
validateDocument,
readDocumentValidationCache,
writeDocumentValidationCache,
DocumentValidationCache,
} from "./validate-documents";
import { DocumentNode } from "graphql";

function memoize<V>(fn: (arg: string) => V): (arg: string) => V {
const cache: { [key: string]: V } = {};
Expand All @@ -38,9 +38,22 @@ function memoize<V>(fn: (arg: string) => V): (arg: string) => V {

const walk = promisify(_walk);

function weakMemoize<Arg extends object, Return>(
fn: (arg: Arg) => Return
): (arg: Arg) => Return {
const cache = new WeakMap<Arg, Return>();
return (arg: Arg) => {
if (cache.has(arg)) return cache.get(arg)!;
const result = fn(arg);
cache.set(arg, result);
return result;
};
}

function getPrintCompilerError() {
let readFile = memoize((filename: string) => fs.readFile(filename, "utf8"));
return async (error: CompilerError) => {

return weakMemoize(async (error: CompilerError) => {
const { codeFrameColumns } =
lazyRequire<typeof import("@babel/code-frame")>();
let content = await readFile(error.filename);
Expand All @@ -57,11 +70,9 @@ function getPrintCompilerError() {
})
: error.message)
);
};
});
}

type Dependencies = string | Dependencies[];

let tsExtensionRegex = /\.tsx?$/;

let dtsExtensionRegex = /\.d\.ts$/;
Expand Down Expand Up @@ -198,15 +209,26 @@ export const getGeneratedTypes = async (
const dependencies = getDependencies(uniqueDocumentsByName, errors);
const documentValidationCache = await readDocumentValidationCache(config);
const newDocumentValidationCache: DocumentValidationCache = {};

const hasValidated = resolvable<void>();
const nodeNames = Object.keys(uniqueDocumentsByName);
let i = 0;
let validate = () => {
i++;
if (i === nodeNames.length) {
hasValidated.resolve();
}
return hasValidated;
};
const operationHashByName: Record<string, string> = {};

await Promise.all(
Object.keys(uniqueDocumentsByName).map(async (key) => {
nodeNames.map(async (key) => {
const documentInfo = uniqueDocumentsByName[key];
let nodes = [...new Set(dependencies[key].flat(Infinity))].map(
(x) => uniqueDocumentsByName[x].node as FragmentDefinitionNode
);
let nodes = dependencies[key].map((x) => uniqueDocumentsByName[x].node);

let document = {
kind: "Document",
kind: "Document" as DocumentNode["kind"],
definitions: nodes,
} as const;
let operationHash = hashString(
Expand All @@ -227,20 +249,32 @@ export const getGeneratedTypes = async (
}
newDocumentValidationCache[operationHash] =
documentValidationCache[operationHash];
operationHashByName[key] = operationHash;
const gqlErrors = documentValidationCache[operationHash];
errors.push(...gqlErrors);

await validate();
let filename = nodePath.join(
generatedDirectory,
`${nodes[0].name.value}.ts`
);
let operation = gqlErrors.length
const allGraphQLErrorsForDocument = new Set<CompilerError>(gqlErrors);
for (const node of nodes) {
const errors =
newDocumentValidationCache[operationHashByName[node.name.value]];
for (const error of errors) {
allGraphQLErrorsForDocument.add(error);
}
}

let operation = allGraphQLErrorsForDocument.size
? await cachedGenerateErrorModuleFsOperation(
filename,
`${documentInfo.filename}\nThere ${
gqlErrors.length === 1 ? "is an error" : "are errors"
} with ${nodes[0].name.value}\n${(
await Promise.all(errors.map((x) => printCompilerError(x)))
`\n${(
await Promise.all(
[...allGraphQLErrorsForDocument].map((x) =>
printCompilerError(x)
)
)
).join("\n")}`
)
: await cachedGenerateOperationTypes(
Expand All @@ -255,10 +289,14 @@ export const getGeneratedTypes = async (
await writeDocumentValidationCache(config, newDocumentValidationCache);
return {
fsOperations,
errors: await Promise.all(errors.map((x) => printCompilerError(x))),
errors: await Promise.all(
[...new Set(errors)].map((x) => printCompilerError(x))
),
};
};

type Dependencies = string | Dependencies[];

function getDependencies(
uniqueDocumentsByName: Record<string, TSGQLDocument>,
errors: CompilerError[]
Expand Down Expand Up @@ -291,5 +329,9 @@ function getDependencies(
},
});
}
return dependencies;
return Object.fromEntries(
Object.entries(dependencies).map(([name, deps]) => {
return [name, deps.flat(Infinity)];
})
);
}
7 changes: 5 additions & 2 deletions packages/compiler/src/inline-fragments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ export function inlineIntoFirstOperationOrFragment(
throw new Error("unexpected non-number key");
}
let inlineSpread: InlineFragmentNode = {
kind: "InlineFragment",
kind: "InlineFragment" as InlineFragmentNode["kind"],
selectionSet: fragment.selectionSet,
typeCondition: fragment.typeCondition,
};
parent[key] = inlineSpread;
},
});
let newDocument = { kind: "Document", definitions: [firstNode] } as const;
let newDocument = {
kind: "Document" as DocumentNode["kind"],
definitions: [firstNode],
} as const;

removeUnnecessaryFragmentSpreads(newDocument, schema);

Expand Down
Loading

0 comments on commit 0e3e2f5

Please sign in to comment.