-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Bugfix: Don't mutate fragments that are attached to query documents #447
Conversation
@@ -124,6 +125,7 @@ export function createFragmentMap(fragments: FragmentDefinition[]): FragmentMap | |||
export function addFragmentsToDocument(queryDoc: Document, | |||
fragments: FragmentDefinition[]): Document { | |||
checkDocument(queryDoc); | |||
queryDoc = cloneShallow(queryDoc); |
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 assume the cloneShallow
works (as opposed to a deep clone) since we have OperationDefinitions
as a top level attribute of the Document
?
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.
Yeah; definitions
is the only value being mutated here
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.
Can we add a comment about this? That we are basically using this to clone the definitions
array?
Could we achieve this in a simpler way using:
queryDoc = {
definitions: [...queryDoc.definitions],
};
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.
There's a few more properties on a Document
than just the definitions
- I'll go w/ assign
(since we can't object spread in TypeScript)
Looks good to me other than the question I commented. Once this is rebased and @stubailo can review, we can probably merge. |
rebased |
We either need to add a comment about the clone, or replace it with a more explicit clone of |
This fixes an issue that arises when you perform the same query multiple times, and that query has fragments. Because `addFragmentsToDocument` _appends_ fragments to `queryDoc.definitions`, each time the query is performed, the query document gains a new copy of each fragment. This is particularly egregious, since `graphql-tag` maintains an identity map for queries
updated to use |
Thanks! |
This fixes an issue that arises when you perform the same query multiple times, and that query has fragments.
Because
addFragmentsToDocument
appends fragments toqueryDoc.definitions
, each time the query is performed, the query document gains a new copy of each fragment. This is particularly egregious, sincegraphql-tag
maintains an identity map for queries.For example, the following currently results in a server error (duplicate fragment authorDetails) on the second query: