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

Reduce the bundle for some utilities #4844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/sweet-chairs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-tools/executor': patch
'@graphql-tools/schema': patch
'@graphql-tools/utils': patch
---

Reduce the bundle size
26 changes: 10 additions & 16 deletions packages/executor/src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
SchemaMetaFieldDef,
TypeMetaFieldDef,
TypeNameMetaFieldDef,
getArgumentValues,
} from 'graphql';
import type { GraphQLError } from 'graphql';
import {
Expand All @@ -34,20 +35,17 @@ 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';
Expand All @@ -61,16 +59,6 @@ export interface SingularExecutionResult<TData = any, TExtensions = any> {
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<FieldNode>) =>
_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.

Expand Down Expand Up @@ -1134,7 +1122,13 @@ function collectAndExecuteSubfields(
asyncPayloadRecord?: AsyncPayloadRecord
): MaybePromise<Record<string, unknown>> {
// 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);

Expand Down
2 changes: 1 addition & 1 deletion packages/executor/src/execution/normalizedExecutor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/addResolversToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function addResolversToSchema({
return schema;
}

function addResolversToExistingSchema(
export function addResolversToExistingSchema(
schema: GraphQLSchema,
resolvers: IResolvers,
defaultFieldResolver?: GraphQLFieldResolver<any, any>
Expand Down Expand Up @@ -256,7 +256,7 @@ function addResolversToExistingSchema(
return schema;
}

function createNewSchemaWithResolvers(
export function createNewSchemaWithResolvers(
schema: GraphQLSchema,
resolvers: IResolvers,
defaultFieldResolver?: GraphQLFieldResolver<any, any>
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/get-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import {
GraphQLEnumValue,
GraphQLEnumValueConfig,
EnumValueDefinitionNode,
getArgumentValues,
} from 'graphql';

import { getArgumentValues } from './getArgumentValues.js';

export interface DirectiveAnnotation {
name: string;
args?: Record<string, any>;
Expand Down
83 changes: 3 additions & 80 deletions packages/utils/src/getArgumentValues.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,69 +14,5 @@ export function getArgumentValues(
node: FieldNode | DirectiveNode,
variableValues: Record<string, any> = {}
): Record<string, any> {
const coercedValues = {};

const argumentNodes = node.arguments ?? [];
const argNodeMap: Record<string, ArgumentNode> = 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);
}
4 changes: 1 addition & 3 deletions packages/wrap/src/transforms/FilterObjectFieldDirectives.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
18 changes: 16 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down