Skip to content
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

Merged
merged 6 commits into from Aug 29, 2022

Conversation

drakhart
Copy link
Contributor

resolves #842

Fixes context.__currentQuery being overwritten by subsequent batched query operations.
Stores context.operationsCount and context.operationId (same naming as with subscriptions) to uniquely identify batched query operations at a later stage.

lib/routes.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, good work!

Copy link
Contributor

@jonnydgreen jonnydgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally looks great! Should we update the TypeScript typings (with type tests for these) for the context as well?

@drakhart
Copy link
Contributor Author

Functionally looks great! Should we update the TypeScript typings (with type tests for these) for the context as well?

I've added the new (and missing) context properties, but I'm unsure how to test these types: never wrote type tests and by looking at the existing ones it's unclear to me how to test this specific ones. Perhaps you can lend a hand? 🙏

@simoneb
Copy link
Collaborator

simoneb commented Aug 26, 2022

Functionally looks great! Should we update the TypeScript typings (with type tests for these) for the context as well?

I've added the new (and missing) context properties, but I'm unsure how to test these types: never wrote type tests and by looking at the existing ones it's unclear to me how to test this specific ones. Perhaps you can lend a hand? 🙏

Check this out @drakhart https://github.com/mercurius-js/mercurius/blob/master/test/types/index.ts

@drakhart
Copy link
Contributor Author

Functionally looks great! Should we update the TypeScript typings (with type tests for these) for the context as well?

I've added the new (and missing) context properties, but I'm unsure how to test these types: never wrote type tests and by looking at the existing ones it's unclear to me how to test this specific ones. Perhaps you can lend a hand? 🙏

Check this out @drakhart https://github.com/mercurius-js/mercurius/blob/master/test/types/index.ts

Yes yes, I did, that's what I meant with "by looking at the existing ones it's unclear to me how to test this specific ones". I'll try my best, no promises made.

lib/routes.js Outdated Show resolved Hide resolved
lib/routes.js Outdated Show resolved Hide resolved
@jonnydgreen
Copy link
Contributor

Yes yes, I did, that's what I meant with "by looking at the existing ones it's unclear to me how to test this specific ones". I'll try my best, no promises made.

You probably want a test similar in structure to this one where the parameter types are checked: https://github.com/mercurius-js/mercurius/blob/master/test/types/index.ts#L748

Happy to help if you have any further questions :)

@drakhart drakhart requested review from simoneb and removed request for jonnydgreen August 29, 2022 10:48
@drakhart
Copy link
Contributor Author

Yes yes, I did, that's what I meant with "by looking at the existing ones it's unclear to me how to test this specific ones". I'll try my best, no promises made.

You probably want a test similar in structure to this one where the parameter types are checked: https://github.com/mercurius-js/mercurius/blob/master/test/types/index.ts#L748

Happy to help if you have any further questions :)

I think it should be fine now, but I'm not sure because it felt too easy in the end. Please have a look and let me know.

test/batched.js Outdated Show resolved Hide resolved

return Promise.all(request.body.map(async (r, operationId) => {
// Create individual reqs for multiple operations, otherwise reference the original req
const operationReq = operationsCount > 1 ? Object.create(request) : request
Copy link
Collaborator

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

@drakhart drakhart requested a review from simoneb August 29, 2022 11:43
@mcollina mcollina merged commit 911a5ef into mercurius-js:master Aug 29, 2022
Comment on lines +387 to +389
sinon.assert.calledTwice(contextSpy)
sinon.assert.calledWith(contextSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(contextSpy, 1, 2, sinon.match(/DoubleQuery/))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@drakhart drakhart Sep 1, 2022

Choose a reason for hiding this comment

The 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 operationId and operationsCount attributes from the context, and checking that they've been passed as expected: a call with operationId === 0, another call with operationId === 1, both of them with operationsCount === 2, and each one of them with the correct query contents.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uniquely identify batched queries through all their lifecycle
5 participants