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

Allow @directive and $variable strings in field policy keyArgs: ["arg", "@dir", "$var"] arrays #8678

Merged
Prev Previous commit
Next Next commit
Test key: ["@directive"] field policies.
  • Loading branch information
benjamn committed Aug 25, 2021
commit 55cf401dcea2c4f6a3ecc6aac54d2f744b8541fa
111 changes: 111 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,117 @@ describe("type policies", function () {
});
}));

// Use several different directives to prove we're not hard-coding support
// for the @connection directive.
["connection",
"directive",
"misdirective",
].forEach(directiveName => it(`can refer to directive @${
directiveName
} in field key shorthand array`, function () {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
feed: {
key: ["@" + directiveName, ["key"], "arg"],
},
},
},
},
});

const helloTokens = [
{ __typename: "Token", text: "Hello" },
{ __typename: "Token", text: "World" },
{ __typename: "Token", text: "!" },
];

cache.writeQuery({
query: gql`
query FeedQuery($num: Int) {
feed(arg: $num) @${directiveName}(key: "asdf", ignored: "boo") {
text
}
}
`,
variables: {
num: 1234,
},
data: {
feed: helloTokens,
},
});

expect(cache.extract()).toEqual({
ROOT_QUERY: {
"__typename": "Query",
[`feed:{"@${directiveName}":{"key":"asdf"},"arg":1234}`]: helloTokens,
},
});

const farewellTokens = [
{ __typename: "Token", text: "farewell" },
{ __typename: "Token", text: "cruel" },
{ __typename: "Token", text: "world" },
];

cache.writeQuery({
query: gql`
query FeedQuery($num: Int) {
feed(arg: $num) {
text
}
}
`,
variables: {
num: 2345,
},
data: {
feed: farewellTokens,
},
});

expect(cache.extract()).toEqual({
ROOT_QUERY: {
"__typename": "Query",
[`feed:{"@${directiveName}":{"key":"asdf"},"arg":1234}`]: helloTokens,
'feed:{"arg":2345}': farewellTokens,
},
});

const directivesTokens = [
{ __typename: "Token", text: "directives" },
{ __typename: "Token", text: "consuming" },
{ __typename: "Token", text: "variables" },
];

cache.writeQuery({
query: gql`
query FeedQuery($num: Int) {
feed @${directiveName}(key: $num, ignored: "boo") {
text
}
}
`,
variables: {
num: 3456,
},
data: {
feed: directivesTokens,
},
});

expect(cache.extract()).toEqual({
ROOT_QUERY: {
"__typename": "Query",
[`feed:{"@${directiveName}":{"key":"asdf"},"arg":1234}`]: helloTokens,
'feed:{"arg":2345}': farewellTokens,
[`feed:{"@${directiveName}":{"key":3456}}`]: directivesTokens,
},
});
}));

it("can use options.storage in read functions", function () {
const storageSet = new Set<Record<string, any>>();

Expand Down