Skip to content

Commit 4e84def

Browse files
cheapsteakabernix
authored andcommitted
Avoid duplicate cacheControl directives via isDirectiveDefined (#2428)
1 parent ecc9880 commit 4e84def

4 files changed

Lines changed: 75 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- `apollo-datasource-rest`: Correctly allow a TTL value of `0` to represent "not-cacheable". [PR #2588](https://github.com/apollographql/apollo-server/pull/2588)
2323
- `apollo-datasource-rest`: Fix `Invalid argument` in IE11, when `this.headers` is `undefined`. [PR #2607](https://github.com/apollographql/apollo-server/pull/2607)
2424

25+
- Don't add `cacheControl` directive if one has already been defined. [PR #2428](https://github.com/apollographql/apollo-server/pull/2428)
26+
2527
### v2.4.8
2628

2729
- No functional changes in this version. The patch version has been bumped to fix the `README.md` displayed on the [npm package for `apollo-server`](https://npm.im/apollo-server) as a result of a broken publish. Apologies for the additional noise!

packages/apollo-server-core/src/ApolloServer.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
} from './playground';
5252

5353
import { generateSchemaHash } from './utils/schemaHash';
54+
import { isDirectiveDefined } from './utils/isDirectiveDefined';
5455
import {
5556
processGraphQLRequest,
5657
GraphQLRequestContext,
@@ -270,19 +271,22 @@ export class ApolloServerBase {
270271

271272
// We augment the typeDefs with the @cacheControl directive and associated
272273
// scope enum, so makeExecutableSchema won't fail SDL validation
273-
augmentedTypeDefs.push(
274-
gql`
275-
enum CacheControlScope {
276-
PUBLIC
277-
PRIVATE
278-
}
279274

280-
directive @cacheControl(
281-
maxAge: Int
282-
scope: CacheControlScope
283-
) on FIELD_DEFINITION | OBJECT | INTERFACE
284-
`,
285-
);
275+
if (!isDirectiveDefined(augmentedTypeDefs, 'cacheControl')) {
276+
augmentedTypeDefs.push(
277+
gql`
278+
enum CacheControlScope {
279+
PUBLIC
280+
PRIVATE
281+
}
282+
283+
directive @cacheControl(
284+
maxAge: Int
285+
scope: CacheControlScope
286+
) on FIELD_DEFINITION | OBJECT | INTERFACE
287+
`,
288+
);
289+
}
286290

287291
if (this.uploadsConfig) {
288292
const { GraphQLUpload } = require('graphql-upload');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { gql } from '../';
2+
import { isDirectiveDefined } from '../utils/isDirectiveDefined';
3+
4+
describe('isDirectiveDefined', () => {
5+
it('returns false when a directive is not defined', () => {
6+
expect(
7+
isDirectiveDefined(
8+
[
9+
gql`
10+
type Query {
11+
hello: String
12+
}
13+
`,
14+
],
15+
'cacheControl',
16+
),
17+
).toBe(false);
18+
});
19+
20+
it('returns true when a directive is defined', () => {
21+
expect(
22+
isDirectiveDefined(
23+
[
24+
gql`
25+
type Query {
26+
hello: String
27+
}
28+
29+
enum CacheControlScope {
30+
PUBLIC
31+
PRIVATE
32+
}
33+
34+
directive @cacheControl(
35+
maxAge: Int
36+
scope: CacheControlScope
37+
) on FIELD_DEFINITION | OBJECT | INTERFACE
38+
`,
39+
],
40+
'cacheControl',
41+
),
42+
).toBe(true);
43+
});
44+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DocumentNode, Kind } from 'graphql/language';
2+
3+
export const isDirectiveDefined = (
4+
typeDefs: DocumentNode[],
5+
directiveName: string,
6+
) =>
7+
typeDefs.some(typeDef =>
8+
typeDef.definitions.some(
9+
definition =>
10+
definition.kind === Kind.DIRECTIVE_DEFINITION &&
11+
definition.name.value === directiveName,
12+
),
13+
);

0 commit comments

Comments
 (0)