Skip to content

Commit

Permalink
Revert "Revert "Avoid duplicate cacheControl directives via `isDire…
Browse files Browse the repository at this point in the history
…ctiveDefined` (#2428)""

This reverts commit c742335.
  • Loading branch information
cheapsteak committed Jun 1, 2019
1 parent ce459ce commit e2ed33f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ import {
PlaygroundRenderPageOptions,
} from './playground';

import createSHA from './utils/createSHA';
import { generateSchemaHash } from './utils/schemaHash';
import { isDirectiveDefined } from './utils/isDirectiveDefined';
import createSHA from './utils/createSHA';
import {
processGraphQLRequest,
GraphQLRequestContext,
Expand Down Expand Up @@ -270,19 +271,22 @@ export class ApolloServerBase {

// We augment the typeDefs with the @cacheControl directive and associated
// scope enum, so makeExecutableSchema won't fail SDL validation
augmentedTypeDefs.push(
gql`
enum CacheControlScope {
PUBLIC
PRIVATE
}

directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
`,
);
if (!isDirectiveDefined(augmentedTypeDefs, 'cacheControl')) {
augmentedTypeDefs.push(
gql`
enum CacheControlScope {
PUBLIC
PRIVATE
}
directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
`,
);
}

if (this.uploadsConfig) {
const { GraphQLUpload } = require('graphql-upload');
Expand Down
44 changes: 44 additions & 0 deletions packages/apollo-server-core/src/__tests__/isDirectiveDefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { gql } from '../';
import { isDirectiveDefined } from '../utils/isDirectiveDefined';

describe('isDirectiveDefined', () => {
it('returns false when a directive is not defined', () => {
expect(
isDirectiveDefined(
[
gql`
type Query {
hello: String
}
`,
],
'cacheControl',
),
).toBe(false);
});

it('returns true when a directive is defined', () => {
expect(
isDirectiveDefined(
[
gql`
type Query {
hello: String
}
enum CacheControlScope {
PUBLIC
PRIVATE
}
directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
`,
],
'cacheControl',
),
).toBe(true);
});
});
13 changes: 13 additions & 0 deletions packages/apollo-server-core/src/utils/isDirectiveDefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DocumentNode, Kind } from 'graphql/language';

export const isDirectiveDefined = (
typeDefs: DocumentNode[],
directiveName: string,
) =>
typeDefs.some(typeDef =>
typeDef.definitions.some(
definition =>
definition.kind === Kind.DIRECTIVE_DEFINITION &&
definition.name.value === directiveName,
),
);

0 comments on commit e2ed33f

Please sign in to comment.