-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7778 from apollographql/7642-allow-merging-into-e…
…xisting-refs Allow merge:true to merge references with non-normalized objects, and vice-versa.
- Loading branch information
Showing
5 changed files
with
250 additions
and
16 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
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 |
---|---|---|
|
@@ -4294,6 +4294,188 @@ describe("type policies", function () { | |
expect(personMergeCount).toBe(3); | ||
}); | ||
|
||
it("can force merging references with non-normalized objects", function () { | ||
const nameQuery = gql` | ||
query GetName { | ||
viewer { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const emailQuery = gql` | ||
query GetEmail { | ||
viewer { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
check(new InMemoryCache({ | ||
typePolicies: { | ||
Query: { | ||
fields: { | ||
viewer: { | ||
merge: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
})); | ||
|
||
check(new InMemoryCache({ | ||
typePolicies: { | ||
User: { | ||
merge: true, | ||
}, | ||
}, | ||
})); | ||
|
||
function check(cache: InMemoryCache) { | ||
// Write nameQuery first, so the existing data will be a | ||
// non-normalized object when we write emailQuery next. | ||
cache.writeQuery({ | ||
query: nameQuery, | ||
data: { | ||
viewer: { | ||
__typename: "User", | ||
name: "Alice", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
viewer: { | ||
__typename: "User", | ||
name: "Alice", | ||
}, | ||
}, | ||
}); | ||
|
||
cache.writeQuery({ | ||
query: emailQuery, | ||
data: { | ||
viewer: { | ||
__typename: "User", | ||
id: 12345, | ||
email: "[email protected]", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
viewer: { | ||
__ref: "User:12345", | ||
}, | ||
}, | ||
"User:12345": { | ||
__typename: "User", | ||
name: "Alice", | ||
id: 12345, | ||
email: "[email protected]", | ||
}, | ||
}); | ||
|
||
expect(cache.readQuery({ | ||
query: nameQuery, | ||
})).toEqual({ | ||
viewer: { | ||
__typename: "User", | ||
name: "Alice", | ||
}, | ||
}); | ||
|
||
expect(cache.readQuery({ | ||
query: emailQuery, | ||
})).toEqual({ | ||
viewer: { | ||
__typename: "User", | ||
id: 12345, | ||
email: "[email protected]", | ||
}, | ||
}); | ||
|
||
cache.reset(); | ||
expect(cache.extract()).toEqual({}); | ||
|
||
// Write emailQuery first, so the existing data will be a | ||
// normalized reference when we write nameQuery next. | ||
cache.writeQuery({ | ||
query: emailQuery, | ||
data: { | ||
viewer: { | ||
__typename: "User", | ||
id: 12345, | ||
email: "[email protected]", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toEqual({ | ||
"User:12345": { | ||
id: 12345, | ||
__typename: "User", | ||
email: "[email protected]" | ||
}, | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
viewer: { | ||
__ref: "User:12345", | ||
}, | ||
}, | ||
}); | ||
|
||
cache.writeQuery({ | ||
query: nameQuery, | ||
data: { | ||
viewer: { | ||
__typename: "User", | ||
name: "Alice", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toEqual({ | ||
"User:12345": { | ||
id: 12345, | ||
__typename: "User", | ||
email: "[email protected]", | ||
name: "Alice", | ||
}, | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
viewer: { | ||
__ref: "User:12345", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.readQuery({ | ||
query: nameQuery, | ||
})).toEqual({ | ||
viewer: { | ||
__typename: "User", | ||
name: "Alice", | ||
}, | ||
}); | ||
|
||
expect(cache.readQuery({ | ||
query: emailQuery, | ||
})).toEqual({ | ||
viewer: { | ||
__typename: "User", | ||
id: 12345, | ||
email: "[email protected]", | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
it("can force merging with inherited field merge function", function () { | ||
let authorMergeCount = 0; | ||
|
||
|
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