-
Notifications
You must be signed in to change notification settings - Fork 233
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
feat: uniquely identify batched queries #849
Changes from all commits
76e6d18
6f05624
c31bee0
56f14c5
94aaede
f82fefe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const sinon = require('sinon') | ||
const GQL = require('..') | ||
|
||
test('POST regular query', async (t) => { | ||
|
@@ -346,3 +347,44 @@ test('POST batched query with a resolver which succeeds and a resolver which thr | |
|
||
t.same(JSON.parse(res.body), [{ data: { add: 3 } }, { data: null, errors: [{ message: 'Internal Server Error' }] }]) | ||
}) | ||
|
||
test('POST batched query has an individual context for each operation', async (t) => { | ||
const app = Fastify() | ||
|
||
const contextSpy = sinon.spy() | ||
|
||
const schema = ` | ||
type Query { | ||
test: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
test: (_, ctx) => contextSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery) | ||
} | ||
|
||
app.register(GQL, { | ||
schema, | ||
resolvers, | ||
allowBatchedQueries: true | ||
}) | ||
|
||
await app.inject({ | ||
method: 'POST', | ||
url: '/graphql', | ||
body: [ | ||
{ | ||
operationName: 'TestQuery', | ||
query: 'query TestQuery { test }' | ||
}, | ||
{ | ||
operationName: 'DoubleQuery', | ||
query: 'query DoubleQuery { test }' | ||
} | ||
] | ||
}) | ||
|
||
sinon.assert.calledTwice(contextSpy) | ||
sinon.assert.calledWith(contextSpy, 0, 2, sinon.match(/TestQuery/)) | ||
sinon.assert.calledWith(contextSpy, 1, 2, sinon.match(/DoubleQuery/)) | ||
Comment on lines
+387
to
+389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we not testing that operationId and operationCount are also passed around properly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I don't understand your question, but I believe that's what we're doing here: when calling the resolver we're reading the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry I read that wrong, I thought those were the number of the call and the position of the argument for some reason! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, I feel you: I even had to double check the spy parameters when writing that assertion. |
||
}) |
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.
I would like to hear @mcollina's opinion on the approach to cloning a request object. We need it to be cloned because every query in the batch needs a different context, but I don't know if this is a sensible way to clone it