UNPKG

@graphql-tools/stitch

Version:

A set of utils for faster development of GraphQL tools

177 lines (164 loc) • 10.3 kB
import { FieldNode, SelectionSetNode, GraphQLSchema, GraphQLNamedType, GraphQLFieldConfig, GraphQLObjectType, GraphQLInterfaceType, GraphQLInputFieldConfig, GraphQLInputObjectType, GraphQLEnumValueConfig, GraphQLEnumType, GraphQLOutputType, SelectionNode, FragmentDefinitionNode } from 'graphql'; import { SubschemaConfig, Subschema, MergedTypeResolverOptions, MergedTypeResolver, MergedTypeConfig, MergedTypeEntryPoint, Transform, DelegationContext } from '@graphql-tools/delegate'; import { IExecutableSchemaDefinition } from '@graphql-tools/schema'; import { TypeSource, Maybe, ExecutionRequest } from '@graphql-tools/utils'; interface MergeTypeCandidate<TContext = Record<string, any>> { type: GraphQLNamedType; subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; } interface MergeFieldConfigCandidate<TContext = Record<string, any>> { fieldConfig: GraphQLFieldConfig<any, TContext>; fieldName: string; type: GraphQLObjectType | GraphQLInterfaceType; subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; } interface MergeInputFieldConfigCandidate<TContext = Record<string, any>> { inputFieldConfig: GraphQLInputFieldConfig; fieldName: string; type: GraphQLInputObjectType; subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; } interface MergeEnumValueConfigCandidate<TContext = Record<string, any>> { enumValueConfig: GraphQLEnumValueConfig; enumValue: string; type: GraphQLEnumType; subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; } type MergeTypeFilter<TContext = Record<string, any>> = (mergeTypeCandidates: Array<MergeTypeCandidate<TContext>>, typeName: string) => boolean; interface IStitchSchemasOptions<TContext = Record<string, any>> extends Omit<IExecutableSchemaDefinition<TContext>, 'typeDefs'> { subschemas?: Array<GraphQLSchema | SubschemaConfig<any, any, any, TContext>>; typeDefs?: TypeSource; types?: Array<GraphQLNamedType>; onTypeConflict?: OnTypeConflict<TContext>; /** * Should directive definitions from all subschemas be collected and included in the stitched schema. * * When false, custom directive definitions are excluded and their usages are stripped from all type * and field AST nodes. * * @default true */ mergeDirectives?: boolean | undefined; mergeTypes?: boolean | Array<string> | MergeTypeFilter<TContext>; typeMergingOptions?: TypeMergingOptions<TContext>; subschemaConfigTransforms?: Array<SubschemaConfigTransform<TContext>>; } type SubschemaConfigTransform<TContext = Record<string, any>> = (subschemaConfig: SubschemaConfig<any, any, any, TContext>) => SubschemaConfig<any, any, any, TContext> | Array<SubschemaConfig<any, any, any, TContext>>; interface TypeMergingOptions<TContext = Record<string, any>> { validationSettings?: ValidationSettings; validationScopes?: Record<string, ValidationSettings>; typeCandidateMerger?: (candidates: Array<MergeTypeCandidate<TContext>>) => MergeTypeCandidate<TContext>; typeDescriptionsMerger?: (candidates: Array<MergeTypeCandidate<TContext>>) => Maybe<string>; fieldConfigMerger?: (candidates: Array<MergeFieldConfigCandidate<TContext>>) => GraphQLFieldConfig<any, TContext>; inputFieldConfigMerger?: (candidates: Array<MergeInputFieldConfigCandidate<TContext>>) => GraphQLInputFieldConfig; enumValueConfigMerger?: (candidates: Array<MergeEnumValueConfigCandidate<TContext>>) => GraphQLEnumValueConfig; useNonNullableFieldOnConflict?: boolean; } declare enum ValidationLevel { Error = "error", Warn = "warn", Off = "off" } interface ValidationSettings { validationLevel?: ValidationLevel; strictNullComparison?: boolean; proxiableScalars?: Record<string, Array<string>>; } type OnTypeConflict<TContext = Record<string, any>> = (left: GraphQLNamedType, right: GraphQLNamedType, info?: { left: { subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; }; right: { subschema?: GraphQLSchema | SubschemaConfig<any, any, any, TContext>; transformedSubschema?: Subschema<any, any, any, TContext>; }; }) => GraphQLNamedType; declare module '@graphql-tools/utils' { interface IFieldResolverOptions<TSource = any, TContext = any, TArgs = any> { selectionSet?: string | ((node: FieldNode) => SelectionSetNode); } } declare function stitchSchemas<TContext extends Record<string, any> = Record<string, any>>(options: IStitchSchemasOptions<TContext>): GraphQLSchema; /** * Overlay a single entryPoint on the parent merge config so shared * `key` / `argsFromKeys` / `valuesFromResults` / `dataLoaderOptions` / * `args` / `fieldName` defaults are preserved. */ type OverlayMergedTypeEntryPointResult<TContext extends Record<string, any> = any> = MergedTypeResolverOptions & { selectionSet?: string; key?: (originalResult: any) => any; resolve?: MergedTypeResolver<TContext>; }; declare function overlayMergedTypeEntryPoint<TContext extends Record<string, any> = any>(mergedTypeConfig: MergedTypeConfig<any, any, TContext>, entryPoint: MergedTypeResolverOptions & { selectionSet?: string; key?: (originalResult: any) => any; resolve?: MergedTypeResolver<any>; }): OverlayMergedTypeEntryPointResult<TContext>; /** * Multi-key merge on one subschema: try each entryPoint's `key` until one * returns a non-null value, then batchDelegate with that entry's fieldName. */ declare function createMultiEntryMergedTypeResolver<TContext extends Record<string, any> = any>(mergedTypeConfig: MergedTypeConfig<any, any, TContext>, mergedType?: GraphQLOutputType | string): MergedTypeResolver<TContext> | undefined; declare function createMergedTypeResolver<TContext extends Record<string, any> = any>(mergedTypeResolverOptions: MergedTypeResolverOptions, mergedType?: GraphQLOutputType | string): MergedTypeResolver<TContext> | undefined; /** * True when `parent` carries the fields of an entry's selectionSet, including * nested key paths like `{ organization { id } }`. * Used so multi-entry pick does not treat `{ upc: undefined }` from a naive * `({ upc }) => ({ upc })` key as a usable representation, and does not pick * an entry whose nested key object is empty. */ declare function entryPointAppliesToParent(entryPoint: Pick<MergedTypeEntryPoint, 'selectionSet'>, parent: any): boolean; declare const forwardArgsToSelectionSet: (selectionSet: string, mapping?: Record<string, string[]>) => (field: FieldNode) => SelectionSetNode; /** * Computed-field deferral: keep computed fields on the single * subschema schema (so merge hops can select them), but strip them from * outbound requests that are not an internal merge hop. Internal merge hops * set `skipTypeMerging` and use a merge entry `fieldName`; a client query that * happens to call the same root field still goes through deferral. * * When a computed field is stripped, inject the type's merge-key fields * (`@key` / `selectionSet`) so the parent still carries identity for later * leftover hops. Nested key selections are preserved as parsed field nodes * and merged into already-selected parent fields when needed. */ declare class DeferComputedFieldsTransform<TContext = Record<string, any>> implements Transform<Record<string, any>, TContext> { private readonly computedByType; private readonly keyFieldsByType; private readonly mergeEntryFieldNames; private readonly schema; constructor(schema: GraphQLSchema, computedByType: Map<string, Set<string>>, keyFieldsByType: Map<string, FieldNode[]>, mergeEntryFieldNames: Set<string>); transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext<TContext>): ExecutionRequest; } /** * Single-subschema request deferral for computed fields (`computed: true` + `selectionSet`). */ declare function deferComputedFieldsTransformer(subschemaConfig: SubschemaConfig): Array<SubschemaConfig>; /** * Normalize merged-type `entryPoints` without cloning the subschema. * * - 0 entryPoints → unchanged * - 1 entryPoint → flatten onto the merge config (single-key shape) * - 2+ entryPoints → leave `entryPoints` on the single SubschemaConfig so * stitchingInfo / the delegation plan builder pick the matching key at hop time */ declare function splitMergedTypeEntryPointsTransformer(subschemaConfig: SubschemaConfig): Array<SubschemaConfig>; declare function handleRelaySubschemas(subschemas: SubschemaConfig[], getTypeNameFromId?: (id: string) => string): SubschemaConfig<any, any, any, Record<string, any>>[]; /** * Creates an executor that uses the schema created by stitching together multiple subschemas. * Not ready for production * Breaking changes can be introduced in the meanwhile * * @experimental * */ declare function createStitchingExecutor(stitchedSchema: GraphQLSchema): (executorRequest: ExecutionRequest) => Promise<{ data: Record<string, any>; }>; declare function getDefaultFieldConfigMerger(useNonNullableFieldOnConflict?: boolean): <TContext = Record<string, any>>(candidates: Array<MergeFieldConfigCandidate<TContext>>) => GraphQLFieldConfig<any, TContext, any>; declare const calculateSelectionScore: (selections: readonly SelectionNode[], fragments: Record<string, FragmentDefinitionNode>) => number; export { DeferComputedFieldsTransform, type IStitchSchemasOptions, type MergeEnumValueConfigCandidate, type MergeFieldConfigCandidate, type MergeInputFieldConfigCandidate, type MergeTypeCandidate, type MergeTypeFilter, type OnTypeConflict, type SubschemaConfigTransform, type TypeMergingOptions, ValidationLevel, type ValidationSettings, calculateSelectionScore, createMergedTypeResolver, createMultiEntryMergedTypeResolver, createStitchingExecutor, deferComputedFieldsTransformer, entryPointAppliesToParent, forwardArgsToSelectionSet, getDefaultFieldConfigMerger, handleRelaySubschemas, overlayMergedTypeEntryPoint, splitMergedTypeEntryPointsTransformer, stitchSchemas };