Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Add support for latest graphql-js (#108)
Browse files Browse the repository at this point in the history
- graphql-js 16.x updates two collection of constants to ts enums which
  affect this library.
- Changes needed to update the use the exported Kind &
  OperationTypeNode enums are backward incompatible so remove support
  for lower versions of graphql-js
  • Loading branch information
lorefnon authored Apr 15, 2022
1 parent e31ecb6 commit 2af2d46
Show file tree
Hide file tree
Showing 7 changed files with 1,161 additions and 483 deletions.
2 changes: 1 addition & 1 deletion codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"ast-types": "^0.14.2",
"fs-extra": "^9.0.1",
"graphql": "^15.3.0",
"graphql": "^16.3.0",
"jssha": "^3.2.0",
"node-fetch": "^2.6.1",
"outvariant": "^1.2.1",
Expand Down
78 changes: 62 additions & 16 deletions codegen/src/render.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { parse, buildSchema, visit, GraphQLEnumType } from "graphql";
import { parse, buildSchema, visit, GraphQLEnumType, Kind, OperationTypeNode } from "graphql";
import type { Code } from "ts-poet";
import prettier from "prettier";

import { typeTransform, selectorInterfaceTransform } from "./transforms";
import { printType } from "./utils";

interface ASTNode {
kind: string
}

export const render = (sdl: string): string => {
const ast = parse(sdl, { noLocation: true });
const schema = buildSchema(sdl);
Expand All @@ -15,9 +19,9 @@ export const render = (sdl: string): string => {
];

// additive transforms
const results: ReadonlyArray<{ definitions: Code[] }> = transforms.map(
const results = transforms.map(
(vistor) => visit(ast, vistor)
);
) as unknown as Array<{ readonly definitions: Code[] }> ;

const types = Object.values(schema.getTypeMap()).filter(
(type) => !type.name.startsWith("__")
Expand Down Expand Up @@ -47,31 +51,32 @@ export const render = (sdl: string): string => {

const source =
`
import { buildASTSchema } from 'graphql'
import { buildASTSchema, Kind, OperationTypeNode } from 'graphql'
import {
import {
TypeConditionError,
NamedType,
Field,
InlineFragment,
Argument,
Variable,
Selection,
SelectionSet,
SelectionBuilder,
Argument,
Variable,
Selection,
SelectionSet,
SelectionBuilder,
namedType,
field,
inlineFragment,
argument,
argument,
selectionSet
} from '@timkendall/tql'
export { Result, Variables, $ } from '@timkendall/tql'
export type { Result, Variables } from '@timkendall/tql'
export { $ } from '@timkendall/tql'
` +
`
export const SCHEMA = buildASTSchema(${JSON.stringify(ast)})
export const SCHEMA = buildASTSchema(${stringifyAST(ast)})
export const ENUMS = ${ENUMS}
${typeMap}
Expand All @@ -84,3 +89,44 @@ export const render = (sdl: string): string => {

return prettier.format(source, { parser: "typescript" });
};

const stringifyAST = (ast: ASTNode) => {
const acc: string[] = [];
accumulateASTNode(ast, acc)
return acc.join("\n")
}

const reverseRecord = <TRecord extends Record<string, string>>(input: TRecord) => Object.fromEntries(Object.entries(input).map(([k, v]) => [v, k]))

const kindRevMapping = reverseRecord(Kind)
const operationTypeRevMapping = reverseRecord(OperationTypeNode)

const accumulateASTNode = (
astNode: ASTNode,
acc: string[]
) => {
acc.push('{')
for (const [k,v] of Object.entries(astNode)) {
if (v === undefined) continue
acc.push(`${JSON.stringify(k)}: `)
if (Array.isArray(v)) {
acc.push(`[`)
for (const childNode of v) {
accumulateASTNode(childNode, acc)
acc.push(',')
}
acc.push(']')
} else if (typeof v === "object" && typeof v.kind === "string") {
accumulateASTNode(v, acc)
} else if (k === "kind" && kindRevMapping[v]) {
acc.push(`Kind.${kindRevMapping[v]}`)
} else if (k === "operation" && operationTypeRevMapping[v]) {
acc.push(`OperationTypeNode.${operationTypeRevMapping[v]}`)
} else {
acc.push(JSON.stringify(v))
}
acc.push(',')
}
acc.push('}')
}

Loading

1 comment on commit 2af2d46

@vercel
Copy link

@vercel vercel bot commented on 2af2d46 Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tql – ./

tql-git-master-timkendall.vercel.app
tql.dev
tql-timkendall.vercel.app

Please sign in to comment.