-
-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
batch-execute enhancements #3588
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d42f19f
improve batch execute.
gmac 977b72e
happy typescript.
gmac fa42023
cleanup auto-assignement.
gmac ac8ddce
linters dont agree on style.
gmac 018806c
Create pink-walls-return.md
gmac 21e6fb5
Update batch-execution.mdx
gmac b95b393
more tests.
gmac 544a970
Merge branch 'gmac/batch-execute' of github.com:gmac/graphql-tools in…
gmac 31b0416
fix ts error.
gmac 44ba4cb
fix batch execute fragment handling.
gmac 1642bdd
setup operation type util.
gmac 3bd4cbe
reorganize.
gmac 6a0b4cf
leave operationType required.
gmac 14305c2
Update pink-walls-return.md
gmac 7229033
Update pink-walls-return.md
gmac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 @@ | ||
--- | ||
"@graphql-tools/batch-execute": patch | ||
"@graphql-tools/delegate": patch | ||
"@graphql-tools/utils": patch | ||
"@graphql-tools/website": patch | ||
--- | ||
|
||
batch-execute enhancements: | ||
- fixes bugs with batched fragment definitions | ||
- unpathed errors are now returned for all batch results | ||
- the "graphqlTools" prefix is simplified down to just "_" | ||
- new tests and documentation |
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
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
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,197 @@ | ||
import { | ||
graphql, | ||
parse, | ||
print, | ||
OperationDefinitionNode, | ||
ExecutionResult | ||
} from 'graphql'; | ||
import { makeExecutableSchema } from '@graphql-tools/schema'; | ||
import { createBatchingExecutor } from '@graphql-tools/batch-execute'; | ||
import { Executor } from '@graphql-tools/utils'; | ||
|
||
describe('batch execution', () => { | ||
let executorCalls = 0; | ||
let executorDocument: string | undefined; | ||
let executorVariables: Record<string, any> | undefined; | ||
|
||
const schema = makeExecutableSchema({ | ||
typeDefs: /* GraphQL */` | ||
type Query { | ||
field1: String | ||
field2: String | ||
field3(input: String): String | ||
boom(message: String): String | ||
widget: Widget | ||
} | ||
type Widget { | ||
name: String | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
field1: () => '1', | ||
field2: () => '2', | ||
field3: (_root, { input }) => String(input), | ||
boom: (_root, { message }) => new Error(message), | ||
widget: () => ({ name: 'wingnut' }), | ||
}, | ||
}, | ||
}); | ||
|
||
const exec = (({ document, variables }) => { | ||
executorCalls += 1; | ||
executorDocument = print(document); | ||
executorVariables = variables; | ||
return graphql({ | ||
schema, | ||
source: executorDocument, | ||
variableValues: executorVariables, | ||
}); | ||
}) as Executor; | ||
|
||
const batchExec = createBatchingExecutor(exec); | ||
|
||
beforeEach(() => { | ||
executorCalls = 0; | ||
executorDocument = undefined; | ||
executorVariables = undefined; | ||
}); | ||
|
||
function getRequestFields(): Array<string> { | ||
if (executorDocument != null) { | ||
const op = parse(executorDocument).definitions[0] as OperationDefinitionNode; | ||
const names = op.selectionSet.selections.map(sel => 'name' in sel ? sel.name.value : undefined); | ||
return names.filter(Boolean) as Array<string>; | ||
} | ||
return []; | ||
} | ||
|
||
it('batchs multiple executions', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' }), | ||
batchExec({ document: parse('{ field2 field3(input: "3") }'), operationType: 'query' }), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.data).toEqual({ field1: '1', field2: '2' }); | ||
expect(second?.data).toEqual({ field2: '2', field3: '3' }); | ||
expect(executorCalls).toEqual(1); | ||
expect(getRequestFields()).toEqual(['field1', 'field2', 'field2', 'field3']); | ||
}); | ||
|
||
it('preserves root field aliases in the final result', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ document: parse('{ a: field1 b: field2 }'), operationType: 'query' }), | ||
batchExec({ document: parse('{ c: field2 d: field3(input: "3") }'), operationType: 'query' }), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.data).toEqual({ a: '1', b: '2' }); | ||
expect(second?.data).toEqual({ c: '2', d: '3' }); | ||
expect(executorCalls).toEqual(1); | ||
expect(getRequestFields()).toEqual(['field1', 'field2', 'field2', 'field3']); | ||
}); | ||
|
||
it('renames input variables', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ | ||
document: parse('query($a: String){ field3(input: $a) }'), | ||
variables: { a: '1' }, | ||
operationType: 'query', | ||
}), | ||
batchExec({ | ||
document: parse('query($a: String){ field3(input: $a) }'), | ||
variables: { a: '2' }, | ||
operationType: 'query', | ||
}), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.data).toEqual({ field3: '1' }); | ||
expect(second?.data).toEqual({ field3: '2' }); | ||
expect(executorVariables).toEqual({ _0_a: '1', _1_a: '2' }); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
|
||
it('renames fields within inline spreads', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ document: parse('{ ...on Query { field1 } }'), operationType: 'query' }), | ||
batchExec({ document: parse('{ ...on Query { field2 } }'), operationType: 'query' }), | ||
]) as ExecutionResult[]; | ||
|
||
const squishedDoc = executorDocument?.replace(/\s+/g, ' '); | ||
expect(squishedDoc).toMatch('... on Query { _0_field1: field1 }'); | ||
expect(squishedDoc).toMatch('... on Query { _1_field2: field2 }'); | ||
expect(first?.data).toEqual({ field1: '1' }); | ||
expect(second?.data).toEqual({ field2: '2' }); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
|
||
it('renames fragment definitions and spreads', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ | ||
document: parse('fragment A on Widget { name } query{ widget { ...A } }'), | ||
operationType: 'query', | ||
}), | ||
batchExec({ | ||
document: parse('fragment A on Widget { name } query{ widget { ...A } }'), | ||
operationType: 'query', | ||
}), | ||
]) as ExecutionResult[]; | ||
|
||
const squishedDoc = executorDocument?.replace(/\s+/g, ' '); | ||
expect(squishedDoc).toMatch('_0_widget: widget { ..._0_A }'); | ||
expect(squishedDoc).toMatch('_1_widget: widget { ..._1_A }'); | ||
expect(squishedDoc).toMatch('fragment _0_A on Widget'); | ||
expect(squishedDoc).toMatch('fragment _1_A on Widget'); | ||
expect(first?.data).toEqual({ widget: { name: 'wingnut' } }); | ||
expect(second?.data).toEqual({ widget: { name: 'wingnut' } }); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
|
||
it('removes expanded root fragment definitions', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ | ||
document: parse('fragment A on Query { field1 } query{ ...A }'), | ||
operationType: 'query', | ||
}), | ||
batchExec({ | ||
document: parse('fragment A on Query { field2 } query{ ...A }'), | ||
operationType: 'query', | ||
}), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.data).toEqual({ field1: '1' }); | ||
expect(second?.data).toEqual({ field2: '2' }); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
|
||
it('preserves pathed errors in the final result', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ | ||
document: parse('{ first: boom(message: "first error") }'), | ||
operationType: 'query', | ||
}), | ||
batchExec({ | ||
document: parse('{ second: boom(message: "second error") }'), | ||
operationType: 'query', | ||
}), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.errors?.[0].message).toEqual('first error'); | ||
expect(first?.errors?.[0].path).toEqual(['first']); | ||
expect(second?.errors?.[0].message).toEqual('second error'); | ||
expect(second?.errors?.[0].path).toEqual(['second']); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
|
||
it('returns request-level errors to all results', async () => { | ||
const [first, second] = await Promise.all([ | ||
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' }), | ||
batchExec({ document: parse('{ notgonnawork }'), operationType: 'query' }), | ||
]) as ExecutionResult[]; | ||
|
||
expect(first?.errors?.length).toEqual(1); | ||
expect(second?.errors?.length).toEqual(1); | ||
expect(first?.errors?.[0].message).toMatch(/notgonnawork/); | ||
expect(second?.errors?.[0].message).toMatch(/notgonnawork/); | ||
expect(executorCalls).toEqual(1); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yaacovCR – have a look at 44ba4cb. I've fixed a couple bugs related to fragments here: