Skip to content

Commit

Permalink
Change 'isSpecified*' & 'isIntrospectionType' accept only correct typ…
Browse files Browse the repository at this point in the history
…es (#2196)

Fixes #2153
Previous disscussion: #1837 (comment)
  • Loading branch information
IvanGoncharov committed Oct 1, 2019
1 parent f36ca25 commit 730f5cc
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 76 deletions.
53 changes: 0 additions & 53 deletions src/type/__tests__/predicate-test.js
Expand Up @@ -157,35 +157,6 @@ describe('Type predicates', () => {
it('returns false for custom scalar', () => {
expect(isSpecifiedScalarType(ScalarType)).to.equal(false);
});

it('returns false for scalar class (rather than specified instance)', () => {
expect(isSpecifiedScalarType(GraphQLScalarType)).to.equal(false);
});

it('returns false for wrapped specified scalar', () => {
expect(isSpecifiedScalarType(GraphQLList(GraphQLString))).to.equal(false);
});

it('returns false for non-scalar', () => {
expect(isSpecifiedScalarType(EnumType)).to.equal(false);
expect(isSpecifiedScalarType(Directive)).to.equal(false);
});

it('returns false for spec defined directive', () => {
expect(isSpecifiedScalarType(GraphQLSkipDirective)).to.equal(false);
});

it('returns false for object type named like specified scalar', () => {
const ObjectNamedLikeScalar = new GraphQLObjectType({
name: 'String',
fields: { serialize: { type: GraphQLString } },
});
expect(isSpecifiedScalarType(ObjectNamedLikeScalar)).to.equal(false);
});

it('returns false for random garbage', () => {
expect(isSpecifiedScalarType({ what: 'is this' })).to.equal(false);
});
});

describe('isObjectType', () => {
Expand Down Expand Up @@ -700,29 +671,5 @@ describe('Directive predicates', () => {
it('returns false for custom directive', () => {
expect(isSpecifiedDirective(Directive)).to.equal(false);
});

it('returns false for directive class (rather than specified instance)', () => {
expect(isSpecifiedDirective(GraphQLDirective)).to.equal(false);
});

it('returns false for non-directive', () => {
expect(isSpecifiedDirective(EnumType)).to.equal(false);
expect(isSpecifiedDirective(ScalarType)).to.equal(false);
});

it('returns false for spec defined scalar type', () => {
expect(isSpecifiedDirective(GraphQLString)).to.equal(false);
});

it('returns false for scalar type named like specified directive', () => {
const ScalarNamedLikeDirective = new GraphQLScalarType({
name: 'deprecated',
});
expect(isSpecifiedDirective(ScalarNamedLikeDirective)).to.equal(false);
});

it('returns false for random garbage', () => {
expect(isSpecifiedDirective({ what: 'is this' })).to.equal(false);
});
});
});
9 changes: 4 additions & 5 deletions src/type/directives.js
Expand Up @@ -198,9 +198,8 @@ export const specifiedDirectives = Object.freeze([
GraphQLDeprecatedDirective,
]);

export function isSpecifiedDirective(directive: mixed): boolean %checks {
return (
isDirective(directive) &&
specifiedDirectives.some(({ name }) => name === directive.name)
);
export function isSpecifiedDirective(
directive: GraphQLDirective,
): boolean %checks {
return specifiedDirectives.some(({ name }) => name === directive.name);
}
9 changes: 3 additions & 6 deletions src/type/introspection.js
Expand Up @@ -14,6 +14,7 @@ import { type GraphQLDirective } from './directives';
import { GraphQLString, GraphQLBoolean } from './scalars';
import {
type GraphQLType,
type GraphQLNamedType,
type GraphQLInputField,
type GraphQLEnumValue,
type GraphQLField,
Expand All @@ -31,7 +32,6 @@ import {
isListType,
isNonNullType,
isAbstractType,
isNamedType,
} from './definition';

export const __Schema = new GraphQLObjectType({
Expand Down Expand Up @@ -485,9 +485,6 @@ export const introspectionTypes = Object.freeze([
__TypeKind,
]);

export function isIntrospectionType(type: mixed): boolean %checks {
return (
isNamedType(type) &&
introspectionTypes.some(({ name }) => type.name === name)
);
export function isIntrospectionType(type: GraphQLNamedType): boolean %checks {
return introspectionTypes.some(({ name }) => type.name === name);
}
9 changes: 3 additions & 6 deletions src/type/scalars.js
Expand Up @@ -11,7 +11,7 @@ import { print } from '../language/printer';

import { GraphQLError } from '../error/GraphQLError';

import { GraphQLScalarType, isScalarType } from './definition';
import { type GraphQLNamedType, GraphQLScalarType } from './definition';

// As per the GraphQL Spec, Integers are only treated as valid when a valid
// 32-bit signed integer, providing the broadest support across platforms.
Expand Down Expand Up @@ -271,9 +271,6 @@ export const specifiedScalarTypes = Object.freeze([
GraphQLID,
]);

export function isSpecifiedScalarType(type: mixed): boolean %checks {
return (
isScalarType(type) &&
specifiedScalarTypes.some(({ name }) => type.name === name)
);
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean %checks {
return specifiedScalarTypes.some(({ name }) => type.name === name);
}
4 changes: 1 addition & 3 deletions tstypes/type/directives.d.ts
Expand Up @@ -69,6 +69,4 @@ export const GraphQLDeprecatedDirective: GraphQLDirective;
*/
export const specifiedDirectives: ReadonlyArray<GraphQLDirective>;

export function isSpecifiedDirective(
directive: any,
): directive is GraphQLDirective;
export function isSpecifiedDirective(directive: GraphQLDirective): boolean;
2 changes: 1 addition & 1 deletion tstypes/type/introspection.d.ts
Expand Up @@ -37,4 +37,4 @@ export const TypeNameMetaFieldDef: GraphQLField<any, any>;

export const introspectionTypes: ReadonlyArray<GraphQLType>;

export function isIntrospectionType(type: any): boolean;
export function isIntrospectionType(type: GraphQLType): boolean;
4 changes: 2 additions & 2 deletions tstypes/type/scalars.d.ts
@@ -1,4 +1,4 @@
import { GraphQLScalarType } from './definition';
import { GraphQLScalarType, GraphQLNamedType } from './definition';

export const GraphQLInt: GraphQLScalarType;
export const GraphQLFloat: GraphQLScalarType;
Expand All @@ -8,4 +8,4 @@ export const GraphQLID: GraphQLScalarType;

export const specifiedScalarTypes: ReadonlyArray<GraphQLScalarType>;

export function isSpecifiedScalarType(type: any): type is GraphQLScalarType;
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean;

0 comments on commit 730f5cc

Please sign in to comment.