Skip to content

Commit 730f5cc

Browse files
Change 'isSpecified*' & 'isIntrospectionType' accept only correct types (#2196)
Fixes #2153 Previous disscussion: #1837 (comment)
1 parent f36ca25 commit 730f5cc

File tree

7 files changed

+14
-76
lines changed

7 files changed

+14
-76
lines changed

src/type/__tests__/predicate-test.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,6 @@ describe('Type predicates', () => {
157157
it('returns false for custom scalar', () => {
158158
expect(isSpecifiedScalarType(ScalarType)).to.equal(false);
159159
});
160-
161-
it('returns false for scalar class (rather than specified instance)', () => {
162-
expect(isSpecifiedScalarType(GraphQLScalarType)).to.equal(false);
163-
});
164-
165-
it('returns false for wrapped specified scalar', () => {
166-
expect(isSpecifiedScalarType(GraphQLList(GraphQLString))).to.equal(false);
167-
});
168-
169-
it('returns false for non-scalar', () => {
170-
expect(isSpecifiedScalarType(EnumType)).to.equal(false);
171-
expect(isSpecifiedScalarType(Directive)).to.equal(false);
172-
});
173-
174-
it('returns false for spec defined directive', () => {
175-
expect(isSpecifiedScalarType(GraphQLSkipDirective)).to.equal(false);
176-
});
177-
178-
it('returns false for object type named like specified scalar', () => {
179-
const ObjectNamedLikeScalar = new GraphQLObjectType({
180-
name: 'String',
181-
fields: { serialize: { type: GraphQLString } },
182-
});
183-
expect(isSpecifiedScalarType(ObjectNamedLikeScalar)).to.equal(false);
184-
});
185-
186-
it('returns false for random garbage', () => {
187-
expect(isSpecifiedScalarType({ what: 'is this' })).to.equal(false);
188-
});
189160
});
190161

191162
describe('isObjectType', () => {
@@ -700,29 +671,5 @@ describe('Directive predicates', () => {
700671
it('returns false for custom directive', () => {
701672
expect(isSpecifiedDirective(Directive)).to.equal(false);
702673
});
703-
704-
it('returns false for directive class (rather than specified instance)', () => {
705-
expect(isSpecifiedDirective(GraphQLDirective)).to.equal(false);
706-
});
707-
708-
it('returns false for non-directive', () => {
709-
expect(isSpecifiedDirective(EnumType)).to.equal(false);
710-
expect(isSpecifiedDirective(ScalarType)).to.equal(false);
711-
});
712-
713-
it('returns false for spec defined scalar type', () => {
714-
expect(isSpecifiedDirective(GraphQLString)).to.equal(false);
715-
});
716-
717-
it('returns false for scalar type named like specified directive', () => {
718-
const ScalarNamedLikeDirective = new GraphQLScalarType({
719-
name: 'deprecated',
720-
});
721-
expect(isSpecifiedDirective(ScalarNamedLikeDirective)).to.equal(false);
722-
});
723-
724-
it('returns false for random garbage', () => {
725-
expect(isSpecifiedDirective({ what: 'is this' })).to.equal(false);
726-
});
727674
});
728675
});

src/type/directives.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ export const specifiedDirectives = Object.freeze([
198198
GraphQLDeprecatedDirective,
199199
]);
200200

201-
export function isSpecifiedDirective(directive: mixed): boolean %checks {
202-
return (
203-
isDirective(directive) &&
204-
specifiedDirectives.some(({ name }) => name === directive.name)
205-
);
201+
export function isSpecifiedDirective(
202+
directive: GraphQLDirective,
203+
): boolean %checks {
204+
return specifiedDirectives.some(({ name }) => name === directive.name);
206205
}

src/type/introspection.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { type GraphQLDirective } from './directives';
1414
import { GraphQLString, GraphQLBoolean } from './scalars';
1515
import {
1616
type GraphQLType,
17+
type GraphQLNamedType,
1718
type GraphQLInputField,
1819
type GraphQLEnumValue,
1920
type GraphQLField,
@@ -31,7 +32,6 @@ import {
3132
isListType,
3233
isNonNullType,
3334
isAbstractType,
34-
isNamedType,
3535
} from './definition';
3636

3737
export const __Schema = new GraphQLObjectType({
@@ -485,9 +485,6 @@ export const introspectionTypes = Object.freeze([
485485
__TypeKind,
486486
]);
487487

488-
export function isIntrospectionType(type: mixed): boolean %checks {
489-
return (
490-
isNamedType(type) &&
491-
introspectionTypes.some(({ name }) => type.name === name)
492-
);
488+
export function isIntrospectionType(type: GraphQLNamedType): boolean %checks {
489+
return introspectionTypes.some(({ name }) => type.name === name);
493490
}

src/type/scalars.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { print } from '../language/printer';
1111

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

14-
import { GraphQLScalarType, isScalarType } from './definition';
14+
import { type GraphQLNamedType, GraphQLScalarType } from './definition';
1515

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

274-
export function isSpecifiedScalarType(type: mixed): boolean %checks {
275-
return (
276-
isScalarType(type) &&
277-
specifiedScalarTypes.some(({ name }) => type.name === name)
278-
);
274+
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean %checks {
275+
return specifiedScalarTypes.some(({ name }) => type.name === name);
279276
}

tstypes/type/directives.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,4 @@ export const GraphQLDeprecatedDirective: GraphQLDirective;
6969
*/
7070
export const specifiedDirectives: ReadonlyArray<GraphQLDirective>;
7171

72-
export function isSpecifiedDirective(
73-
directive: any,
74-
): directive is GraphQLDirective;
72+
export function isSpecifiedDirective(directive: GraphQLDirective): boolean;

tstypes/type/introspection.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ export const TypeNameMetaFieldDef: GraphQLField<any, any>;
3737

3838
export const introspectionTypes: ReadonlyArray<GraphQLType>;
3939

40-
export function isIntrospectionType(type: any): boolean;
40+
export function isIntrospectionType(type: GraphQLType): boolean;

tstypes/type/scalars.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLScalarType } from './definition';
1+
import { GraphQLScalarType, GraphQLNamedType } from './definition';
22

33
export const GraphQLInt: GraphQLScalarType;
44
export const GraphQLFloat: GraphQLScalarType;
@@ -8,4 +8,4 @@ export const GraphQLID: GraphQLScalarType;
88

99
export const specifiedScalarTypes: ReadonlyArray<GraphQLScalarType>;
1010

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

0 commit comments

Comments
 (0)