From 8911354da21d9c9b1111b2a52761f03b92025589 Mon Sep 17 00:00:00 2001 From: Boopathi Nedunchezhiyan Date: Fri, 13 Sep 2024 12:46:00 +0200 Subject: [PATCH 1/3] fix: support esm; fix imports --- src/ast.ts | 38 +++++++++++++------------ src/compat.ts | 27 +++++++++--------- src/error.ts | 5 +++- src/execution.ts | 67 +++++++++++++++++++++++---------------------- src/index.ts | 18 ++++++------ src/json.ts | 24 ++++++++-------- src/non-null.ts | 18 ++++++------ src/resolve-info.ts | 14 +++++----- src/variables.ts | 12 ++++---- 9 files changed, 115 insertions(+), 108 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index ec47b915..a7460b03 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -1,38 +1,40 @@ import genFn from "generate-function"; import { - ArgumentNode, - ASTNode, - DirectiveNode, - FieldNode, - FragmentDefinitionNode, + type ArgumentNode, + type ASTNode, + type DirectiveNode, + type FieldNode, + type FragmentDefinitionNode, getLocation, - GraphQLArgument, + type GraphQLArgument, GraphQLDirective, GraphQLError, - GraphQLField, + type GraphQLField, GraphQLIncludeDirective, - GraphQLInputType, + type GraphQLInputType, GraphQLObjectType, GraphQLSkipDirective, - InlineFragmentNode, + type InlineFragmentNode, isEnumType, isInputObjectType, isListType, isNonNullType, isScalarType, print, - SelectionSetNode, - SourceLocation, + type SelectionSetNode, + type SourceLocation, typeFromAST, valueFromASTUntyped, - ValueNode, - VariableNode + type ValueNode, + type VariableNode, + Kind, + type SelectionNode, + type TypeNode, + isAbstractType } from "graphql"; -import { Kind, SelectionNode, TypeNode } from "graphql/language"; -import { isAbstractType } from "graphql/type"; -import { CompilationContext, GLOBAL_VARIABLES_NAME } from "./execution"; -import createInspect from "./inspect"; -import { getGraphQLErrorOptions, resolveFieldDef } from "./compat"; +import { type CompilationContext, GLOBAL_VARIABLES_NAME } from "./execution.js"; +import createInspect from "./inspect.js"; +import { getGraphQLErrorOptions, resolveFieldDef } from "./compat.js"; export interface JitFieldNode extends FieldNode { /** diff --git a/src/compat.ts b/src/compat.ts index 3aa1e013..49f80181 100644 --- a/src/compat.ts +++ b/src/compat.ts @@ -2,18 +2,19 @@ import { GraphQLSchema, GraphQLError, versionInfo, - FieldNode, - GraphQLField + type FieldNode, + type GraphQLField, + type ASTNode, + type OperationDefinitionNode, + type GraphQLObjectType, + formatError as formatErrorV15, + getOperationRootType as getOperationRootTypeV15 } from "graphql"; -import { GraphQLObjectType } from "graphql/type/definition"; -import { Maybe } from "graphql/jsutils/Maybe"; - -import { ASTNode, OperationDefinitionNode } from "graphql/language/ast"; -import * as errorUtilities from "graphql/error"; -import * as utilities from "graphql/utilities"; -import { GraphQLFormattedError } from "graphql/error"; -import { CompilationContext } from "./execution"; -import * as execute from "graphql/execution/execute"; +import { type Maybe } from "./types.js"; +import { type GraphQLFormattedError } from "graphql/error"; +import { type CompilationContext } from "./execution.js"; +// uses internal graphql-js APIs +import * as execute from "graphql/execution/execute.js"; /** * A helper file to support backward compatibility for different versions of graphql-js. @@ -36,7 +37,7 @@ export function getOperationRootType( operation: OperationDefinitionNode ): GraphQLObjectType { if (versionInfo.major < 16) { - return (utilities as any).getOperationRootType(schema, operation); + return getOperationRootTypeV15(schema, operation); } const type = (schema as any).getRootType(operation.operation); @@ -54,7 +55,7 @@ export function getOperationRootType( */ export function formatError(error: GraphQLError): GraphQLFormattedError { if (versionInfo.major < 16) { - return (errorUtilities as any).formatError(error); + return formatErrorV15(error); } return (error as any).toJSON(); diff --git a/src/error.ts b/src/error.ts index 405c99a1..d0f37a9f 100644 --- a/src/error.ts +++ b/src/error.ts @@ -2,7 +2,10 @@ * Based on https://github.com/graphql/graphql-js/blob/master/src/error/GraphQLError.js */ -import { GraphQLError as UpstreamGraphQLError, SourceLocation } from "graphql"; +import { + GraphQLError as UpstreamGraphQLError, + type SourceLocation +} from "graphql"; export function GraphQLError( message: string, diff --git a/src/execution.ts b/src/execution.ts index 73b526a8..837398c1 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -1,25 +1,25 @@ -import { TypedDocumentNode } from "@graphql-typed-document-node/core"; +import { type TypedDocumentNode } from "@graphql-typed-document-node/core"; import fastJson from "fast-json-stringify"; import genFn from "generate-function"; import { - ASTNode, - DocumentNode, - ExecutionResult, - FragmentDefinitionNode, - GraphQLAbstractType, + type ASTNode, + type DocumentNode, + type ExecutionResult, + type FragmentDefinitionNode, + type GraphQLAbstractType, GraphQLEnumType, GraphQLError, - GraphQLFieldResolver, - GraphQLIsTypeOfFn, - GraphQLLeafType, + type GraphQLFieldResolver, + type GraphQLIsTypeOfFn, + type GraphQLLeafType, GraphQLList, GraphQLObjectType, - GraphQLOutputType, - GraphQLResolveInfo, - GraphQLScalarSerializer, + type GraphQLOutputType, + type GraphQLResolveInfo, + type GraphQLScalarSerializer, GraphQLScalarType, GraphQLSchema, - GraphQLType, + type GraphQLType, isAbstractType, isLeafType, isListType, @@ -28,42 +28,43 @@ import { isSpecifiedScalarType, Kind, locatedError, - TypeNameMetaFieldDef + TypeNameMetaFieldDef, + type FieldNode, + type OperationDefinitionNode, + type GraphQLTypeResolver } from "graphql"; -import { ExecutionContext as GraphQLContext } from "graphql/execution/execute"; -import { pathToArray } from "graphql/jsutils/Path"; -import { FieldNode, OperationDefinitionNode } from "graphql/language/ast"; -import { GraphQLTypeResolver } from "graphql/type/definition"; +import { type ExecutionContext as GraphQLContext } from "graphql/execution/execute.js"; +import { pathToArray } from "graphql/jsutils/Path.js"; import { addPath, - Arguments, + type Arguments, collectFields, collectSubfields, computeLocations, - FieldsAndNodes, + type FieldsAndNodes, flattenPath, getArgumentDefs, - JitFieldNode, + type JitFieldNode, joinSkipIncludePath, - ObjectPath, + type ObjectPath, resolveFieldDef, serializeObjectPathForSkipInclude -} from "./ast"; -import { GraphQLError as GraphqlJitError } from "./error"; -import createInspect from "./inspect"; -import { queryToJSONSchema } from "./json"; -import { createNullTrimmer, NullTrimmer } from "./non-null"; +} from "./ast.js"; +import { GraphQLError as GraphqlJitError } from "./error.js"; +import createInspect from "./inspect.js"; +import { queryToJSONSchema } from "./json.js"; +import { createNullTrimmer, type NullTrimmer } from "./non-null.js"; import { createResolveInfoThunk, - ResolveInfoEnricherInput -} from "./resolve-info"; -import { Maybe } from "./types"; + type ResolveInfoEnricherInput +} from "./resolve-info.js"; +import { type Maybe } from "./types.js"; import { - CoercedVariableValues, + type CoercedVariableValues, compileVariableParsing, failToParseVariables -} from "./variables"; -import { getGraphQLErrorOptions, getOperationRootType } from "./compat"; +} from "./variables.js"; +import { getGraphQLErrorOptions, getOperationRootType } from "./compat.js"; const inspect = createInspect(); diff --git a/src/index.ts b/src/index.ts index ce90aba3..180b85e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,16 @@ export { compileQuery, isCompiledQuery, - CompilerOptions, - CompiledQuery -} from "./execution"; + type CompilerOptions, + type CompiledQuery +} from "./execution.js"; export { - GraphQLJitResolveInfo, - FieldExpansion, - LeafField, - TypeExpansion, + type GraphQLJitResolveInfo, + type FieldExpansion, + type LeafField, + type TypeExpansion, fieldExpansionEnricher, isLeafField, - ResolveInfoEnricherInput -} from "./resolve-info"; + type ResolveInfoEnricherInput +} from "./resolve-info.js"; diff --git a/src/json.ts b/src/json.ts index 61626461..6baf5e61 100644 --- a/src/json.ts +++ b/src/json.ts @@ -4,8 +4,8 @@ * @type {} */ import { - FieldNode, - GraphQLType, + type FieldNode, + type GraphQLType, isAbstractType, isEnumType, isListType, @@ -14,17 +14,17 @@ import { isScalarType } from "graphql"; import { - BooleanSchema, - IntegerSchema, - NumberSchema, - ObjectSchema, - RefSchema, - Schema, - StringSchema + type BooleanSchema, + type IntegerSchema, + type NumberSchema, + type ObjectSchema, + type RefSchema, + type Schema, + type StringSchema } from "fast-json-stringify"; -import { collectFields, collectSubfields, resolveFieldDef } from "./ast"; -import { getOperationRootType } from "./compat"; -import { CompilationContext } from "./execution"; +import { collectFields, collectSubfields, resolveFieldDef } from "./ast.js"; +import { getOperationRootType } from "./compat.js"; +import { type CompilationContext } from "./execution.js"; const PRIMITIVES: { [key: string]: diff --git a/src/non-null.ts b/src/non-null.ts index 26bf0f92..12b9fbe3 100644 --- a/src/non-null.ts +++ b/src/non-null.ts @@ -1,17 +1,17 @@ import { - ExecutionResult, - FieldNode, - GraphQLError, - GraphQLType, + type ExecutionResult, + type FieldNode, + type GraphQLError, + type GraphQLType, isListType, isNonNullType, - isObjectType + isObjectType, + isAbstractType } from "graphql"; -import { isAbstractType } from "graphql/type"; import merge from "lodash.merge"; -import { collectFields, collectSubfields, resolveFieldDef } from "./ast"; -import { getOperationRootType } from "./compat"; -import { CompilationContext } from "./execution"; +import { collectFields, collectSubfields, resolveFieldDef } from "./ast.js"; +import { getOperationRootType } from "./compat.js"; +import { type CompilationContext } from "./execution.js"; interface QueryMetadata { isNullable: boolean; diff --git a/src/resolve-info.ts b/src/resolve-info.ts index 37402914..220289b3 100644 --- a/src/resolve-info.ts +++ b/src/resolve-info.ts @@ -1,14 +1,14 @@ import genFn from "generate-function"; import { doTypesOverlap, - FieldNode, - GraphQLCompositeType, + type FieldNode, + type GraphQLCompositeType, GraphQLError, GraphQLInterfaceType, - GraphQLNamedType, + type GraphQLNamedType, GraphQLObjectType, - GraphQLOutputType, - GraphQLResolveInfo, + type GraphQLOutputType, + type GraphQLResolveInfo, GraphQLSchema, isAbstractType, isCompositeType, @@ -17,11 +17,11 @@ import { isObjectType, isUnionType, Kind, - SelectionSetNode + type SelectionSetNode } from "graphql"; import memoize from "lodash.memoize"; import mergeWith from "lodash.mergewith"; -import { memoize2, memoize4 } from "./memoize"; +import { memoize2, memoize4 } from "./memoize.js"; // TODO(boopathi): Use negated types to express // Enrichments = { [key in (string & not keyof GraphQLResolveInfo)]: T[key] } diff --git a/src/variables.ts b/src/variables.ts index 582380bc..dbbca5cf 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -4,7 +4,7 @@ import { GraphQLError, GraphQLFloat, GraphQLID, - GraphQLInputType, + type GraphQLInputType, GraphQLInt, GraphQLSchema, GraphQLString, @@ -14,14 +14,14 @@ import { isNonNullType, isScalarType, print, - SourceLocation, + type SourceLocation, typeFromAST, valueFromAST, - VariableDefinitionNode + type VariableDefinitionNode } from "graphql"; -import { addPath, computeLocations, ObjectPath } from "./ast"; -import { GraphQLError as GraphQLJITError } from "./error"; -import createInspect from "./inspect"; +import { addPath, computeLocations, type ObjectPath } from "./ast.js"; +import { GraphQLError as GraphQLJITError } from "./error.js"; +import createInspect from "./inspect.js"; const inspect = createInspect(); From 88f9a790764a16bebe4045862294fdda2d2c6064 Mon Sep 17 00:00:00 2001 From: Boopathi Nedunchezhiyan Date: Fri, 13 Sep 2024 12:55:11 +0200 Subject: [PATCH 2/3] fix for compatibility --- src/compat.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/compat.ts b/src/compat.ts index 49f80181..92c398df 100644 --- a/src/compat.ts +++ b/src/compat.ts @@ -7,13 +7,12 @@ import { type ASTNode, type OperationDefinitionNode, type GraphQLObjectType, - formatError as formatErrorV15, - getOperationRootType as getOperationRootTypeV15 + type GraphQLFormattedError } from "graphql"; import { type Maybe } from "./types.js"; -import { type GraphQLFormattedError } from "graphql/error"; +import * as errorUtilities from "graphql/error/index.js"; +import * as utilities from "graphql/utilities/index.js"; import { type CompilationContext } from "./execution.js"; -// uses internal graphql-js APIs import * as execute from "graphql/execution/execute.js"; /** @@ -37,7 +36,7 @@ export function getOperationRootType( operation: OperationDefinitionNode ): GraphQLObjectType { if (versionInfo.major < 16) { - return getOperationRootTypeV15(schema, operation); + return (utilities as any).getOperationRootType(schema, operation); } const type = (schema as any).getRootType(operation.operation); @@ -55,7 +54,7 @@ export function getOperationRootType( */ export function formatError(error: GraphQLError): GraphQLFormattedError { if (versionInfo.major < 16) { - return formatErrorV15(error); + return (errorUtilities as any).formatError(error); } return (error as any).toJSON(); From 6da7eab2f1052d8989fb775fce4fcbb35515e9b4 Mon Sep 17 00:00:00 2001 From: Boopathi Nedunchezhiyan Date: Fri, 13 Sep 2024 12:58:25 +0200 Subject: [PATCH 3/3] configure jest to treat .js as .ts --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 0fcb9d7f..d478d57b 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,9 @@ "js", "json" ], + "moduleNameMapper": { + "(.+)\\.js": "$1" + }, "testEnvironment": "node", "testRegex": "(/tests/.*|(\\.|/)test)\\.ts$", "transform": {