From 330404d1a9ceea5c319f4cc2f772ffa5bb0e9e6a Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Mon, 14 Nov 2022 14:30:50 +0100 Subject: [PATCH 1/2] Experiemnts to reduce the bundle size --- .changeset/sweet-chairs-help.md | 7 ++ package.json | 2 +- packages/executor/src/execution/execute.ts | 24 ++---- .../src/execution/normalizedExecutor.ts | 2 +- packages/schema/src/addResolversToSchema.ts | 4 +- packages/utils/src/getArgumentValues.ts | 83 +------------------ yarn.lock | 18 +++- 7 files changed, 39 insertions(+), 101 deletions(-) create mode 100644 .changeset/sweet-chairs-help.md diff --git a/.changeset/sweet-chairs-help.md b/.changeset/sweet-chairs-help.md new file mode 100644 index 00000000000..025e66f4bc3 --- /dev/null +++ b/.changeset/sweet-chairs-help.md @@ -0,0 +1,7 @@ +--- +'@graphql-tools/executor': patch +'@graphql-tools/schema': patch +'@graphql-tools/utils': patch +--- + +Reduce the bundle size diff --git a/package.json b/package.json index 7c349a1bc03..20e644e3272 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "benchmark/*" ], "resolutions": { - "graphql": "16.6.0", + "graphql": "15^", "esbuild": "^0.15.0" }, "yarn": { diff --git a/packages/executor/src/execution/execute.ts b/packages/executor/src/execution/execute.ts index 500eb0cd58e..a5080d3091f 100644 --- a/packages/executor/src/execution/execute.ts +++ b/packages/executor/src/execution/execute.ts @@ -34,20 +34,18 @@ import { isIterableObject, isObjectLike, isPromise, - Path, pathToArray, addPath, getArgumentValues, promiseReduce, - Maybe, - memoize3, getDefinedRootType, MaybePromise, mapAsyncIterator, GraphQLStreamDirective, collectFields, - collectSubFields as _collectSubfields, + collectSubFields, } from '@graphql-tools/utils'; +import type { Maybe, Path } from '@graphql-tools/utils'; import { getVariableValues } from './values.js'; import { promiseForObject } from './promiseForObject.js'; import { flattenAsyncIterable } from './flattenAsyncIterable.js'; @@ -61,16 +59,6 @@ export interface SingularExecutionResult { extensions?: TExtensions; } -/** - * A memoized collection of relevant subfields with regard to the return - * type. Memoizing ensures the subfields are not repeatedly calculated, which - * saves overhead when resolving lists of values. - */ -const collectSubfields = memoize3( - (exeContext: ExecutionContext, returnType: GraphQLObjectType, fieldNodes: Array) => - _collectSubfields(exeContext.schema, exeContext.fragments, exeContext.variableValues, returnType, fieldNodes) -); - // This file contains a lot of such errors but we plan to refactor it anyway // so just disable it for entire file. @@ -1134,7 +1122,13 @@ function collectAndExecuteSubfields( asyncPayloadRecord?: AsyncPayloadRecord ): MaybePromise> { // Collect sub-fields to execute to complete this value. - const { fields: subFieldNodes, patches: subPatches } = collectSubfields(exeContext, returnType, fieldNodes); + const { fields: subFieldNodes, patches: subPatches } = collectSubFields( + exeContext.schema, + exeContext.fragments, + exeContext.variableValues, + returnType, + fieldNodes + ); const subFields = executeFields(exeContext, returnType, result, path, subFieldNodes, asyncPayloadRecord); diff --git a/packages/executor/src/execution/normalizedExecutor.ts b/packages/executor/src/execution/normalizedExecutor.ts index d8b216d1760..51d2b43c5ef 100644 --- a/packages/executor/src/execution/normalizedExecutor.ts +++ b/packages/executor/src/execution/normalizedExecutor.ts @@ -1,4 +1,4 @@ -import { MaybeAsyncIterable, ExecutionResult, MaybePromise } from '@graphql-tools/utils'; +import type { MaybeAsyncIterable, ExecutionResult, MaybePromise } from '@graphql-tools/utils'; import { getOperationAST } from 'graphql'; import { execute, ExecutionArgs, flattenIncrementalResults, subscribe } from './execute.js'; import { ValueOrPromise } from 'value-or-promise'; diff --git a/packages/schema/src/addResolversToSchema.ts b/packages/schema/src/addResolversToSchema.ts index becec011d78..a7fa6fb5bd4 100644 --- a/packages/schema/src/addResolversToSchema.ts +++ b/packages/schema/src/addResolversToSchema.ts @@ -133,7 +133,7 @@ export function addResolversToSchema({ return schema; } -function addResolversToExistingSchema( +export function addResolversToExistingSchema( schema: GraphQLSchema, resolvers: IResolvers, defaultFieldResolver?: GraphQLFieldResolver @@ -256,7 +256,7 @@ function addResolversToExistingSchema( return schema; } -function createNewSchemaWithResolvers( +export function createNewSchemaWithResolvers( schema: GraphQLSchema, resolvers: IResolvers, defaultFieldResolver?: GraphQLFieldResolver diff --git a/packages/utils/src/getArgumentValues.ts b/packages/utils/src/getArgumentValues.ts index 82b91d4563f..731afee3461 100644 --- a/packages/utils/src/getArgumentValues.ts +++ b/packages/utils/src/getArgumentValues.ts @@ -1,18 +1,5 @@ -import { hasOwnProperty } from './jsutils.js'; -import { - valueFromAST, - GraphQLField, - GraphQLDirective, - DirectiveNode, - FieldNode, - isNonNullType, - Kind, - print, - ArgumentNode, -} from 'graphql'; -import { createGraphQLError } from './errors.js'; - -import { inspect } from './inspect.js'; +import type { GraphQLField, GraphQLDirective, FieldNode, DirectiveNode } from 'graphql'; +import { getArgumentValues as gqljsGetArgumentValues } from 'graphql'; /** * Prepares an object map of argument values given a list of argument @@ -27,69 +14,5 @@ export function getArgumentValues( node: FieldNode | DirectiveNode, variableValues: Record = {} ): Record { - const coercedValues = {}; - - const argumentNodes = node.arguments ?? []; - const argNodeMap: Record = argumentNodes.reduce( - (prev, arg) => ({ - ...prev, - [arg.name.value]: arg, - }), - {} - ); - - for (const { name, type: argType, defaultValue } of def.args) { - const argumentNode = argNodeMap[name]; - - if (!argumentNode) { - if (defaultValue !== undefined) { - coercedValues[name] = defaultValue; - } else if (isNonNullType(argType)) { - throw createGraphQLError(`Argument "${name}" of required type "${inspect(argType)}" ` + 'was not provided.', { - nodes: [node], - }); - } - continue; - } - - const valueNode = argumentNode.value; - let isNull = valueNode.kind === Kind.NULL; - - if (valueNode.kind === Kind.VARIABLE) { - const variableName = valueNode.name.value; - if (variableValues == null || !hasOwnProperty(variableValues, variableName)) { - if (defaultValue !== undefined) { - coercedValues[name] = defaultValue; - } else if (isNonNullType(argType)) { - throw createGraphQLError( - `Argument "${name}" of required type "${inspect(argType)}" ` + - `was provided the variable "$${variableName}" which was not provided a runtime value.`, - { - nodes: [valueNode], - } - ); - } - continue; - } - isNull = variableValues[variableName] == null; - } - - if (isNull && isNonNullType(argType)) { - throw createGraphQLError(`Argument "${name}" of non-null type "${inspect(argType)}" ` + 'must not be null.', { - nodes: [valueNode], - }); - } - - const coercedValue = valueFromAST(valueNode, argType, variableValues); - if (coercedValue === undefined) { - // Note: ValuesOfCorrectTypeRule validation should catch this before - // execution. This is a runtime check to ensure execution does not - // continue with an invalid argument value. - throw createGraphQLError(`Argument "${name}" has invalid value ${print(valueNode)}.`, { - nodes: [valueNode], - }); - } - coercedValues[name] = coercedValue; - } - return coercedValues; + return gqljsGetArgumentValues(def, node, variableValues); } diff --git a/yarn.lock b/yarn.lock index 6999453bcd5..cb417512721 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1684,6 +1684,13 @@ tslib "^2.4.0" value-or-promise "1.0.11" +"@graphql-tools/utils@9.1.0": + version "9.1.0" + resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-9.1.0.tgz#c33893e0aa9cbd3760d94f1771477e722adb4e54" + integrity sha512-4Ketxo98IwKA/56LP6cI6PgQBwUCujszQcTNkzjq7liJPa2mLjKnmVOJ0bauMwKcEazeYuZagceljb0POmEGvQ== + dependencies: + tslib "^2.4.0" + "@graphql-tools/utils@^8.5.2", "@graphql-tools/utils@^8.8.0": version "8.13.1" resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-8.13.1.tgz#b247607e400365c2cd87ff54654d4ad25a7ac491" @@ -6910,11 +6917,18 @@ graphql-yoga@3.0.0-next.11: dset "^3.1.1" tslib "^2.3.1" -"graphql@14 - 16", graphql@16.6.0, graphql@^14.5.3, graphql@^16.6.0: +"graphql@14 - 16", graphql@16.6.0, graphql@^16.6.0: version "16.6.0" resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.6.0.tgz#c2dcffa4649db149f6282af726c8c83f1c7c5fdb" integrity sha512-KPIBPDlW7NxrbT/eh4qPXz5FiFdL5UbaA0XUNz2Rp3Z3hqBSkbj0GVjwFDztsWVauZUWsbKHgMg++sk8UX0bkw== +graphql@^14.5.3: + version "14.7.0" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.7.0.tgz#7fa79a80a69be4a31c27dda824dc04dac2035a72" + integrity sha512-l0xWZpoPKpppFzMfvVyFmp9vLN7w/ZZJPefUicMCepfJeQ8sMcztloGYY9DfjVPo6tIUDzU5Hw3MUbIjj9AVVA== + dependencies: + iterall "^1.2.2" + gray-matter@^4.0.3: version "4.0.3" resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.3.tgz#e893c064825de73ea1f5f7d88c7a9f7274288798" @@ -7576,7 +7590,7 @@ istanbul-reports@^3.1.3: html-escaper "^2.0.0" istanbul-lib-report "^3.0.0" -iterall@^1.2.1, iterall@^1.3.0: +iterall@^1.2.1, iterall@^1.2.2, iterall@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea" integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg== From 5a47c526acaaef3684b30ed19034c0c21c8088d8 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Mon, 14 Nov 2022 14:33:54 +0100 Subject: [PATCH 2/2] Go --- package.json | 2 +- packages/executor/src/execution/execute.ts | 2 +- packages/utils/src/get-directives.ts | 3 +-- packages/wrap/src/transforms/FilterObjectFieldDirectives.ts | 4 +--- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 20e644e3272..7c349a1bc03 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "benchmark/*" ], "resolutions": { - "graphql": "15^", + "graphql": "16.6.0", "esbuild": "^0.15.0" }, "yarn": { diff --git a/packages/executor/src/execution/execute.ts b/packages/executor/src/execution/execute.ts index a5080d3091f..fad765a0f1a 100644 --- a/packages/executor/src/execution/execute.ts +++ b/packages/executor/src/execution/execute.ts @@ -25,6 +25,7 @@ import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, + getArgumentValues, } from 'graphql'; import type { GraphQLError } from 'graphql'; import { @@ -36,7 +37,6 @@ import { isPromise, pathToArray, addPath, - getArgumentValues, promiseReduce, getDefinedRootType, MaybePromise, diff --git a/packages/utils/src/get-directives.ts b/packages/utils/src/get-directives.ts index 0817e8e3a20..46e4748bdc8 100644 --- a/packages/utils/src/get-directives.ts +++ b/packages/utils/src/get-directives.ts @@ -22,10 +22,9 @@ import { GraphQLEnumValue, GraphQLEnumValueConfig, EnumValueDefinitionNode, + getArgumentValues, } from 'graphql'; -import { getArgumentValues } from './getArgumentValues.js'; - export interface DirectiveAnnotation { name: string; args?: Record; diff --git a/packages/wrap/src/transforms/FilterObjectFieldDirectives.ts b/packages/wrap/src/transforms/FilterObjectFieldDirectives.ts index 674d563af34..1150348212c 100644 --- a/packages/wrap/src/transforms/FilterObjectFieldDirectives.ts +++ b/packages/wrap/src/transforms/FilterObjectFieldDirectives.ts @@ -1,6 +1,4 @@ -import { GraphQLSchema, GraphQLFieldConfig } from 'graphql'; - -import { getArgumentValues } from '@graphql-tools/utils'; +import { GraphQLSchema, GraphQLFieldConfig, getArgumentValues } from 'graphql'; import { SubschemaConfig, Transform } from '@graphql-tools/delegate';