From b66f889942a9d868c37930dd77feaa755e5d95c3 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Tue, 6 Aug 2024 17:26:37 -0700 Subject: [PATCH 1/2] Write a test for @cacheControl interaction between type and interface --- .../cacheControlDirective.test.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts b/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts index 8e543f086d6..b852d683ea5 100644 --- a/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts +++ b/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts @@ -277,6 +277,58 @@ describe('@cacheControl directives', () => { ); }); + it('interaction between type implementing interface, both with specified `maxAge`', async () => { + const schema = buildSchemaWithCacheControlSupport(` + type Query { + droid(id: ID!): Droid + named: Named + } + + type Droid implements Named @cacheControl(maxAge: 30) { + id: ID! + name: String! + } + + interface Named @cacheControl(maxAge: 60) { + name: String! + } + `); + + const hintsNamed = await collectCacheControlHints( + schema, + ` + query { + named { + ... on Droid { + id + name + } + } + } + `, + ); + + expect(hintsNamed).toStrictEqual( + new Map([['named', { maxAge: 60, scope: undefined }]]), + ); + + const hintsDroid = await collectCacheControlHints( + schema, + ` + query { + droid(id: 2001) { + id + name + } + } + `, + ); + + expect(hintsDroid).toStrictEqual( + new Map([['droid', { maxAge: 30, scope: undefined }]]), + ); + }); + it('inheritMaxAge', async () => { const schema = makeExecutableSchemaWithCacheControlSupport({ typeDefs: `#graphql From 711bf0329849f82dc732f3298963412fd8124e6a Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Wed, 7 Aug 2024 08:27:04 -0700 Subject: [PATCH 2/2] add one more case for type without cachecontrol implementing interface with cachecontrol --- .changeset/many-shirts-kneel.md | 2 ++ .../cacheControlDirective.test.ts | 27 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 .changeset/many-shirts-kneel.md diff --git a/.changeset/many-shirts-kneel.md b/.changeset/many-shirts-kneel.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/many-shirts-kneel.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts b/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts index b852d683ea5..a834d76663c 100644 --- a/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts +++ b/packages/server/src/__tests__/plugin/cacheControl/cacheControlDirective.test.ts @@ -280,8 +280,13 @@ describe('@cacheControl directives', () => { it('interaction between type implementing interface, both with specified `maxAge`', async () => { const schema = buildSchemaWithCacheControlSupport(` type Query { - droid(id: ID!): Droid named: Named + droid(id: ID!): Droid + alien(id: ID!): Alien + } + + interface Named @cacheControl(maxAge: 60) { + name: String! } type Droid implements Named @cacheControl(maxAge: 30) { @@ -289,7 +294,8 @@ describe('@cacheControl directives', () => { name: String! } - interface Named @cacheControl(maxAge: 60) { + type Alien implements Named { + id: ID! name: String! } `); @@ -327,6 +333,23 @@ describe('@cacheControl directives', () => { expect(hintsDroid).toStrictEqual( new Map([['droid', { maxAge: 30, scope: undefined }]]), ); + + const hintsAlien = await collectCacheControlHints( + schema, + ` + query { + alien(id: 3001) { + id + name + } + } + `, + { defaultMaxAge: 10 }, + ); + + expect(hintsAlien).toStrictEqual( + new Map([['alien', { maxAge: 10, scope: undefined }]]), + ); }); it('inheritMaxAge', async () => {