Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Cannot read property 'some' of undefined in apollo-server-core/di… #2924

Merged
merged 9 commits into from
Jun 26, 2019
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The version headers in this history reflect the versions of Apollo Server itself

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section.

- `apollo-server-core`: Guard against undefined property access in `isDirectiveDefined`. [PR #2924](https://github.com/apollographql/apollo-server/pull/2924)

### v2.6.6

- `apollo-server-core`: Avoid duplicate `cacheControl` directives being added via `isDirectiveDefined`, re-landing the implementation reverted in v2.6.1 which first surfaced in v2.6.0. [PR #2762](https://github.com/apollographql/apollo-server/pull/2762) [Reversion PR #2754](https://github.com/apollographql/apollo-server/pull/2754) [Original PR #2428](https://github.com/apollographql/apollo-server/pull/2428)
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export class ApolloServerBase {
);
}

let augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];

// We augment the typeDefs with the @cacheControl directive and associated
// scope enum, so makeExecutableSchema won't fail SDL validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ describe('isDirectiveDefined', () => {
) on FIELD_DEFINITION | OBJECT | INTERFACE
`;

describe('When passed a 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 an array of DocumentNode', () => {
it('returns false when a directive is not defined', () => {
expect(isDirectiveDefined([gql(noCacheControl)], 'cacheControl')).toBe(
Expand All @@ -36,6 +49,15 @@ describe('isDirectiveDefined', () => {
});
});

describe('When passed an array of strings', () => {
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);
});
});

describe('When passed a string', () => {
it('returns false when a directive is not defined', () => {
expect(isDirectiveDefined(noCacheControl, 'cacheControl')).toBe(false);
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type BaseConfig = Pick<
// fields that are not specific to a single integration
export interface Config extends BaseConfig {
modules?: GraphQLSchemaModule[];
typeDefs?: DocumentNode | Array<DocumentNode>;
typeDefs?: DocumentNode | Array<DocumentNode> | string | Array<string>;
parseOptions?: GraphQLParseOptions;
resolvers?: IResolvers;
schema?: GraphQLSchema;
Expand Down
20 changes: 12 additions & 8 deletions packages/apollo-server-core/src/utils/isDirectiveDefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { DocumentNode, Kind } from 'graphql/language';
import { gql } from '../';

export const isDirectiveDefined = (
typeDefs: DocumentNode[] | string,
typeDefs: (DocumentNode | string)[],
directiveName: string,
): boolean => {
if (typeof typeDefs === 'string') {
return isDirectiveDefined([gql(typeDefs)], directiveName);
}
return typeDefs.some(typeDef =>
typeDef.definitions.some(
// If we didn't receive an array of what we want, ensure it's an array.
typeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];

return typeDefs.some(typeDef => {
if (typeof typeDef === 'string') {
typeDef = gql(typeDef);
}

return typeDef.definitions.some(
definition =>
definition.kind === Kind.DIRECTIVE_DEFINITION &&
definition.name.value === directiveName,
),
);
);
});
};