-
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
[Data masking] Fix aggressive warnings when using objects marked with @unmask(mode: "migrate")
with cache.identify
#12116
Changes from 12 commits
3bd46d3
8070ae1
ac909c2
dedcbec
819475f
d8298e4
01d0212
706c891
53e9109
afd18c3
1b23594
a8303d1
0a6a3fb
c57ea51
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Prevent field accessor warnings when using `@unmask(mode: "migrate")` on objects that are passed into `cache.identify`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"dist/apollo-client.min.cjs": 41506, | ||
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34257 | ||
"dist/apollo-client.min.cjs": 41516, | ||
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34299 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ import { | |
keyArgsFnFromSpecifier, | ||
keyFieldsFnFromSpecifier, | ||
} from "./key-extractor.js"; | ||
import { disableWarningsSlot } from "../../core/masking.js"; | ||
|
||
export type TypePolicies = { | ||
[__typename: string]: TypePolicy; | ||
|
@@ -392,7 +393,9 @@ export class Policies { | |
const policy = typename && this.getTypePolicy(typename); | ||
let keyFn = (policy && policy.keyFn) || this.config.dataIdFromObject; | ||
while (keyFn) { | ||
const specifierOrId = keyFn({ ...object, ...storeObject }, context); | ||
const specifierOrId = disableWarningsSlot.withValue(true, () => { | ||
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. It might make sense to wrap this slot around 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. Good call! Updated in 0a6a3fb |
||
return keyFn!({ ...object, ...storeObject }, context); | ||
}); | ||
if (isArray(specifierOrId)) { | ||
keyFn = keyFieldsFnFromSpecifier(specifierOrId); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2244,7 +2244,7 @@ describe("maskFragment", () => { | |
|
||
test("warns when accessing unmasked fields when using `@unmask` directive with mode 'migrate'", () => { | ||
using _ = spyOnConsole("warn"); | ||
const query = gql` | ||
const fragment = gql` | ||
fragment UnmaskedFragment on User { | ||
id | ||
name | ||
|
@@ -2258,18 +2258,22 @@ describe("maskFragment", () => { | |
|
||
const data = maskFragment( | ||
deepFreeze({ | ||
currentUser: { | ||
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. This test was written incorrectly the first time 😅. This just happen to pass because we added accessor warnings for fields on the masked object that weren't present on the original |
||
__typename: "User", | ||
id: 1, | ||
name: "Test User", | ||
age: 30, | ||
}, | ||
__typename: "User", | ||
id: 1, | ||
name: "Test User", | ||
age: 30, | ||
}), | ||
query, | ||
fragment, | ||
new InMemoryCache(), | ||
"UnmaskedFragment" | ||
); | ||
|
||
data.__typename; | ||
data.id; | ||
data.name; | ||
|
||
expect(console.warn).not.toHaveBeenCalled(); | ||
|
||
data.age; | ||
|
||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
|
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.
As per my other comment, I had to make this change in the test since the keys are no longer present on the returned masked object since they weren't on the original
data
object (due to no data yet in the cache). This gets the test passing again.