Changes to ease incremental supermassive integration (#289)

* Allow optional schemaResolvers
* expose printed version of implicit types in supermassive-extractors
* Support conditional annotations in `graphql-codegen-supermassive-typed-document-node-plugin`
This commit is contained in:
Vladimir Razuvaev 2023-04-04 12:16:21 +02:00 коммит произвёл GitHub
Родитель b09d46863c
Коммит 05c91eddd4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 92 добавлений и 18 удалений

3
.gitattributes поставляемый
Просмотреть файл

@ -1 +1,4 @@
/.yarn/** linguist-vendored
* text=auto eol=lf
yarn.lock text eol=lf

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Support conditional annotations in `graphql-codegen-supermassive-typed-document-node-plugin`",
"packageName": "@graphitation/graphql-codegen-supermassive-typed-document-node-plugin",
"email": "vladimir.razuvaev@gmail.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Make execution argument `schemaResolvers` optional",
"packageName": "@graphitation/supermassive",
"email": "vladimir.razuvaev@gmail.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Expose printed TS version of extracted implicit types",
"packageName": "@graphitation/supermassive-extractors",
"email": "vladimir.razuvaev@gmail.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -27,14 +27,21 @@ import {
import { optimizeDocumentNode } from "@graphql-tools/optimize";
import gqlTag from "graphql-tag";
type RawClientSidePluginConfig = RawClientSideBasePluginConfig & {
supermassiveDocumentNodeConditional?: string;
};
type ClientSidePluginConfig = ClientSideBasePluginConfig & {
supermassiveDocumentNodeConditional?: string;
};
export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
RawClientSideBasePluginConfig,
ClientSideBasePluginConfig
RawClientSidePluginConfig,
ClientSidePluginConfig
> {
constructor(
schema: GraphQLSchema,
fragments: LoadedFragment[],
rawConfig: RawClientSideBasePluginConfig,
rawConfig: RawClientSidePluginConfig,
documents: Types.DocumentFile[],
) {
super(
@ -46,7 +53,10 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
"@graphql-typed-document-node/core#TypedDocumentNode",
...rawConfig,
},
{},
{
supermassiveDocumentNodeConditional:
rawConfig.supermassiveDocumentNodeConditional,
},
documents,
);
@ -68,6 +78,18 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
protected _gql(
node: FragmentDefinitionNode | OperationDefinitionNode,
): string {
if (this.config.supermassiveDocumentNodeConditional) {
const supermassive = this._render(node, true);
const standard = this._render(node, false);
return `(${this.config.supermassiveDocumentNodeConditional}\n? ${supermassive}\n: ${standard})`;
}
return this._render(node, true);
}
protected _render(
node: FragmentDefinitionNode | OperationDefinitionNode,
annotate = false,
): string {
const supermassiveNode = addTypesToRequestDocument(this._schema, {
kind: Kind.DOCUMENT,
@ -90,8 +112,11 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
if (this.config.optimizeDocumentNode) {
gqlObj = optimizeDocumentNode(gqlObj);
}
if (annotate) {
gqlObj = this._transformDocumentNodeToSupermassive(gqlObj);
}
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
return JSON.stringify(gqlObj);
} else if (
this.config.documentMode === DocumentMode.documentNodeImportFragments
) {
@ -105,10 +130,12 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
const definitions = [
...gqlObj.definitions.map((t) =>
JSON.stringify(
addTypesToRequestDocument(this._schema, {
kind: Kind.DOCUMENT,
definitions: [t],
}).definitions[0],
annotate
? addTypesToRequestDocument(this._schema, {
kind: Kind.DOCUMENT,
definitions: [t],
}).definitions[0]
: t,
),
),
...fragments.map((name) => `...${name}.definitions`),
@ -116,8 +143,11 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
return `{"kind":"${Kind.DOCUMENT}","definitions":[${definitions}]}`;
}
if (annotate) {
gqlObj = this._transformDocumentNodeToSupermassive(gqlObj);
}
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
return JSON.stringify(gqlObj);
} else if (this.config.documentMode === DocumentMode.string) {
return "`" + doc + "`";
}
@ -137,7 +167,7 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<
definitions: [t],
}).definitions[0],
),
};
} as DocumentNode;
}
protected getDocumentNodeSignature(
resultType: string,

Просмотреть файл

@ -2,11 +2,14 @@ import * as fs from "fs";
import * as path from "path";
import ts from "typescript";
import { parse } from "graphql";
import { extractImplicitTypesToTypescript } from "../extractImplicitTypesToTypescript";
import {
extractAndPrintImplicitTypesToTypescript,
extractImplicitTypesToTypescript,
} from "../extractImplicitTypesToTypescript";
describe(extractImplicitTypesToTypescript, () => {
it("benchmark schema extract", () => {
expect.assertions(1);
expect.assertions(2);
const typeDefs = fs.readFileSync(
path.join(
__dirname,
@ -16,13 +19,16 @@ describe(extractImplicitTypesToTypescript, () => {
encoding: "utf-8",
},
);
const sourceFile = extractImplicitTypesToTypescript(parse(typeDefs));
const document = parse(typeDefs);
const sourceFile = extractImplicitTypesToTypescript(document);
const printer = ts.createPrinter();
const printedSource = printer.printNode(
ts.EmitHint.SourceFile,
sourceFile,
sourceFile,
);
const printed = extractAndPrintImplicitTypesToTypescript(document);
expect(printedSource).toMatchSnapshot();
expect(printed).toEqual(printedSource);
});
});

Просмотреть файл

@ -197,6 +197,13 @@ export function extractImplicitTypesToTypescript(
);
}
export function extractAndPrintImplicitTypesToTypescript(
document: DocumentNode,
): string {
const file = extractImplicitTypesToTypescript(document);
return ts.createPrinter().printFile(file);
}
function createDeclaration(
name: string,
decl: ts.Expression,

Просмотреть файл

@ -1 +1,4 @@
export { extractImplicitTypesToTypescript } from "./extractImplicitTypesToTypescript";
export {
extractImplicitTypesToTypescript,
extractAndPrintImplicitTypesToTypescript,
} from "./extractImplicitTypesToTypescript";

Просмотреть файл

@ -123,7 +123,9 @@ export function executeWithoutSchema(
fieldExecutionHooks,
} = args;
const combinedResolvers = mergeResolvers(resolvers, schemaResolvers);
const combinedResolvers = schemaResolvers
? mergeResolvers(resolvers, schemaResolvers)
: resolvers;
// If arguments are missing or incorrect, throw an error.
assertValidExecutionArguments(document, variableValues);

Просмотреть файл

@ -71,7 +71,9 @@ export async function subscribeWithoutSchema(
subscribeFieldResolver,
} = args;
const combinedResolvers = mergeResolvers(resolvers, schemaResolvers);
const combinedResolvers = schemaResolvers
? mergeResolvers(resolvers, schemaResolvers)
: resolvers;
const resultOrStream = await createSourceEventStream(
combinedResolvers,

Просмотреть файл

@ -167,7 +167,7 @@ export interface CommonExecutionArgs {
}
export type ExecutionWithoutSchemaArgs = CommonExecutionArgs & {
document: DocumentNode;
schemaResolvers: Resolvers;
schemaResolvers?: Resolvers;
};
export type ExecutionWithSchemaArgs = CommonExecutionArgs & {