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

feat(perf): remove imports that bloat bundle #1215

Merged
merged 3 commits into from
Oct 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ website/.vitepress/.temp
website/.vitepress/dist
website/.vitepress/cache
legacy
project/
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tsEslint from 'typescript-eslint'

export default tsEslint.config({
ignores: [
'project/**/*',
'examples/35_custom-scalar/custom-scalar.ts',
'eslint.config.js',
'vite.config.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
{ name: 'Weedle' }
]
},
errors: undefined,
extensions: undefined,
response: Response {
status: 200,
statusText: 'OK',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ContextualAggregateError: One or more errors in the execution result.
context: {},
cause: undefined,
errors: [
GraphQLError: [
ContextualError: [
{
"code": "too_small",
"minimum": 1,
Expand All @@ -21,15 +21,15 @@ ContextualAggregateError: One or more errors in the execution result.
]
}
]
at <anonymous> (/some/path/to/http.ts:XX:XX:47)
at <anonymous> (/some/path/to/handleOutput.ts:XX:XX:16)
at Array.map (<anonymous>)
at parseExecutionResult (/some/path/to/http.ts:XX:XX:28)
at Object.unpack (/some/path/to/core.ts:XX:XX:26)
at handleOutput (/some/path/to/handleOutput.ts:XX:XX:21)
at executeDocument (/some/path/to/requestMethods.ts:XX:XX:10)
at process.processTicksAndRejections (node:internal/process/task_queues:XX:XX)
at async runHook (/some/path/to/runHook.ts:XX:XX:16) {
path: [ 'addPokemon' ],
locations: undefined,
extensions: [Object: null prototype] {}
at async executeRootField (/some/path/to/requestMethods.ts:XX:XX:18)
at async <anonymous> (/some/path/to/output_return-error_return-error-execution__return-error-execution.ts:XX:XX:16) {
context: { locations: [ { line: 2, column: 3 } ], path: [ 'addPokemon' ] },
cause: undefined
}
]
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"check:publint": "publint run --strict",
"prepublishOnly": "pnpm build",
"build:docs": "doctoc README.md --notitle && dprint fmt README.md",
"build:emit": "pnpm clean && pnpm tsc --noCheck --project tsconfig.build.json && chmod +x ./build/generator/cli/generate.js",
"build": "pnpm clean && pnpm tsc --project tsconfig.build.json && chmod +x ./build/generator/cli/generate.js",
"clean": "tsc --build --clean && rm -rf build",
"test:unit": "vitest --exclude tests/examples",
Expand Down
6 changes: 3 additions & 3 deletions src/documentBuilder/Select/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const createDocumentNormalizedFromRootTypeSelection = (
operations: {
[operationName ?? defaultOperationName]: {
name: operationName ?? null,
type: Grafaid.RootTypeNameToOperationName[rootTypeName],
type: Grafaid.Document.RootTypeToOperationType[rootTypeName],
rootType: rootTypeName,
selectionSet,
},
Expand All @@ -100,15 +100,15 @@ export const normalizeOrThrow = (document: DocumentObject): DocumentNormalized =
[name, selectionSet],
): [name: string, OperationNormalized] => [name, {
name,
type: Grafaid.RootTypeNameToOperationName[Grafaid.Schema.RootTypeName.Query],
type: Grafaid.Document.RootTypeToOperationType[Grafaid.Schema.RootTypeName.Query],
rootType: Grafaid.Schema.RootTypeName.Query,
selectionSet,
}])
const mutationOperations = Object.entries(document.mutation ?? {}).map((
[name, selectionSet],
): [name: string, OperationNormalized] => [name, {
name,
type: Grafaid.RootTypeNameToOperationName[Grafaid.Schema.RootTypeName.Mutation],
type: Grafaid.Document.RootTypeToOperationType[Grafaid.Schema.RootTypeName.Mutation],
rootType: Grafaid.Schema.RootTypeName.Mutation,
selectionSet,
}])
Expand Down
4 changes: 2 additions & 2 deletions src/generator/generators/MethodsRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const ModuleGeneratorMethodsRoot = createModuleGenerator(
export interface BuilderMethodsRoot<$Context extends ${identifiers.$$Utilities}.ClientContext> {
${
config.schema.kindMap.Root.map(node => {
const operationName =
Grafaid.RootTypeNameToOperationName[node.name as keyof typeof Grafaid.RootTypeNameToOperationName]
const operationName = Grafaid.Document
.RootTypeToOperationType[node.name as keyof typeof Grafaid.Document.RootTypeToOperationType]
return `${operationName}: ${node.name}Methods<$Context>`
}).join(`\n`)
}
Expand Down
11 changes: 5 additions & 6 deletions src/layers/5_request/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { type ExecutionResult } from 'graphql'
import { SelectionSetGraphqlMapper } from '../../documentBuilder/SelectGraphQLMapper/__.js'
import { Anyware } from '../../lib/anyware/__.js'
import type { Grafaid } from '../../lib/grafaid/__.js'
import { Grafaid } from '../../lib/grafaid/__.js'
import { getOperationDefinition, OperationTypeToAccessKind, print } from '../../lib/grafaid/document.js'
import { execute } from '../../lib/grafaid/execute.js'
import { operationTypeToRootType } from '../../lib/grafaid/graphql.js'
import { execute } from '../../lib/grafaid/execute.js' // todo
import {
getRequestEncodeSearchParameters,
getRequestHeadersRec,
Expand Down Expand Up @@ -44,15 +42,16 @@ export const graffleMappedResultToRequest = (
if (!operation_) throw new Error(`Unknown operation named "${String(operationName)}".`)

return {
rootType: operationTypeToRootType[operation_.operation],
rootType: Grafaid.Document.OperationTypeToRootType[operation_.operation],
operationName,
operation: operation_,
query: document,
variables: variables_,
}
}

export const anyware = Anyware.create<HookSequence, HookMap, ExecutionResult>({
// todo execution result only if transport is memory
export const anyware = Anyware.create<HookSequence, HookMap, Grafaid.FormattedExecutionResult>({
// If core errors caused by an abort error then raise it as a direct error.
// This is an expected possible error. Possible when user cancels a request.
passthroughErrorWith: (signal) => {
Expand Down
6 changes: 3 additions & 3 deletions src/layers/5_request/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ExecutionResult, GraphQLSchema } from 'graphql'
import type { FormattedExecutionResult, GraphQLSchema } from 'graphql'
import type { Select } from '../../documentBuilder/Select/__.js'
import type { Grafaid } from '../../lib/grafaid/__.js'
import type { getRequestEncodeSearchParameters, postRequestEncodeBody } from '../../lib/grafaid/http/http.js'
Expand Down Expand Up @@ -96,7 +96,7 @@ export type HookDefUnpack<$Config extends Config> = {
& TransportInput<
$Config,
{ response: Response },
{ result: ExecutionResult }
{ result: FormattedExecutionResult }
>
}

Expand All @@ -108,7 +108,7 @@ export type HookDefDecode<$Config extends Config> = {
$Config,
{ response: Response }
>
& { result: ExecutionResult }
& { result: FormattedExecutionResult }
}

export type HookMap<$Config extends Config = Config> = {
Expand Down
4 changes: 2 additions & 2 deletions src/layers/6_client/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomScalars } from '../../extensions/CustomScalars/CustomScalars.js'
import { CustomScalars } from '../../extensions/CustomScalars/CustomScalars.js' // todo
import type { ConfigManager } from '../../lib/config-manager/__.js'
import type { Fluent } from '../../lib/fluent/__.js'
import { proxyGet } from '../../lib/prelude.js'
Expand All @@ -11,7 +11,7 @@ import type { FnInternal } from './properties/internal.js'
import { type FnRetry, retryProperties } from './properties/retry.js'
import { type ScalarFn, scalarProperties } from './properties/scalar.js'
import { type FnWith, withProperties } from './properties/with.js'
import { type FnRequestMethods, requestMethodsProperties } from './requestMethods/requestMethods.js'
import { type FnRequestMethods, requestMethodsProperties } from './requestMethods/requestMethods.js' // todo
import { type InputStatic } from './Settings/Input.js'
import { type NormalizeInput } from './Settings/InputToConfig.js'

Expand Down
7 changes: 3 additions & 4 deletions src/layers/6_client/gql/gql.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Fluent } from '../../../lib/fluent/__.js'
import type { Grafaid } from '../../../lib/grafaid/__.js'
import { getOperationType } from '../../../lib/grafaid/document.js'
import { operationTypeToRootType } from '../../../lib/grafaid/graphql.js'
import { getOperationType, OperationTypeToRootType } from '../../../lib/grafaid/document.js'
import {
isTemplateStringArguments,
joinTemplateStringArrayAndArgs,
type TemplateStringsArguments,
} from '../../../lib/template-string.js'
import { RequestCore } from '../../5_request/__.js'
import { RequestCore } from '../../5_request/__.js' // todo
import type { InterfaceRaw } from '../../5_request/types.js'
import { type ClientContext, defineTerminus } from '../fluent.js'
import { handleOutput } from '../handleOutput.js'
Expand Down Expand Up @@ -55,7 +54,7 @@ export const gqlProperties = defineTerminus((state) => {
if (!operationType) throw new Error(`Could not get operation type`)

const analyzedRequest = {
rootType: operationTypeToRootType[operationType],
rootType: OperationTypeToRootType[operationType],
operation: operationType,
query,
variables,
Expand Down
14 changes: 12 additions & 2 deletions src/layers/6_client/handleOutput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GraphQLError } from 'graphql'
import { Errors } from '../../lib/errors/__.js'
import type { Grafaid } from '../../lib/grafaid/__.js'
import type { SomeObjectData } from '../../lib/grafaid/graphql.js'
import type { GraphQLExecutionResultError } from '../../lib/grafaid/graphql.js'
import {
Expand Down Expand Up @@ -32,7 +33,12 @@ export type ErrorsOther =
export type GraffleExecutionResultEnvelope<$Config extends Config = Config> =
// & ExecutionResult
& {
errors?: ReadonlyArray<GraphQLError>
errors?: ReadonlyArray<
// formatted comes from http transport
| Grafaid.FormattedExecutionResultError
// unformatted comes from memory transport
| Grafaid.GraphQLError
>
data?: SomeObjectData | null
extensions?: ObjMap
}
Expand Down Expand Up @@ -91,7 +97,11 @@ export const handleOutput = (
const error = new Errors.ContextualAggregateError(
`One or more errors in the execution result.`,
{},
result.errors,
result.errors.map(e => {
if (e instanceof Error) return e
const { message, ...context } = e
return new Errors.ContextualError(message, context)
}),
)
if (isThrowExecution) throw error
if (isReturnExecution) return error
Expand Down
24 changes: 20 additions & 4 deletions src/lib/grafaid/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import {
type FragmentSpreadNode,
type InlineFragmentNode,
type IntValueNode,
Kind,
type ListValueNode,
type NamedTypeNode,
type NameNode,
type NullValueNode,
type ObjectFieldNode,
type ObjectValueNode,
type OperationDefinitionNode,
OperationTypeNode,
parse,
print as graphqlPrint,
type SelectionSetNode,
Expand All @@ -29,7 +27,10 @@ import {
} from 'graphql'
import type { HasRequiredKeys } from 'type-fest'
import { isString } from '../prelude.js'
import { Kind } from './document/kind.js'
import { OperationTypeNode } from './document/OperationTypeNode.js'
import type { RequestDocumentNodeInput, RequestInput } from './graphql.js'
import { RootTypeName } from './schema/schema.js'
import { TypedDocument } from './typed-document/__.js'

export type {
Expand All @@ -50,7 +51,6 @@ export type {
ObjectValueNode,
OperationDefinitionNode,
OperationTypeDefinitionNode,
OperationTypeNode,
SelectionNode,
SelectionSetNode,
StringValueNode,
Expand All @@ -59,7 +59,9 @@ export type {
VariableNode,
} from 'graphql'

export { Kind } from 'graphql'
export { OperationTypeNode } from './document/OperationTypeNode.js'

export { Kind } from './document/kind.js'

export * as Typed from './typed-document/TypedDocument.js'

Expand Down Expand Up @@ -253,12 +255,25 @@ export const ObjectField: Constructor<ObjectFieldNode> = (objectField) => {
}
}

export const RootTypeToOperationType = {
Query: OperationTypeNode.QUERY,
Mutation: OperationTypeNode.MUTATION,
Subscription: OperationTypeNode.SUBSCRIPTION,
} as const
export type RootTypeNameToOperationName = typeof RootTypeToOperationType

export const OperationTypeToAccessKind = {
query: `read`,
mutation: `write`,
subscription: `read`,
} as const

export const OperationTypeToRootType = {
query: RootTypeName.Query,
mutation: RootTypeName.Mutation,
subscription: RootTypeName.Subscription,
} as const

export const print = (document: TypedDocument.TypedDocumentLike): string => {
const documentUntyped = TypedDocument.unType(document)
return isString(documentUntyped) ? documentUntyped : graphqlPrint(documentUntyped)
Expand Down Expand Up @@ -291,6 +306,7 @@ const definedOperationPattern = new RegExp(`^\\b(${Object.values(OperationTypeNo
* to avoid document encode/decode performance costs.
*/
export const getOperationType = (request: RequestInput): OperationTypeNode | null => {
// return null
const { operationName, query: document } = request

const documentUntyped = TypedDocument.unType(document)
Expand Down
8 changes: 8 additions & 0 deletions src/lib/grafaid/document/OperationTypeNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* This is a tree-shakable version of https://github.com/graphql/graphql-js/blob/16.x.x/src/language/kinds.ts
*/

import type { OperationTypeNode as GraphQLOperationTypeNode } from 'graphql'

export type OperationTypeNode = GraphQLOperationTypeNode
export * as OperationTypeNode from './OperationTypeNodes.js'
9 changes: 9 additions & 0 deletions src/lib/grafaid/document/OperationTypeNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This is a tree-shakable version of https://github.com/graphql/graphql-js/blob/16.x.x/src/language/ast.ts#L326-L331
*/

import type { OperationTypeNode as GraphQLOperationTypeNode } from 'graphql'

export const QUERY = `query` as GraphQLOperationTypeNode.QUERY
export const MUTATION = `mutation` as GraphQLOperationTypeNode.MUTATION
export const SUBSCRIPTION = `subscription` as GraphQLOperationTypeNode.SUBSCRIPTION
9 changes: 9 additions & 0 deletions src/lib/grafaid/document/kind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This is a tree-shakable version of https://github.com/graphql/graphql-js/blob/16.x.x/src/language/kinds.ts
*/

import type { Kind as GraphQLKind } from 'graphql'

export * as Kind from './kinds.js'

export type Kind = GraphQLKind
Loading