Skip to content

Commit

Permalink
GraphQL v15 support for new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Jun 24, 2024
1 parent 5586b8c commit 3283e6f
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
specifiedRules,
validate,
validateSchema,
version as graphqlVersion,
visit,
visitInParallel,
visitWithTypeInfo,
Expand All @@ -31,6 +32,8 @@ import { OperationPathsVisitor } from "./OperationPathsVisitor.js";
import type { RuleError } from "./ruleError.js";
import { RulesContext } from "./rulesContext.js";

const graphqlVersionMajor = parseInt(graphqlVersion.split(".")[0], 10);

if (isMainThread) {
throw new Error(
"This script is designed to be called by `scan.ts`, but it's the main thread",
Expand Down Expand Up @@ -111,19 +114,30 @@ async function main() {
config,
onError,
);

const baseValidationRules = [
...specifiedRules,
const baseValidationRules = [...specifiedRules];
const mode =
graphqlVersionMajor === 15 ? 1 : graphqlVersionMajor === 16 ? 2 : 0;
if (mode > 0) {
// We need to run this so we know what the operation path/operation names are for rule errors.
() => OperationPathsVisitor(rulesContext),
];
const validationErrors = validate(
schema,
document,
baseValidationRules,
{},
typeInfo,
);
baseValidationRules.push(() => OperationPathsVisitor(rulesContext));
}

const validationErrors =
mode === 1
? // GraphQL v15 style
validate(
schema,
document,
baseValidationRules,
typeInfo as any,

Check failure on line 132 in src/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
{} as any,

Check failure on line 133 in src/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
)
: mode === 2
? // GraphQL v16 style
validate(schema, document, baseValidationRules, {}, typeInfo)
: // GraphQL v17 MIGHT remove typeInfo
validate(schema, document, baseValidationRules);

if (validationErrors.length > 0) {
return {
sourceName,
Expand All @@ -137,6 +151,17 @@ async function main() {
};
}

if (mode === 0) {
// Need to revisit
visit(
document,
visitWithTypeInfo(
rulesContext.getTypeInfo(),
visitInParallel([OperationPathsVisitor(rulesContext)]),
),
);
}

const visitors = await middleware.run(
"visitors",
{ rulesContext, visitors: [DepthVisitor(rulesContext)] },
Expand Down

0 comments on commit 3283e6f

Please sign in to comment.