From 36d5abbca095b1153fd171709e191f0718e590bc Mon Sep 17 00:00:00 2001 From: Tim Kendall Date: Thu, 25 Nov 2021 14:25:10 -0800 Subject: [PATCH] Fix buildVariableDefinitions --- .github/workflows/ci.yml | 7 +++++-- src/AST.ts | 1 + src/Selection.ts | 1 - src/Variables.ts | 31 +++++++++++++++++-------------- src/__tests__/Variables.test.ts | 4 +--- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 788adf1..9b5277f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,10 @@ jobs: run_install: true - name: 'Build' - run: yarn build + run: pnpm build + + - name: 'Type Test' + run: pnpm type-test - name: 'Test' - run: yarn test \ No newline at end of file + run: pnpm test \ No newline at end of file diff --git a/src/AST.ts b/src/AST.ts index 333ff63..55b9ce0 100644 --- a/src/AST.ts +++ b/src/AST.ts @@ -251,6 +251,7 @@ export const document = >( definitions, }); +// @todo use the proper `astFromValue` export const toValueNode = (value: any, enums: any[] = []): ValueNode => { if (typeof value === "string") { if (enums.some((e) => Object.values(e).includes(value))) diff --git a/src/Selection.ts b/src/Selection.ts index 4b66e6e..b12b94a 100644 --- a/src/Selection.ts +++ b/src/Selection.ts @@ -79,7 +79,6 @@ export class Selection< const op = this.type.toLowerCase() as OperationTypeNode; const selectionSet = this.toSelectionSet(); const variableDefinitions = buildVariableDefinitions( - op, this.schema, selectionSet ); diff --git a/src/Variables.ts b/src/Variables.ts index 0188977..eb83b6e 100644 --- a/src/Variables.ts +++ b/src/Variables.ts @@ -1,6 +1,6 @@ import type { GraphQLSchema } from "graphql"; import { Kind, visitWithTypeInfo, TypeInfo, visit } from "graphql"; -import { O, U } from "ts-toolbelt"; +import type { O, U } from "ts-toolbelt"; import { SelectionSet, @@ -13,41 +13,44 @@ import { variable, variableDefinition, NamedType, + operation, } from "./AST"; export const $ = (name: Name): Variable => variable(name); export const buildVariableDefinitions = >( - op: "query" | "mutation" | "subscription", schema: GraphQLSchema, selectionSet: T ): Array> => { const variableDefinitions: VariableDefinition[] = []; const typeInfo = new TypeInfo(schema); + + // @note need to wrap selectionset in operation for TypeInfo to track correctly + const operationDefinition = operation( + "query", + "", + selectionSet, + variableDefinitions + ); + const visitor = visitWithTypeInfo(typeInfo, { - [Kind.ARGUMENT]: (argNode) => { - if (isVariable(argNode.value) && typeInfo.getArgument()?.astNode?.type) { + [Kind.ARGUMENT]: (node) => { + const type = typeInfo.getArgument()?.astNode?.type!; + + if (node.value.kind === "Variable") { // define the `VariableDefinition` - variableDefinitions.push( - variableDefinition( - argNode.value, - typeInfo.getArgument()?.astNode?.type! - ) - ); + variableDefinitions.push(variableDefinition(node.value, type)); } }, }); // @todo return from here - visit(selectionSet, visitor); + visit(operationDefinition, visitor); return variableDefinitions; }; -export const isVariable = (value: any): value is Variable => - typeof value === "object" && value.kind === Kind.VARIABLE; - // @note Traverse the AST extracting `Argument` nodes w/ values of `Variable`. // Extract the type the Variable value needs to be against/the Schema. export type Variables< diff --git a/src/__tests__/Variables.test.ts b/src/__tests__/Variables.test.ts index f5ae26d..7e56b12 100644 --- a/src/__tests__/Variables.test.ts +++ b/src/__tests__/Variables.test.ts @@ -29,7 +29,6 @@ describe("Variables", () => { // @note we need a way to get the input type at runtime const variableDefinitions = buildVariableDefinitions( - "query", schema, selectionSet(selection) ); @@ -48,7 +47,7 @@ describe("Variables", () => { type User { id: ID! - friends(limit: Int = 10): [User!] + friends(limit: Int!): [User!] } `, { noLocation: true } @@ -69,7 +68,6 @@ describe("Variables", () => { ]); const variableDefinitions = buildVariableDefinitions( - "query", schema, selectionSet(selection) );