-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid duplicate
cacheControl
directives via `isDirectiveDefin… (#2762)
* Revert "Revert "Avoid duplicate `cacheControl` directives via `isDirectiveDefined` (#2428)"" This reverts commit c742335. * Account for typeDefs possibly being a string * Add tests to ensure isDirectiveDefiend works for strings * rename test file to add ".test" * update changelog
- Loading branch information
1 parent
a2b2a0d
commit 65ca915
Showing
4 changed files
with
84 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/apollo-server-core/src/__tests__/isDirectiveDefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { gql } from '../'; | ||
import { isDirectiveDefined } from '../utils/isDirectiveDefined'; | ||
|
||
describe('isDirectiveDefined', () => { | ||
const noCacheControl = ` | ||
type Query { | ||
hello: String | ||
} | ||
`; | ||
const hasCacheControl = ` | ||
type Query { | ||
hello: String | ||
} | ||
enum CacheControlScope { | ||
PUBLIC | ||
PRIVATE | ||
} | ||
directive @cacheControl( | ||
maxAge: Int | ||
scope: CacheControlScope | ||
) on FIELD_DEFINITION | OBJECT | INTERFACE | ||
`; | ||
|
||
describe('When passed an array of DocumentNode', () => { | ||
it('returns false when a directive is not defined', () => { | ||
expect(isDirectiveDefined([gql(noCacheControl)], 'cacheControl')).toBe( | ||
false, | ||
); | ||
}); | ||
it('returns true when a directive is defined', () => { | ||
expect(isDirectiveDefined([gql(hasCacheControl)], 'cacheControl')).toBe( | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
describe('When passed a string', () => { | ||
it('returns false when a directive is not defined', () => { | ||
expect(isDirectiveDefined(noCacheControl, 'cacheControl')).toBe(false); | ||
}); | ||
it('returns true when a directive is defined', () => { | ||
expect(isDirectiveDefined(hasCacheControl, 'cacheControl')).toBe(true); | ||
}); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/apollo-server-core/src/utils/isDirectiveDefined.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DocumentNode, Kind } from 'graphql/language'; | ||
import { gql } from '../'; | ||
|
||
export const isDirectiveDefined = ( | ||
typeDefs: DocumentNode[] | string, | ||
directiveName: string, | ||
): boolean => { | ||
if (typeof typeDefs === 'string') { | ||
return isDirectiveDefined([gql(typeDefs)], directiveName); | ||
} | ||
return typeDefs.some(typeDef => | ||
typeDef.definitions.some( | ||
definition => | ||
definition.kind === Kind.DIRECTIVE_DEFINITION && | ||
definition.name.value === directiveName, | ||
), | ||
); | ||
}; |