Skip to content

Commit

Permalink
Test key: ["$variable"] field policies.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Aug 20, 2021
1 parent 298d73c commit e1885d7
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,106 @@ describe("type policies", function () {
});
}));

it("can refer to variables in field key shorthand array", function () {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
defaultToNumVariable: {
key: ["input", "$num"],
read(existing, { args, variables }) {
return existing ?? args?.input ?? variables?.num;
},
},
},
},
},
});

const queryWithInputArg = gql`
query WithInputArg($num: Int) {
defaultToNumVariable(input: 1234)
}
`;

cache.writeQuery({
query: queryWithInputArg,
data: {
defaultToNumVariable: "forced with input and $num",
},
variables: {
num: 1234,
},
});

expect(cache.extract()).toEqual({
ROOT_QUERY: {
"__typename": "Query",
'defaultToNumVariable:{"input":1234,"$num":1234}': "forced with input and $num",
},
});

const queryWithNoArgs = gql`
query WithInputArg($num: Int) {
defaultToNumVariable
}
`;

cache.writeQuery({
query: queryWithNoArgs,
data: {
defaultToNumVariable: "forced with only $num",
},
variables: {
num: 2345,
},
});

expect(cache.extract()).toEqual({
ROOT_QUERY: {
"__typename": "Query",
'defaultToNumVariable:{"input":1234,"$num":1234}': "forced with input and $num",
'defaultToNumVariable:{"$num":2345}': "forced with only $num",
},
});

expect(cache.readQuery({
query: queryWithInputArg,
variables: {
num: 1234,
},
})).toEqual({
defaultToNumVariable: "forced with input and $num",
});

expect(cache.readQuery({
query: queryWithNoArgs,
variables: {
num: 2345,
},
})).toEqual({
defaultToNumVariable: "forced with only $num",
});

expect(cache.readQuery({
query: queryWithInputArg,
variables: {
num: 3456,
},
})).toEqual({
defaultToNumVariable: 1234,
});

expect(cache.readQuery({
query: queryWithNoArgs,
variables: {
num: 4567,
},
})).toEqual({
defaultToNumVariable: 4567,
});
});

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

Expand Down

0 comments on commit e1885d7

Please sign in to comment.