Skip to content

Commit ac70985

Browse files
committed
TS: Fix strict issues in src/type
1 parent 5accb29 commit ac70985

File tree

8 files changed

+68
-53
lines changed

8 files changed

+68
-53
lines changed

src/type/__tests__/definition-test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ describe('Type System: Scalars', () => {
8383
},
8484
});
8585

86-
// @ts-expect-error FIXME: TS Conversion
8786
expect(scalar.parseLiteral(parseValue('null'))).to.equal(
8887
'parseValue: null',
8988
);
90-
// @ts-expect-error FIXME: TS Conversion
9189
expect(scalar.parseLiteral(parseValue('{ foo: "bar" }'))).to.equal(
9290
'parseValue: { foo: "bar" }',
9391
);
@@ -335,6 +333,7 @@ describe('Type System: Objects', () => {
335333
const objType = new GraphQLObjectType({
336334
name: 'SomeObject',
337335
fields: {
336+
// TODO ts-expect-error (must not be undefined)
338337
f: undefined,
339338
},
340339
});
@@ -357,7 +356,7 @@ describe('Type System: Objects', () => {
357356
it('rejects an Object type with a field function that returns incorrect type', () => {
358357
const objType = new GraphQLObjectType({
359358
name: 'SomeObject',
360-
// @ts-expect-error FIXME: TS Conversion
359+
// @ts-expect-error (Wrong type of return)
361360
fields() {
362361
return [{ field: ScalarType }];
363362
},
@@ -397,7 +396,7 @@ describe('Type System: Objects', () => {
397396
const objType = new GraphQLObjectType({
398397
name: 'SomeObject',
399398
fields: {},
400-
// @ts-expect-error FIXME: TS Conversion
399+
// @ts-expect-error (Expected interfaces to return array)
401400
interfaces() {
402401
return {};
403402
},
@@ -411,7 +410,7 @@ describe('Type System: Objects', () => {
411410
const objType = new GraphQLObjectType({
412411
name: 'SomeObject',
413412
fields: {
414-
// @ts-expect-error FIXME: TS Conversion
413+
// @ts-expect-error (Expected resolve to be a function)
415414
field: { type: ScalarType, resolve: {} },
416415
},
417416
});
@@ -425,7 +424,7 @@ describe('Type System: Objects', () => {
425424
const objType = new GraphQLObjectType({
426425
name: 'SomeObject',
427426
fields: {
428-
// @ts-expect-error FIXME: TS Conversion
427+
// @ts-expect-error (Expected resolve to be a function)
429428
field: { type: ScalarType, resolve: 0 },
430429
},
431430
});
@@ -500,7 +499,7 @@ describe('Type System: Interfaces', () => {
500499
const objType = new GraphQLInterfaceType({
501500
name: 'AnotherInterface',
502501
fields: {},
503-
// @ts-expect-error FIXME: TS Conversion
502+
// @ts-expect-error (Expected Array return)
504503
interfaces() {
505504
return {};
506505
},
@@ -698,6 +697,7 @@ describe('Type System: Enums', () => {
698697
() =>
699698
new GraphQLEnumType({
700699
name: 'SomeEnum',
700+
// TODO ts-expect-error (must not be null)
701701
values: { FOO: null },
702702
}),
703703
).to.throw(
@@ -796,7 +796,7 @@ describe('Type System: Input Objects', () => {
796796
const inputObjType = new GraphQLInputObjectType({
797797
name: 'SomeInputObject',
798798
fields: {
799-
// @ts-expect-error FIXME: TS Conversion
799+
// @ts-expect-error (Input fields cannot have resolvers)
800800
f: { type: ScalarType, resolve: dummyFunc },
801801
},
802802
});
@@ -809,7 +809,7 @@ describe('Type System: Input Objects', () => {
809809
const inputObjType = new GraphQLInputObjectType({
810810
name: 'SomeInputObject',
811811
fields: {
812-
// @ts-expect-error FIXME: TS Conversion
812+
// @ts-expect-error (Input fields cannot have resolvers)
813813
f: { type: ScalarType, resolve: {} },
814814
},
815815
});
@@ -843,7 +843,9 @@ describe('Type System: List', () => {
843843
expectList(String).to.throw(
844844
'Expected [function String] to be a GraphQL type.',
845845
);
846+
// TODO ts-expect-error (must provide type)
846847
expectList(null).to.throw('Expected null to be a GraphQL type.');
848+
// TODO ts-expect-error (must provide type)
847849
expectList(undefined).to.throw('Expected undefined to be a GraphQL type.');
848850
});
849851
});
@@ -874,9 +876,11 @@ describe('Type System: Non-Null', () => {
874876
expectNonNull(String).to.throw(
875877
'Expected [function String] to be a GraphQL nullable type.',
876878
);
879+
// TODO ts-expect-error (must provide type)
877880
expectNonNull(null).to.throw(
878881
'Expected null to be a GraphQL nullable type.',
879882
);
883+
// TODO ts-expect-error (must provide type)
880884
expectNonNull(undefined).to.throw(
881885
'Expected undefined to be a GraphQL nullable type.',
882886
);

src/type/__tests__/introspection-test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { getIntrospectionQuery } from '../../utilities/getIntrospectionQuery';
66

77
import { graphqlSync } from '../../graphql';
88

9+
import type { GraphQLResolveInfo } from '../definition';
10+
911
describe('Introspection', () => {
1012
it('executes an introspection query', () => {
1113
const schema = buildSchema(`
@@ -1566,12 +1568,17 @@ describe('Introspection', () => {
15661568
});
15671569

15681570
// istanbul ignore next (Called only to fail test)
1569-
function fieldResolver(_1, _2, _3, info): never {
1571+
function fieldResolver(
1572+
_1: any,
1573+
_2: any,
1574+
_3: any,
1575+
info: GraphQLResolveInfo,
1576+
): never {
15701577
expect.fail(`Called on ${info.parentType.name}::${info.fieldName}`);
15711578
}
15721579

15731580
// istanbul ignore next (Called only to fail test)
1574-
function typeResolver(_1, _2, info): never {
1581+
function typeResolver(_1: any, _2: any, info: GraphQLResolveInfo): never {
15751582
expect.fail(`Called on ${info.parentType.name}::${info.fieldName}`);
15761583
}
15771584

src/type/__tests__/scalars-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ describe('Type System: Specified scalar types', () => {
507507
serialize({
508508
value: true,
509509
valueOf() {
510-
return this.value;
510+
return (this as { value: boolean }).value;
511511
},
512512
}),
513513
).to.equal(true);

src/type/__tests__/schema-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Type System: Schema', () => {
2727
},
2828
});
2929

30-
const BlogAuthor = new GraphQLObjectType({
30+
const BlogAuthor: GraphQLObjectType = new GraphQLObjectType({
3131
name: 'Author',
3232
fields: () => ({
3333
id: { type: GraphQLString },
@@ -40,7 +40,7 @@ describe('Type System: Schema', () => {
4040
}),
4141
});
4242

43-
const BlogArticle = new GraphQLObjectType({
43+
const BlogArticle: GraphQLObjectType = new GraphQLObjectType({
4444
name: 'Article',
4545
fields: {
4646
id: { type: GraphQLString },

src/type/__tests__/validation-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ describe('Type System: Object fields must have output types', () => {
10361036
}
10371037

10381038
it('rejects an empty Object field type', () => {
1039+
// TODO ts-expect-error (type field must not be undefined)
10391040
const schema = schemaWithObjectField({ type: undefined });
10401041
expect(validateSchema(schema)).to.deep.equal([
10411042
{
@@ -1097,6 +1098,7 @@ describe('Type System: Objects can only implement unique interfaces', () => {
10971098
const schema = new GraphQLSchema({
10981099
query: new GraphQLObjectType({
10991100
name: 'BadObject',
1101+
// TODO ts-expect-error (interfaces must not contain undefined)
11001102
interfaces: [undefined],
11011103
fields: { f: { type: GraphQLString } },
11021104
}),
@@ -1355,6 +1357,7 @@ describe('Type System: Interface fields must have output types', () => {
13551357
}
13561358

13571359
it('rejects an empty Interface field type', () => {
1360+
// TODO ts-expect-error (type field must not be undefined)
13581361
const schema = schemaWithInterfaceField({ type: undefined });
13591362
expect(validateSchema(schema)).to.deep.equal([
13601363
{
@@ -1490,6 +1493,7 @@ describe('Type System: Arguments must have input types', () => {
14901493
}
14911494

14921495
it('rejects an empty field arg type', () => {
1496+
// TODO ts-expect-error (type field must not be undefined)
14931497
const schema = schemaWithArg({ type: undefined });
14941498
expect(validateSchema(schema)).to.deep.equal([
14951499
{
@@ -1627,6 +1631,7 @@ describe('Type System: Input Object fields must have input types', () => {
16271631
}
16281632

16291633
it('rejects an empty input field type', () => {
1634+
// TODO ts-expect-error (type field must not be undefined)
16301635
const schema = schemaWithInputField({ type: undefined });
16311636
expect(validateSchema(schema)).to.deep.equal([
16321637
{

src/type/definition.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ export type GraphQLScalarValueParser<TInternal> = (
659659

660660
export type GraphQLScalarLiteralParser<TInternal> = (
661661
valueNode: ValueNode,
662-
variables: Maybe<ObjMap<unknown>>,
662+
variables?: Maybe<ObjMap<unknown>>,
663663
) => Maybe<TInternal>;
664664

665665
export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
@@ -758,8 +758,8 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
758758
this.astNode = config.astNode;
759759
this.extensionASTNodes = config.extensionASTNodes ?? [];
760760

761-
this._fields = defineFieldMap.bind(undefined, config);
762-
this._interfaces = defineInterfaces.bind(undefined, config);
761+
this._fields = () => defineFieldMap(config);
762+
this._interfaces = () => defineInterfaces(config);
763763
devAssert(typeof config.name === 'string', 'Must provide name.');
764764
devAssert(
765765
config.isTypeOf == null || typeof config.isTypeOf === 'function',
@@ -810,8 +810,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
810810

811811
function defineInterfaces(
812812
config: Readonly<
813-
| GraphQLObjectTypeConfig<unknown, unknown>
814-
| GraphQLInterfaceTypeConfig<unknown, unknown>
813+
GraphQLObjectTypeConfig<any, any> | GraphQLInterfaceTypeConfig<any, any>
815814
>,
816815
): Array<GraphQLInterfaceType> {
817816
const interfaces = resolveArrayThunk(config.interfaces ?? []);
@@ -834,7 +833,6 @@ function defineFieldMap<TSource, TContext>(
834833
`${config.name} fields must be an object with field names as keys or a function which returns such an object.`,
835834
);
836835

837-
// @ts-expect-error FIXME: TS Conversion
838836
return mapValue(fieldMap, (fieldConfig, fieldName) => {
839837
devAssert(
840838
isPlainObj(fieldConfig),
@@ -1048,7 +1046,7 @@ export interface GraphQLField<
10481046
name: string;
10491047
description: Maybe<string>;
10501048
type: GraphQLOutputType;
1051-
args: Array<GraphQLArgument>;
1049+
args: ReadonlyArray<GraphQLArgument>;
10521050
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
10531051
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>;
10541052
deprecationReason: Maybe<string>;

src/type/introspection.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,18 @@ export const __Type: GraphQLObjectType = new GraphQLObjectType({
234234
},
235235
name: {
236236
type: GraphQLString,
237-
// @ts-expect-error FIXME: TS Conversion
238-
resolve: (type) => (type.name !== undefined ? type.name : undefined),
237+
resolve: (type) => ('name' in type ? type.name : undefined),
239238
},
240239
description: {
241240
type: GraphQLString,
242241
resolve: (type) =>
243-
// @ts-expect-error FIXME: TS Conversion
244-
type.description !== undefined ? type.description : undefined,
242+
// istanbul ignore next (FIXME: add test case)
243+
'description' in type ? type.description : undefined,
245244
},
246245
specifiedByURL: {
247246
type: GraphQLString,
248247
resolve: (obj) =>
249-
// @ts-expect-error FIXME: TS Conversion
250-
obj.specifiedByURL !== undefined ? obj.specifiedByURL : undefined,
248+
'specifiedByURL' in obj ? obj.specifiedByURL : undefined,
251249
},
252250
fields: {
253251
type: new GraphQLList(new GraphQLNonNull(__Field)),
@@ -312,9 +310,7 @@ export const __Type: GraphQLObjectType = new GraphQLObjectType({
312310
},
313311
ofType: {
314312
type: __Type,
315-
resolve: (type) =>
316-
// @ts-expect-error FIXME: TS Conversion
317-
type.ofType !== undefined ? type.ofType : undefined,
313+
resolve: (type) => ('ofType' in type ? type.ofType : undefined),
318314
},
319315
} as GraphQLFieldConfigMap<GraphQLType, unknown>),
320316
});

0 commit comments

Comments
 (0)