-
-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for forbidden-cross-optimize
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
process.env.GRAPHILE_ENV = "test"; | ||
/* eslint-disable graphile-export/exhaustive-deps, graphile-export/export-methods, graphile-export/export-instances, graphile-export/export-subclasses, graphile-export/no-nested */ | ||
import { expect } from "chai"; | ||
import type { AsyncExecutionResult, ExecutionResult } from "graphql"; | ||
import { it } from "mocha"; | ||
|
||
import type { PromiseOrDirect } from "../dist/index.js"; | ||
import { | ||
arrayOfLength, | ||
constant, | ||
ExecutableStep, | ||
grafast, | ||
lambda, | ||
makeGrafastSchema, | ||
} from "../dist/index.js"; | ||
|
||
class SomeStep extends ExecutableStep { | ||
deduplicate( | ||
_peers: readonly ExecutableStep<any>[], | ||
): readonly ExecutableStep<any>[] { | ||
return _peers; | ||
} | ||
deduplicatedWith(replacement: ExecutableStep<any>): void {} | ||
optimize() { | ||
return this; | ||
} | ||
finalize(): void { | ||
super.finalize(); | ||
} | ||
async execute(l: number) { | ||
return arrayOfLength(l, 42); | ||
} | ||
} | ||
|
||
class BadOptimizeStep extends ExecutableStep { | ||
constructor($parent: ExecutableStep) { | ||
super(); | ||
this.addDependency($parent); | ||
} | ||
optimize() { | ||
const $parent = this.getDep(0); | ||
$parent.optimize?.({ meta: {}, stream: null }); | ||
return this; | ||
} | ||
async execute(l: number) { | ||
return arrayOfLength(l, 42); | ||
} | ||
} | ||
|
||
class BadFinalizeStep extends ExecutableStep { | ||
constructor($parent: ExecutableStep) { | ||
super(); | ||
this.addDependency($parent); | ||
} | ||
finalize() { | ||
const $parent = this.getDep(0); | ||
$parent.finalize(); | ||
return this; | ||
} | ||
execute(l: number) { | ||
return arrayOfLength(l, 42); | ||
} | ||
} | ||
|
||
const schema = makeGrafastSchema({ | ||
typeDefs: ` | ||
type Query { | ||
badOptimize: Int | ||
badFinalize: Int | ||
} | ||
`, | ||
plans: { | ||
Query: { | ||
badOptimize() { | ||
return new BadOptimizeStep(new SomeStep()); | ||
}, | ||
badFinalize() { | ||
return new BadFinalizeStep(new SomeStep()); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
it("forbids calling another step's optimize method", async () => { | ||
const result = (await grafast({ | ||
schema, | ||
source: `{badOptimize}`, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.exist; | ||
expect(result.errors).to.have.length(1); | ||
expect(result.errors![0].message).to.match(/Only Grafast.*optimize method;/); | ||
}); | ||
|
||
it("forbids calling another step's finalize method", async () => { | ||
const result = (await grafast({ | ||
schema, | ||
source: `{badFinalize}`, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.exist; | ||
expect(result.errors).to.have.length(1); | ||
expect(result.errors![0].message).to.match(/Only Grafast.*finalize method;/); | ||
}); |