From 4f4135507f6c9121e6bd3ffb29f946b69af31136 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 14 Mar 2021 00:20:11 +0200 Subject: [PATCH] flow: improve typings of exported definitions (#2967) --- src/type/directives.js | 78 ++++++++++++++++++++------------------- src/type/introspection.js | 40 ++++++++++---------- src/type/scalars.js | 22 +++++------ 3 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/type/directives.js b/src/type/directives.js index 00b57ca1dd..437e54132b 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -126,7 +126,7 @@ type GraphQLDirectiveNormalizedConfig = {| /** * Used to conditionally include fields or fragments. */ -export const GraphQLIncludeDirective = new GraphQLDirective({ +export const GraphQLIncludeDirective: GraphQLDirective = new GraphQLDirective({ name: 'include', description: '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({ /** * Used to conditionally skip (exclude) fields or fragments. */ -export const GraphQLSkipDirective = new GraphQLDirective({ +export const GraphQLSkipDirective: GraphQLDirective = new GraphQLDirective({ name: 'skip', description: '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'; /** * Used to declare element of a GraphQL schema as deprecated. */ -export const GraphQLDeprecatedDirective = new GraphQLDirective({ - name: 'deprecated', - description: 'Marks an element of a GraphQL schema as no longer supported.', - locations: [ - DirectiveLocation.FIELD_DEFINITION, - DirectiveLocation.ARGUMENT_DEFINITION, - DirectiveLocation.INPUT_FIELD_DEFINITION, - DirectiveLocation.ENUM_VALUE, - ], - args: { - reason: { - type: GraphQLString, - description: - '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/).', - defaultValue: DEFAULT_DEPRECATION_REASON, +export const GraphQLDeprecatedDirective: GraphQLDirective = new GraphQLDirective( + { + name: 'deprecated', + description: 'Marks an element of a GraphQL schema as no longer supported.', + locations: [ + DirectiveLocation.FIELD_DEFINITION, + DirectiveLocation.ARGUMENT_DEFINITION, + DirectiveLocation.INPUT_FIELD_DEFINITION, + DirectiveLocation.ENUM_VALUE, + ], + args: { + reason: { + type: GraphQLString, + description: + '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/).', + defaultValue: DEFAULT_DEPRECATION_REASON, + }, }, }, -}); +); /** * Used to provide a URL for specifying the behaviour of custom scalar definitions. */ -export const GraphQLSpecifiedByDirective = new GraphQLDirective({ - name: 'specifiedBy', - description: 'Exposes a URL that specifies the behaviour of this scalar.', - locations: [DirectiveLocation.SCALAR], - args: { - url: { - type: new GraphQLNonNull(GraphQLString), - description: 'The URL that specifies the behaviour of this scalar.', +export const GraphQLSpecifiedByDirective: GraphQLDirective = new GraphQLDirective( + { + name: 'specifiedBy', + description: 'Exposes a URL that specifies the behaviour of this scalar.', + locations: [DirectiveLocation.SCALAR], + args: { + url: { + type: new GraphQLNonNull(GraphQLString), + description: 'The URL that specifies the behaviour of this scalar.', + }, }, }, -}); +); /** * The full list of specified directives. */ -export const specifiedDirectives = Object.freeze([ - GraphQLIncludeDirective, - GraphQLSkipDirective, - GraphQLDeprecatedDirective, - GraphQLSpecifiedByDirective, -]); - -export function isSpecifiedDirective( - directive: GraphQLDirective, -): boolean %checks { +export const specifiedDirectives: $ReadOnlyArray = Object.freeze( + [ + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, + GraphQLSpecifiedByDirective, + ], +); + +export function isSpecifiedDirective(directive: GraphQLDirective): boolean { return specifiedDirectives.some(({ name }) => name === directive.name); } diff --git a/src/type/introspection.js b/src/type/introspection.js index 75e2d7f474..826fb58aa8 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -32,7 +32,7 @@ import { isAbstractType, } from './definition'; -export const __Schema = new GraphQLObjectType({ +export const __Schema: GraphQLObjectType = new GraphQLObjectType({ name: '__Schema', description: '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({ }: GraphQLFieldConfigMap), }); -export const __Directive = new GraphQLObjectType({ +export const __Directive: GraphQLObjectType = new GraphQLObjectType({ name: '__Directive', description: "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({ }: GraphQLFieldConfigMap), }); -export const __DirectiveLocation = new GraphQLEnumType({ +export const __DirectiveLocation: GraphQLEnumType = new GraphQLEnumType({ name: '__DirectiveLocation', description: '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({ }, }); -export const __Type = new GraphQLObjectType({ +export const __Type: GraphQLObjectType = new GraphQLObjectType({ name: '__Type', description: '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({ }: GraphQLFieldConfigMap), }); -export const __Field = new GraphQLObjectType({ +export const __Field: GraphQLObjectType = new GraphQLObjectType({ name: '__Field', description: '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({ }: GraphQLFieldConfigMap, mixed>), }); -export const __InputValue = new GraphQLObjectType({ +export const __InputValue: GraphQLObjectType = new GraphQLObjectType({ name: '__InputValue', description: '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({ }: GraphQLFieldConfigMap), }); -export const __EnumValue = new GraphQLObjectType({ +export const __EnumValue: GraphQLObjectType = new GraphQLObjectType({ name: '__EnumValue', description: '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({ NON_NULL: 'NON_NULL', }); -export const __TypeKind = new GraphQLEnumType({ +export const __TypeKind: GraphQLEnumType = new GraphQLEnumType({ name: '__TypeKind', description: 'An enum describing what kind of type a given `__Type` is.', values: { @@ -528,17 +528,19 @@ export const TypeNameMetaFieldDef: GraphQLField = { astNode: undefined, }; -export const introspectionTypes = Object.freeze([ - __Schema, - __Directive, - __DirectiveLocation, - __Type, - __Field, - __InputValue, - __EnumValue, - __TypeKind, -]); +export const introspectionTypes: $ReadOnlyArray = Object.freeze( + [ + __Schema, + __Directive, + __DirectiveLocation, + __Type, + __Field, + __InputValue, + __EnumValue, + __TypeKind, + ], +); -export function isIntrospectionType(type: GraphQLNamedType): boolean %checks { +export function isIntrospectionType(type: GraphQLNamedType): boolean { return introspectionTypes.some(({ name }) => type.name === name); } diff --git a/src/type/scalars.js b/src/type/scalars.js index f737cc2cff..0d9bdc955b 100644 --- a/src/type/scalars.js +++ b/src/type/scalars.js @@ -57,7 +57,7 @@ function coerceInt(inputValue: mixed): number { return inputValue; } -export const GraphQLInt = new GraphQLScalarType({ +export const GraphQLInt: GraphQLScalarType = new GraphQLScalarType({ name: 'Int', description: '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 { return inputValue; } -export const GraphQLFloat = new GraphQLScalarType({ +export const GraphQLFloat: GraphQLScalarType = new GraphQLScalarType({ name: 'Float', description: '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 { return inputValue; } -export const GraphQLString = new GraphQLScalarType({ +export const GraphQLString: GraphQLScalarType = new GraphQLScalarType({ name: 'String', description: '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 { return inputValue; } -export const GraphQLBoolean = new GraphQLScalarType({ +export const GraphQLBoolean: GraphQLScalarType = new GraphQLScalarType({ name: 'Boolean', description: 'The `Boolean` scalar type represents `true` or `false`.', serialize: serializeBoolean, @@ -252,7 +252,7 @@ function coerceID(inputValue: mixed): string { throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`); } -export const GraphQLID = new GraphQLScalarType({ +export const GraphQLID: GraphQLScalarType = new GraphQLScalarType({ name: 'ID', description: '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({ }, }); -export const specifiedScalarTypes = Object.freeze([ - GraphQLString, - GraphQLInt, - GraphQLFloat, - GraphQLBoolean, - GraphQLID, -]); +export const specifiedScalarTypes: $ReadOnlyArray = Object.freeze( + [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID], +); -export function isSpecifiedScalarType(type: GraphQLNamedType): boolean %checks { +export function isSpecifiedScalarType(type: GraphQLNamedType): boolean { return specifiedScalarTypes.some(({ name }) => type.name === name); }