-
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.
- Loading branch information
1 parent
ecc9880
commit 4e84def
Showing
4 changed files
with
75 additions
and
12 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
44 changes: 44 additions & 0 deletions
44
packages/apollo-server-core/src/__tests__/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,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
13
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,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, | ||
), | ||
); |