Skip to content

Commit 4f41355

Browse files
flow: improve typings of exported definitions (#2967)
1 parent 8109e42 commit 4f41355

File tree

3 files changed

+71
-69
lines changed

3 files changed

+71
-69
lines changed

src/type/directives.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ type GraphQLDirectiveNormalizedConfig = {|
126126
/**
127127
* Used to conditionally include fields or fragments.
128128
*/
129-
export const GraphQLIncludeDirective = new GraphQLDirective({
129+
export const GraphQLIncludeDirective: GraphQLDirective = new GraphQLDirective({
130130
name: 'include',
131131
description:
132132
'Directs the executor to include this field or fragment only when the `if` argument is true.',
@@ -146,7 +146,7 @@ export const GraphQLIncludeDirective = new GraphQLDirective({
146146
/**
147147
* Used to conditionally skip (exclude) fields or fragments.
148148
*/
149-
export const GraphQLSkipDirective = new GraphQLDirective({
149+
export const GraphQLSkipDirective: GraphQLDirective = new GraphQLDirective({
150150
name: 'skip',
151151
description:
152152
'Directs the executor to skip this field or fragment when the `if` argument is true.',
@@ -171,52 +171,56 @@ export const DEFAULT_DEPRECATION_REASON = 'No longer supported';
171171
/**
172172
* Used to declare element of a GraphQL schema as deprecated.
173173
*/
174-
export const GraphQLDeprecatedDirective = new GraphQLDirective({
175-
name: 'deprecated',
176-
description: 'Marks an element of a GraphQL schema as no longer supported.',
177-
locations: [
178-
DirectiveLocation.FIELD_DEFINITION,
179-
DirectiveLocation.ARGUMENT_DEFINITION,
180-
DirectiveLocation.INPUT_FIELD_DEFINITION,
181-
DirectiveLocation.ENUM_VALUE,
182-
],
183-
args: {
184-
reason: {
185-
type: GraphQLString,
186-
description:
187-
'Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).',
188-
defaultValue: DEFAULT_DEPRECATION_REASON,
174+
export const GraphQLDeprecatedDirective: GraphQLDirective = new GraphQLDirective(
175+
{
176+
name: 'deprecated',
177+
description: 'Marks an element of a GraphQL schema as no longer supported.',
178+
locations: [
179+
DirectiveLocation.FIELD_DEFINITION,
180+
DirectiveLocation.ARGUMENT_DEFINITION,
181+
DirectiveLocation.INPUT_FIELD_DEFINITION,
182+
DirectiveLocation.ENUM_VALUE,
183+
],
184+
args: {
185+
reason: {
186+
type: GraphQLString,
187+
description:
188+
'Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).',
189+
defaultValue: DEFAULT_DEPRECATION_REASON,
190+
},
189191
},
190192
},
191-
});
193+
);
192194

193195
/**
194196
* Used to provide a URL for specifying the behaviour of custom scalar definitions.
195197
*/
196-
export const GraphQLSpecifiedByDirective = new GraphQLDirective({
197-
name: 'specifiedBy',
198-
description: 'Exposes a URL that specifies the behaviour of this scalar.',
199-
locations: [DirectiveLocation.SCALAR],
200-
args: {
201-
url: {
202-
type: new GraphQLNonNull(GraphQLString),
203-
description: 'The URL that specifies the behaviour of this scalar.',
198+
export const GraphQLSpecifiedByDirective: GraphQLDirective = new GraphQLDirective(
199+
{
200+
name: 'specifiedBy',
201+
description: 'Exposes a URL that specifies the behaviour of this scalar.',
202+
locations: [DirectiveLocation.SCALAR],
203+
args: {
204+
url: {
205+
type: new GraphQLNonNull(GraphQLString),
206+
description: 'The URL that specifies the behaviour of this scalar.',
207+
},
204208
},
205209
},
206-
});
210+
);
207211

208212
/**
209213
* The full list of specified directives.
210214
*/
211-
export const specifiedDirectives = Object.freeze([
212-
GraphQLIncludeDirective,
213-
GraphQLSkipDirective,
214-
GraphQLDeprecatedDirective,
215-
GraphQLSpecifiedByDirective,
216-
]);
217-
218-
export function isSpecifiedDirective(
219-
directive: GraphQLDirective,
220-
): boolean %checks {
215+
export const specifiedDirectives: $ReadOnlyArray<GraphQLDirective> = Object.freeze(
216+
[
217+
GraphQLIncludeDirective,
218+
GraphQLSkipDirective,
219+
GraphQLDeprecatedDirective,
220+
GraphQLSpecifiedByDirective,
221+
],
222+
);
223+
224+
export function isSpecifiedDirective(directive: GraphQLDirective): boolean {
221225
return specifiedDirectives.some(({ name }) => name === directive.name);
222226
}

src/type/introspection.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
isAbstractType,
3333
} from './definition';
3434

35-
export const __Schema = new GraphQLObjectType({
35+
export const __Schema: GraphQLObjectType = new GraphQLObjectType({
3636
name: '__Schema',
3737
description:
3838
'A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.',
@@ -76,7 +76,7 @@ export const __Schema = new GraphQLObjectType({
7676
}: GraphQLFieldConfigMap<GraphQLSchema, mixed>),
7777
});
7878

79-
export const __Directive = new GraphQLObjectType({
79+
export const __Directive: GraphQLObjectType = new GraphQLObjectType({
8080
name: '__Directive',
8181
description:
8282
"A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.",
@@ -109,7 +109,7 @@ export const __Directive = new GraphQLObjectType({
109109
}: GraphQLFieldConfigMap<GraphQLDirective, mixed>),
110110
});
111111

112-
export const __DirectiveLocation = new GraphQLEnumType({
112+
export const __DirectiveLocation: GraphQLEnumType = new GraphQLEnumType({
113113
name: '__DirectiveLocation',
114114
description:
115115
'A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.',
@@ -193,7 +193,7 @@ export const __DirectiveLocation = new GraphQLEnumType({
193193
},
194194
});
195195

196-
export const __Type = new GraphQLObjectType({
196+
export const __Type: GraphQLObjectType = new GraphQLObjectType({
197197
name: '__Type',
198198
description:
199199
'The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name, description and optional `specifiedByUrl`, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.',
@@ -315,7 +315,7 @@ export const __Type = new GraphQLObjectType({
315315
}: GraphQLFieldConfigMap<GraphQLType, mixed>),
316316
});
317317

318-
export const __Field = new GraphQLObjectType({
318+
export const __Field: GraphQLObjectType = new GraphQLObjectType({
319319
name: '__Field',
320320
description:
321321
'Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.',
@@ -360,7 +360,7 @@ export const __Field = new GraphQLObjectType({
360360
}: GraphQLFieldConfigMap<GraphQLField<mixed, mixed>, mixed>),
361361
});
362362

363-
export const __InputValue = new GraphQLObjectType({
363+
export const __InputValue: GraphQLObjectType = new GraphQLObjectType({
364364
name: '__InputValue',
365365
description:
366366
'Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.',
@@ -399,7 +399,7 @@ export const __InputValue = new GraphQLObjectType({
399399
}: GraphQLFieldConfigMap<GraphQLInputField, mixed>),
400400
});
401401

402-
export const __EnumValue = new GraphQLObjectType({
402+
export const __EnumValue: GraphQLObjectType = new GraphQLObjectType({
403403
name: '__EnumValue',
404404
description:
405405
'One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.',
@@ -435,7 +435,7 @@ export const TypeKind = Object.freeze({
435435
NON_NULL: 'NON_NULL',
436436
});
437437

438-
export const __TypeKind = new GraphQLEnumType({
438+
export const __TypeKind: GraphQLEnumType = new GraphQLEnumType({
439439
name: '__TypeKind',
440440
description: 'An enum describing what kind of type a given `__Type` is.',
441441
values: {
@@ -528,17 +528,19 @@ export const TypeNameMetaFieldDef: GraphQLField<mixed, mixed> = {
528528
astNode: undefined,
529529
};
530530

531-
export const introspectionTypes = Object.freeze([
532-
__Schema,
533-
__Directive,
534-
__DirectiveLocation,
535-
__Type,
536-
__Field,
537-
__InputValue,
538-
__EnumValue,
539-
__TypeKind,
540-
]);
531+
export const introspectionTypes: $ReadOnlyArray<GraphQLNamedType> = Object.freeze(
532+
[
533+
__Schema,
534+
__Directive,
535+
__DirectiveLocation,
536+
__Type,
537+
__Field,
538+
__InputValue,
539+
__EnumValue,
540+
__TypeKind,
541+
],
542+
);
541543

542-
export function isIntrospectionType(type: GraphQLNamedType): boolean %checks {
544+
export function isIntrospectionType(type: GraphQLNamedType): boolean {
543545
return introspectionTypes.some(({ name }) => type.name === name);
544546
}

src/type/scalars.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function coerceInt(inputValue: mixed): number {
5757
return inputValue;
5858
}
5959

60-
export const GraphQLInt = new GraphQLScalarType({
60+
export const GraphQLInt: GraphQLScalarType = new GraphQLScalarType({
6161
name: 'Int',
6262
description:
6363
'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
@@ -110,7 +110,7 @@ function coerceFloat(inputValue: mixed): number {
110110
return inputValue;
111111
}
112112

113-
export const GraphQLFloat = new GraphQLScalarType({
113+
export const GraphQLFloat: GraphQLScalarType = new GraphQLScalarType({
114114
name: 'Float',
115115
description:
116116
'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
@@ -174,7 +174,7 @@ function coerceString(inputValue: mixed): string {
174174
return inputValue;
175175
}
176176

177-
export const GraphQLString = new GraphQLScalarType({
177+
export const GraphQLString: GraphQLScalarType = new GraphQLScalarType({
178178
name: 'String',
179179
description:
180180
'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
@@ -214,7 +214,7 @@ function coerceBoolean(inputValue: mixed): boolean {
214214
return inputValue;
215215
}
216216

217-
export const GraphQLBoolean = new GraphQLScalarType({
217+
export const GraphQLBoolean: GraphQLScalarType = new GraphQLScalarType({
218218
name: 'Boolean',
219219
description: 'The `Boolean` scalar type represents `true` or `false`.',
220220
serialize: serializeBoolean,
@@ -252,7 +252,7 @@ function coerceID(inputValue: mixed): string {
252252
throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`);
253253
}
254254

255-
export const GraphQLID = new GraphQLScalarType({
255+
export const GraphQLID: GraphQLScalarType = new GraphQLScalarType({
256256
name: 'ID',
257257
description:
258258
'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
@@ -270,14 +270,10 @@ export const GraphQLID = new GraphQLScalarType({
270270
},
271271
});
272272

273-
export const specifiedScalarTypes = Object.freeze([
274-
GraphQLString,
275-
GraphQLInt,
276-
GraphQLFloat,
277-
GraphQLBoolean,
278-
GraphQLID,
279-
]);
273+
export const specifiedScalarTypes: $ReadOnlyArray<GraphQLScalarType> = Object.freeze(
274+
[GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID],
275+
);
280276

281-
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean %checks {
277+
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean {
282278
return specifiedScalarTypes.some(({ name }) => type.name === name);
283279
}

0 commit comments

Comments
 (0)