From 43c6657016b8c4ef4db5086bccf79de55aebaea8 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Fri, 23 Aug 2019 14:11:27 -0700 Subject: [PATCH] Sync utilities TS definitions with flow (#2115) --- tstypes/utilities/TypeInfo.d.ts | 8 +- tstypes/utilities/buildASTSchema.d.ts | 24 +-- tstypes/utilities/coerceInputValue.d.ts | 17 +++ tstypes/utilities/coerceValue.d.ts | 8 +- tstypes/utilities/extendSchema.d.ts | 3 +- tstypes/utilities/findBreakingChanges.d.ts | 144 ++---------------- tstypes/utilities/findDeprecatedUsages.d.ts | 4 +- tstypes/utilities/getOperationRootType.d.ts | 2 +- tstypes/utilities/index.d.ts | 7 + tstypes/utilities/separateOperations.d.ts | 2 +- tstypes/utilities/stripIgnoredCharacters.d.ts | 55 +++++++ tstypes/utilities/typeComparators.d.ts | 6 +- tstypes/utilities/typeFromAST.d.ts | 11 +- tstypes/utilities/valueFromAST.d.ts | 7 +- 14 files changed, 116 insertions(+), 182 deletions(-) create mode 100644 tstypes/utilities/coerceInputValue.d.ts create mode 100644 tstypes/utilities/stripIgnoredCharacters.d.ts diff --git a/tstypes/utilities/TypeInfo.d.ts b/tstypes/utilities/TypeInfo.d.ts index 74d740b23d..1f2419b632 100644 --- a/tstypes/utilities/TypeInfo.d.ts +++ b/tstypes/utilities/TypeInfo.d.ts @@ -1,16 +1,16 @@ import Maybe from '../tsutils/Maybe'; +import { ASTNode, FieldNode } from '../language/ast'; import { GraphQLSchema } from '../type/schema'; +import { GraphQLDirective } from '../type/directives'; import { + GraphQLType, + GraphQLInputType, GraphQLOutputType, GraphQLCompositeType, - GraphQLInputType, GraphQLField, GraphQLArgument, GraphQLEnumValue, - GraphQLType, } from '../type/definition'; -import { GraphQLDirective } from '../type/directives'; -import { ASTNode, FieldNode } from '../language/ast'; /** * TypeInfo is a utility class which, given a GraphQL schema, can keep track diff --git a/tstypes/utilities/buildASTSchema.d.ts b/tstypes/utilities/buildASTSchema.d.ts index 9c4c58adb6..ec7e73e964 100644 --- a/tstypes/utilities/buildASTSchema.d.ts +++ b/tstypes/utilities/buildASTSchema.d.ts @@ -9,12 +9,16 @@ import { FieldDefinitionNode, InputValueDefinitionNode, EnumValueDefinitionNode, + TypeNode, } from '../language/ast'; import { GraphQLNamedType, GraphQLFieldConfig, GraphQLInputField, GraphQLEnumValueConfig, + GraphQLType, + GraphQLArgumentConfig, + GraphQLInputFieldConfig, } from '../type/definition'; import { GraphQLDirective } from '../type/directives'; import { Source } from '../language/source'; @@ -66,25 +70,23 @@ type TypeDefinitionsMap = { [key: string]: TypeDefinitionNode }; type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType; export class ASTDefinitionBuilder { - constructor( - typeDefinitionsMap: TypeDefinitionsMap, - options: Maybe, - resolveType: TypeResolver, - ); + constructor(options: Maybe, resolveType: TypeResolver); - buildTypes( - nodes: ReadonlyArray, - ): Array; + getNamedType(node: NamedTypeNode): GraphQLNamedType; - buildType(node: NamedTypeNode | TypeDefinitionNode): GraphQLNamedType; + getWrappedType(node: TypeNode): GraphQLType; - buildDirective(directiveNode: DirectiveDefinitionNode): GraphQLDirective; + buildDirective(directive: DirectiveDefinitionNode): GraphQLDirective; buildField(field: FieldDefinitionNode): GraphQLFieldConfig; - buildInputField(value: InputValueDefinitionNode): GraphQLInputField; + buildArg(value: InputValueDefinitionNode): GraphQLArgumentConfig; + + buildInputField(value: InputValueDefinitionNode): GraphQLInputFieldConfig; buildEnumValue(value: EnumValueDefinitionNode): GraphQLEnumValueConfig; + + buildType(node: NamedTypeNode | TypeDefinitionNode): GraphQLNamedType; } /** diff --git a/tstypes/utilities/coerceInputValue.d.ts b/tstypes/utilities/coerceInputValue.d.ts new file mode 100644 index 0000000000..cb8f1a581b --- /dev/null +++ b/tstypes/utilities/coerceInputValue.d.ts @@ -0,0 +1,17 @@ +import { GraphQLInputType } from '../type/definition'; +import { GraphQLError } from 'tstypes/error'; + +type OnErrorCB = ( + path: ReadonlyArray, + invalidValue: any, + error: GraphQLError, +) => void; + +/** + * Coerces a JavaScript value given a GraphQL Input Type. + */ +export function coerceInputValue( + inputValue: any, + type: GraphQLInputType, + onError?: OnErrorCB, +): any; diff --git a/tstypes/utilities/coerceValue.d.ts b/tstypes/utilities/coerceValue.d.ts index 96fafdca6a..f79389eac5 100644 --- a/tstypes/utilities/coerceValue.d.ts +++ b/tstypes/utilities/coerceValue.d.ts @@ -1,3 +1,4 @@ +import { Path } from '../jsutils/Path'; import { GraphQLError } from '../error/GraphQLError'; import { ASTNode } from '../language/ast'; import { GraphQLInputType } from '../type/definition'; @@ -7,11 +8,6 @@ interface CoercedValue { readonly value: any; } -interface Path { - readonly prev: Path | undefined; - readonly key: string | number; -} - /** * Coerces a JavaScript value given a GraphQL Type. * @@ -20,7 +16,7 @@ interface Path { * */ export function coerceValue( - value: any, + inputValue: any, type: GraphQLInputType, blameNode?: ASTNode, path?: Path, diff --git a/tstypes/utilities/extendSchema.d.ts b/tstypes/utilities/extendSchema.d.ts index 2f0007ba89..9df76c2ae4 100644 --- a/tstypes/utilities/extendSchema.d.ts +++ b/tstypes/utilities/extendSchema.d.ts @@ -1,6 +1,5 @@ import { DocumentNode } from '../language/ast'; -import { GraphQLSchema } from '../type/schema'; -import { GraphQLSchemaValidationOptions } from '../type/schema'; +import { GraphQLSchemaValidationOptions, GraphQLSchema } from '../type/schema'; interface Options extends GraphQLSchemaValidationOptions { /** diff --git a/tstypes/utilities/findBreakingChanges.d.ts b/tstypes/utilities/findBreakingChanges.d.ts index 75daa4f946..87fa0c028e 100644 --- a/tstypes/utilities/findBreakingChanges.d.ts +++ b/tstypes/utilities/findBreakingChanges.d.ts @@ -1,13 +1,3 @@ -import { - getNamedType, - GraphQLScalarType, - GraphQLEnumType, - GraphQLInputObjectType, - GraphQLInterfaceType, - GraphQLObjectType, - GraphQLUnionType, - GraphQLNamedType, -} from '../type/definition'; import { GraphQLDirective } from '../type/directives'; import { GraphQLSchema } from '../type/schema'; import { DirectiveLocationEnum } from '../language/directiveLocation'; @@ -16,33 +6,33 @@ export const BreakingChangeType: _BreakingChangeType; // @internal type _BreakingChangeType = { - FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND'; - FIELD_REMOVED: 'FIELD_REMOVED'; - TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND'; TYPE_REMOVED: 'TYPE_REMOVED'; + TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND'; TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION'; VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM'; - ARG_REMOVED: 'ARG_REMOVED'; - ARG_CHANGED_KIND: 'ARG_CHANGED_KIND'; - REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED'; REQUIRED_INPUT_FIELD_ADDED: 'REQUIRED_INPUT_FIELD_ADDED'; INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT'; + FIELD_REMOVED: 'FIELD_REMOVED'; + FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND'; + REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED'; + ARG_REMOVED: 'ARG_REMOVED'; + ARG_CHANGED_KIND: 'ARG_CHANGED_KIND'; DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED'; DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED'; - DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED'; REQUIRED_DIRECTIVE_ARG_ADDED: 'REQUIRED_DIRECTIVE_ARG_ADDED'; + DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED'; }; export const DangerousChangeType: _DangerousChangeType; // @internal type _DangerousChangeType = { - ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE'; VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM'; - INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT'; TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION'; OPTIONAL_INPUT_FIELD_ADDED: 'OPTIONAL_INPUT_FIELD_ADDED'; OPTIONAL_ARG_ADDED: 'OPTIONAL_ARG_ADDED'; + INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT'; + ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE'; }; export interface BreakingChange { @@ -72,119 +62,3 @@ export function findDangerousChanges( oldSchema: GraphQLSchema, newSchema: GraphQLSchema, ): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any breaking - * changes in the newSchema related to removing an entire type. - */ -export function findRemovedTypes( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any breaking - * changes in the newSchema related to changing the type of a type. - */ -export function findTypesThatChangedKind( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any - * breaking or dangerous changes in the newSchema related to arguments - * (such as removal or change of type of an argument, or a change in an - * argument's default value). - */ -export function findArgChanges( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): { - breakingChanges: Array; - dangerousChanges: Array; -}; - -export function findFieldsThatChangedTypeOnObjectOrInterfaceTypes( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findFieldsThatChangedTypeOnInputObjectTypes( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): { - breakingChanges: Array; - dangerousChanges: Array; -}; - -/** - * Given two schemas, returns an Array containing descriptions of any breaking - * changes in the newSchema related to removing types from a union type. - */ -export function findTypesRemovedFromUnions( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any dangerous - * changes in the newSchema related to adding types to a union type. - */ -export function findTypesAddedToUnions( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any breaking - * changes in the newSchema related to removing values from an enum type. - */ -export function findValuesRemovedFromEnums( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -/** - * Given two schemas, returns an Array containing descriptions of any dangerous - * changes in the newSchema related to adding values to an enum type. - */ -export function findValuesAddedToEnums( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findInterfacesRemovedFromObjectTypes( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findInterfacesAddedToObjectTypes( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findRemovedDirectives( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findRemovedDirectiveArgs( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findAddedNonNullDirectiveArgs( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; - -export function findRemovedLocationsForDirective( - oldDirective: GraphQLDirective, - newDirective: GraphQLDirective, -): Array; - -export function findRemovedDirectiveLocations( - oldSchema: GraphQLSchema, - newSchema: GraphQLSchema, -): Array; diff --git a/tstypes/utilities/findDeprecatedUsages.d.ts b/tstypes/utilities/findDeprecatedUsages.d.ts index 8cd844e15f..0a55f1b82d 100644 --- a/tstypes/utilities/findDeprecatedUsages.d.ts +++ b/tstypes/utilities/findDeprecatedUsages.d.ts @@ -1,6 +1,6 @@ -import { GraphQLSchema } from '../type/schema'; -import { DocumentNode } from '../language/ast'; import { GraphQLError } from '../error/GraphQLError'; +import { DocumentNode } from '../language/ast'; +import { GraphQLSchema } from '../type/schema'; /** * A validation rule which reports deprecated usages. diff --git a/tstypes/utilities/getOperationRootType.d.ts b/tstypes/utilities/getOperationRootType.d.ts index f82eeecbc1..5adc59c7af 100644 --- a/tstypes/utilities/getOperationRootType.d.ts +++ b/tstypes/utilities/getOperationRootType.d.ts @@ -1,8 +1,8 @@ -import { GraphQLSchema } from '../type/schema'; import { OperationDefinitionNode, OperationTypeDefinitionNode, } from '../language/ast'; +import { GraphQLSchema } from '../type/schema'; import { GraphQLObjectType } from '../type/definition'; /** diff --git a/tstypes/utilities/index.d.ts b/tstypes/utilities/index.d.ts index a459298c5b..f3d82f01ec 100644 --- a/tstypes/utilities/index.d.ts +++ b/tstypes/utilities/index.d.ts @@ -81,6 +81,9 @@ export { astFromValue } from './astFromValue'; // the GraphQL type system. export { TypeInfo } from './TypeInfo'; +// Coerces a JavaScript value to a GraphQL type, or produces errors. +export { coerceInputValue } from './coerceInputValue'; + // Coerces a JavaScript value to a GraphQL type, or produces errors. export { coerceValue } from './coerceValue'; @@ -96,6 +99,10 @@ export { concatAST } from './concatAST'; // Separates an AST into an AST per Operation. export { separateOperations } from './separateOperations'; +// Strips characters that are not significant to the validity or execution +// of a GraphQL document. +export { stripIgnoredCharacters } from './stripIgnoredCharacters'; + // Comparators for types export { isEqualType, diff --git a/tstypes/utilities/separateOperations.d.ts b/tstypes/utilities/separateOperations.d.ts index d3dcb6e957..28654fdb1b 100644 --- a/tstypes/utilities/separateOperations.d.ts +++ b/tstypes/utilities/separateOperations.d.ts @@ -1,4 +1,4 @@ -import { DocumentNode, OperationDefinitionNode } from '../language/ast'; +import { DocumentNode } from '../language/ast'; /** * separateOperations accepts a single AST document which may contain many diff --git a/tstypes/utilities/stripIgnoredCharacters.d.ts b/tstypes/utilities/stripIgnoredCharacters.d.ts new file mode 100644 index 0000000000..a131af02f5 --- /dev/null +++ b/tstypes/utilities/stripIgnoredCharacters.d.ts @@ -0,0 +1,55 @@ +import { Source } from '../language/source'; + +/** + * Strips characters that are not significant to the validity or execution + * of a GraphQL document: + * - UnicodeBOM + * - WhiteSpace + * - LineTerminator + * - Comment + * - Comma + * - BlockString indentation + * + * Note: It is required to have a delimiter character between neighboring + * non-punctuator tokens and this function always uses single space as delimiter. + * + * It is guaranteed that both input and output documents if parsed would result + * in the exact same AST except for nodes location. + * + * Warning: It is guaranteed that this function will always produce stable results. + * However, it's not guaranteed that it will stay the same between different + * releases due to bugfixes or changes in the GraphQL specification. + * + * Query example: + * + * query SomeQuery($foo: String!, $bar: String) { + * someField(foo: $foo, bar: $bar) { + * a + * b { + * c + * d + * } + * } + * } + * + * Becomes: + * + * query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}} + * + * SDL example: + * + * """ + * Type description + * """ + * type Foo { + * """ + * Field description + * """ + * bar: String + * } + * + * Becomes: + * + * """Type description""" type Foo{"""Field description""" bar:String} + */ +export function stripIgnoredCharacters(source: string | Source): string; diff --git a/tstypes/utilities/typeComparators.d.ts b/tstypes/utilities/typeComparators.d.ts index ca0cdaf6c2..7de3e00b68 100644 --- a/tstypes/utilities/typeComparators.d.ts +++ b/tstypes/utilities/typeComparators.d.ts @@ -1,9 +1,5 @@ -import { - GraphQLType, - GraphQLCompositeType, - GraphQLAbstractType, -} from '../type/definition'; import { GraphQLSchema } from '../type/schema'; +import { GraphQLType, GraphQLCompositeType } from '../type/definition'; /** * Provided two types, return true if the types are equal (invariant). diff --git a/tstypes/utilities/typeFromAST.d.ts b/tstypes/utilities/typeFromAST.d.ts index 10e467dee9..cf32ac7cee 100644 --- a/tstypes/utilities/typeFromAST.d.ts +++ b/tstypes/utilities/typeFromAST.d.ts @@ -1,17 +1,10 @@ +import { NamedTypeNode, ListTypeNode, NonNullTypeNode } from '../language/ast'; +import { GraphQLSchema } from '../type/schema'; import { - TypeNode, - NamedTypeNode, - ListTypeNode, - NonNullTypeNode, -} from '../language/ast'; -import { - GraphQLType, - GraphQLNullableType, GraphQLNamedType, GraphQLList, GraphQLNonNull, } from '../type/definition'; -import { GraphQLSchema } from '../type/schema'; /** * Given a Schema and an AST node describing a type, return a GraphQLType diff --git a/tstypes/utilities/valueFromAST.d.ts b/tstypes/utilities/valueFromAST.d.ts index 06a130aebf..bef11b75d3 100644 --- a/tstypes/utilities/valueFromAST.d.ts +++ b/tstypes/utilities/valueFromAST.d.ts @@ -1,11 +1,6 @@ import Maybe from '../tsutils/Maybe'; +import { ValueNode } from '../language/ast'; import { GraphQLInputType } from '../type/definition'; -import { - ValueNode, - VariableNode, - ListValueNode, - ObjectValueNode, -} from '../language/ast'; /** * Produces a JavaScript value given a GraphQL Value AST.