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

Commit

Permalink
Fix buildVariableDefinitions
Browse files Browse the repository at this point in the history
  • Loading branch information
timkendall committed Nov 25, 2021
1 parent 61f15c1 commit 36d5abb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
run: pnpm test
1 change: 1 addition & 0 deletions src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export const document = <T extends ReadonlyArray<Definition>>(
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)))
Expand Down
1 change: 0 additions & 1 deletion src/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
31 changes: 17 additions & 14 deletions src/Variables.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,41 +13,44 @@ import {
variable,
variableDefinition,
NamedType,
operation,
} from "./AST";

export const $ = <Name extends string>(name: Name): Variable<Name> =>
variable(name);

export const buildVariableDefinitions = <T extends SelectionSet<any>>(
op: "query" | "mutation" | "subscription",
schema: GraphQLSchema,
selectionSet: T
): Array<VariableDefinition<any, any>> => {
const variableDefinitions: VariableDefinition<any, any>[] = [];
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<string> =>
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<
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/Variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -48,7 +47,7 @@ describe("Variables", () => {
type User {
id: ID!
friends(limit: Int = 10): [User!]
friends(limit: Int!): [User!]
}
`,
{ noLocation: true }
Expand All @@ -69,7 +68,6 @@ describe("Variables", () => {
]);

const variableDefinitions = buildVariableDefinitions(
"query",
schema,
selectionSet(selection)
);
Expand Down

0 comments on commit 36d5abb

Please sign in to comment.