Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow: improve typings of exported definitions #2967

Merged
merged 1 commit into from Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 41 additions & 37 deletions src/type/directives.js
Expand Up @@ -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.',
Expand All @@ -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.',
Expand All @@ -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<GraphQLDirective> = Object.freeze(
[
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
],
);

export function isSpecifiedDirective(directive: GraphQLDirective): boolean {
return specifiedDirectives.some(({ name }) => name === directive.name);
}
40 changes: 21 additions & 19 deletions src/type/introspection.js
Expand Up @@ -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.',
Expand Down Expand Up @@ -76,7 +76,7 @@ export const __Schema = new GraphQLObjectType({
}: GraphQLFieldConfigMap<GraphQLSchema, mixed>),
});

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.",
Expand Down Expand Up @@ -109,7 +109,7 @@ export const __Directive = new GraphQLObjectType({
}: GraphQLFieldConfigMap<GraphQLDirective, mixed>),
});

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.',
Expand Down Expand Up @@ -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.',
Expand Down Expand Up @@ -315,7 +315,7 @@ export const __Type = new GraphQLObjectType({
}: GraphQLFieldConfigMap<GraphQLType, mixed>),
});

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.',
Expand Down Expand Up @@ -360,7 +360,7 @@ export const __Field = new GraphQLObjectType({
}: GraphQLFieldConfigMap<GraphQLField<mixed, mixed>, 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.',
Expand Down Expand Up @@ -399,7 +399,7 @@ export const __InputValue = new GraphQLObjectType({
}: GraphQLFieldConfigMap<GraphQLInputField, mixed>),
});

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.',
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -528,17 +528,19 @@ export const TypeNameMetaFieldDef: GraphQLField<mixed, mixed> = {
astNode: undefined,
};

export const introspectionTypes = Object.freeze([
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
]);
export const introspectionTypes: $ReadOnlyArray<GraphQLNamedType> = 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);
}
22 changes: 9 additions & 13 deletions src/type/scalars.js
Expand Up @@ -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.',
Expand Down Expand Up @@ -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).',
Expand Down Expand Up @@ -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.',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.',
Expand All @@ -270,14 +270,10 @@ export const GraphQLID = new GraphQLScalarType({
},
});

export const specifiedScalarTypes = Object.freeze([
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
]);
export const specifiedScalarTypes: $ReadOnlyArray<GraphQLScalarType> = 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);
}