-
-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable coercion of trapped values (#2024)
- Loading branch information
Showing
24 changed files
with
1,672 additions
and
1,187 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,5 @@ | ||
--- | ||
"grafast": patch | ||
--- | ||
|
||
Planning error paths improved to indicate list positions. |
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,6 @@ | ||
--- | ||
"grafast": patch | ||
--- | ||
|
||
Introduce `trap()` step to trap errors and inhibits, and coerce them to a chosen | ||
form (e.g. null, empty list). |
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,149 @@ | ||
/* 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 { ExecutionResult } from "graphql"; | ||
import { it } from "mocha"; | ||
import sqlite3 from "sqlite3"; | ||
|
||
import type { ExecutionDetails, GrafastResultsList } from "../dist/index.js"; | ||
import { | ||
access, | ||
assertNotNull, | ||
context, | ||
ExecutableStep, | ||
grafast, | ||
lambda, | ||
list, | ||
makeGrafastSchema, | ||
trap, | ||
TRAP_ERROR, | ||
} from "../dist/index.js"; | ||
|
||
const makeSchema = () => { | ||
return makeGrafastSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Error { | ||
message: String | ||
} | ||
type Query { | ||
unhandledError(setNullToError: Int): Int | ||
errorToNull(setNullToError: Int): Int | ||
errorToEmptyList(setNullToError: Int): [Int] | ||
errorToError(setNullToError: Int): Error | ||
} | ||
`, | ||
plans: { | ||
Query: { | ||
unhandledError(_, { $setNullToError }) { | ||
const $a = assertNotNull($setNullToError, "Null!"); | ||
return $a; | ||
}, | ||
errorToNull(_, { $setNullToError }) { | ||
const $a = assertNotNull($setNullToError, "Null!"); | ||
return trap($a, TRAP_ERROR, { valueForError: "NULL" }); | ||
}, | ||
errorToEmptyList(_, { $setNullToError }) { | ||
const $a = assertNotNull($setNullToError, "Null!"); | ||
const $list = list([$a]); | ||
return trap($list, TRAP_ERROR, { valueForError: "EMPTY_LIST" }); | ||
}, | ||
errorToError(_, { $setNullToError }) { | ||
const $a = assertNotNull($setNullToError, "Null!"); | ||
const $derived = lambda($a, () => null, true); | ||
return trap($derived, TRAP_ERROR, { valueForError: "PASS_THROUGH" }); | ||
}, | ||
}, | ||
}, | ||
enableDeferStream: false, | ||
}); | ||
}; | ||
|
||
it("schema works as expected", async () => { | ||
const schema = makeSchema(); | ||
const source = /* GraphQL */ ` | ||
query Q { | ||
nonError: unhandledError(setNullToError: 2) | ||
error: unhandledError(setNullToError: null) | ||
} | ||
`; | ||
const variableValues = {}; | ||
const result = (await grafast({ | ||
schema, | ||
source, | ||
variableValues, | ||
contextValue: {}, | ||
resolvedPreset: {}, | ||
requestContext: {}, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.exist; | ||
expect(result.errors).to.have.length(1); | ||
expect(result.errors![0].path).to.deep.equal(["error"]); | ||
expect(result.errors![0].message).to.equal("Null!"); | ||
expect(result.data).to.deep.equal({ nonError: 2, error: null }); | ||
}); | ||
it("enables trapping an error to null", async () => { | ||
const schema = makeSchema(); | ||
const source = /* GraphQL */ ` | ||
query Q { | ||
nonError: errorToNull(setNullToError: 2) | ||
error: errorToNull(setNullToError: null) | ||
} | ||
`; | ||
const variableValues = {}; | ||
const result = (await grafast({ | ||
schema, | ||
source, | ||
variableValues, | ||
contextValue: {}, | ||
resolvedPreset: {}, | ||
requestContext: {}, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.not.exist; | ||
expect(result.data).to.deep.equal({ nonError: 2, error: null }); | ||
}); | ||
it("enables trapping an error to emptyList", async () => { | ||
const schema = makeSchema(); | ||
const source = /* GraphQL */ ` | ||
query Q { | ||
nonError: errorToEmptyList(setNullToError: 2) | ||
error: errorToEmptyList(setNullToError: null) | ||
} | ||
`; | ||
const variableValues = {}; | ||
const result = (await grafast({ | ||
schema, | ||
source, | ||
variableValues, | ||
contextValue: {}, | ||
resolvedPreset: {}, | ||
requestContext: {}, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.not.exist; | ||
expect(result.data).to.deep.equal({ nonError: [2], error: [] }); | ||
}); | ||
it("enables trapping an error to error", async () => { | ||
const schema = makeSchema(); | ||
const source = /* GraphQL */ ` | ||
query Q { | ||
nonError: errorToError(setNullToError: 2) { | ||
message | ||
} | ||
error: errorToError(setNullToError: null) { | ||
message | ||
} | ||
} | ||
`; | ||
const variableValues = {}; | ||
const result = (await grafast({ | ||
schema, | ||
source, | ||
variableValues, | ||
contextValue: {}, | ||
resolvedPreset: {}, | ||
requestContext: {}, | ||
})) as ExecutionResult; | ||
expect(result.errors).to.not.exist; | ||
expect(result.data).to.deep.equal({ | ||
nonError: null, | ||
error: { message: "Null!" }, | ||
}); | ||
}); |
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
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
Oops, something went wrong.