Skip to content

Commit 2ae3c51

Browse files
committed
reorganize(resolveExternalValue)
1 parent 1d3971d commit 2ae3c51

File tree

7 files changed

+169
-191
lines changed

7 files changed

+169
-191
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { GraphQLSchema, FieldNode, GraphQLObjectType, GraphQLResolveInfo } from 'graphql';
22

33
import { collectFields, GraphQLExecutionContext } from '@graphql-tools/utils';
4-
import { isSubschemaConfig } from '../Subschema';
5-
import { MergedTypeInfo, SubschemaConfig, StitchingInfo } from '../types';
64

7-
import { memoizeInfoAnd2Objects } from '../memoize';
5+
import { isSubschemaConfig } from './Subschema';
6+
import { MergedTypeInfo, SubschemaConfig, StitchingInfo } from './types';
7+
import { memoizeInfoAnd2Objects } from './memoize';
88

99
function collectSubFields(info: GraphQLResolveInfo, typeName: string): Record<string, Array<FieldNode>> {
1010
let subFieldNodes: Record<string, Array<FieldNode>> = Object.create(null);

packages/delegate/src/resolveExternalValue/mergeFields.ts renamed to packages/delegate/src/mergeFields.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111

1212
import isPromise from 'is-promise';
1313

14-
import { MergedTypeInfo, SubschemaConfig } from '../types';
15-
import { memoize4, memoize3, memoize2 } from '../memoize';
16-
import { mergeExternalData } from '../externalData';
14+
import { MergedTypeInfo, SubschemaConfig } from './types';
15+
import { memoize4, memoize3, memoize2 } from './memoize';
16+
import { mergeExternalData } from './externalData';
1717

1818
const sortSubschemasByProxiability = memoize4(function (
1919
mergedTypeInfo: MergedTypeInfo,
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import {
2+
GraphQLResolveInfo,
3+
getNullableType,
4+
isCompositeType,
5+
isLeafType,
6+
isListType,
7+
GraphQLError,
8+
GraphQLSchema,
9+
GraphQLCompositeType,
10+
isAbstractType,
11+
GraphQLList,
12+
GraphQLType,
13+
locatedError,
14+
} from 'graphql';
15+
16+
import { SubschemaConfig } from './types';
17+
import { annotateExternalData } from './externalData';
18+
import { getFieldsNotInSubschema } from './getFieldsNotInSubschema';
19+
import { mergeFields } from './mergeFields';
20+
21+
export function resolveExternalValue(
22+
result: any,
23+
unpathedErrors: Array<GraphQLError>,
24+
subschema: GraphQLSchema | SubschemaConfig,
25+
context: Record<string, any>,
26+
info: GraphQLResolveInfo,
27+
returnType = info.returnType,
28+
skipTypeMerging?: boolean
29+
): any {
30+
const type = getNullableType(returnType);
31+
32+
if (result instanceof Error) {
33+
return result;
34+
}
35+
36+
if (result == null) {
37+
return reportUnpathedErrorsViaNull(unpathedErrors);
38+
}
39+
40+
if (isLeafType(type)) {
41+
return type.parseValue(result);
42+
} else if (isCompositeType(type)) {
43+
return resolveExternalObject(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
44+
} else if (isListType(type)) {
45+
return resolveExternalList(type, result, unpathedErrors, subschema, context, info, skipTypeMerging);
46+
}
47+
}
48+
49+
function resolveExternalObject(
50+
type: GraphQLCompositeType,
51+
object: any,
52+
unpathedErrors: Array<GraphQLError>,
53+
subschema: GraphQLSchema | SubschemaConfig,
54+
context: Record<string, any>,
55+
info: GraphQLResolveInfo,
56+
skipTypeMerging?: boolean
57+
) {
58+
const stitchingInfo = info?.schema.extensions?.stitchingInfo;
59+
60+
annotateExternalData(object, unpathedErrors, subschema);
61+
62+
if (skipTypeMerging || !stitchingInfo) {
63+
return object;
64+
}
65+
66+
const typeName = isAbstractType(type) ? info.schema.getTypeMap()[object.__typename].name : type.name;
67+
const mergedTypeInfo = stitchingInfo.mergedTypes[typeName];
68+
let targetSubschemas: Array<SubschemaConfig>;
69+
70+
if (mergedTypeInfo != null) {
71+
targetSubschemas = mergedTypeInfo.targetSubschemas.get(subschema);
72+
}
73+
74+
if (!targetSubschemas) {
75+
return object;
76+
}
77+
78+
const fieldNodes = getFieldsNotInSubschema(info, subschema, mergedTypeInfo);
79+
80+
return mergeFields(
81+
mergedTypeInfo,
82+
typeName,
83+
object,
84+
fieldNodes,
85+
subschema as SubschemaConfig,
86+
targetSubschemas,
87+
context,
88+
info
89+
);
90+
}
91+
92+
function resolveExternalList(
93+
type: GraphQLList<any>,
94+
list: Array<any>,
95+
unpathedErrors: Array<GraphQLError>,
96+
subschema: GraphQLSchema | SubschemaConfig,
97+
context: Record<string, any>,
98+
info: GraphQLResolveInfo,
99+
skipTypeMerging?: boolean
100+
) {
101+
return list.map(listMember =>
102+
resolveExternalListMember(
103+
getNullableType(type.ofType),
104+
listMember,
105+
unpathedErrors,
106+
subschema,
107+
context,
108+
info,
109+
skipTypeMerging
110+
)
111+
);
112+
}
113+
114+
function resolveExternalListMember(
115+
type: GraphQLType,
116+
listMember: any,
117+
unpathedErrors: Array<GraphQLError>,
118+
subschema: GraphQLSchema | SubschemaConfig,
119+
context: Record<string, any>,
120+
info: GraphQLResolveInfo,
121+
skipTypeMerging?: boolean
122+
): any {
123+
if (listMember instanceof Error) {
124+
return listMember;
125+
}
126+
127+
if (listMember == null) {
128+
return reportUnpathedErrorsViaNull(unpathedErrors);
129+
}
130+
131+
if (isLeafType(type)) {
132+
return type.parseValue(listMember);
133+
} else if (isCompositeType(type)) {
134+
return resolveExternalObject(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
135+
} else if (isListType(type)) {
136+
return resolveExternalList(type, listMember, unpathedErrors, subschema, context, info, skipTypeMerging);
137+
}
138+
}
139+
140+
const reportedErrors: WeakMap<GraphQLError, boolean> = new Map();
141+
142+
function reportUnpathedErrorsViaNull(unpathedErrors: Array<GraphQLError>) {
143+
if (unpathedErrors.length) {
144+
const unreportedErrors: Array<GraphQLError> = [];
145+
unpathedErrors.forEach(error => {
146+
if (!reportedErrors.has(error)) {
147+
unreportedErrors.push(error);
148+
reportedErrors.set(error, true);
149+
}
150+
});
151+
152+
if (unreportedErrors.length) {
153+
if (unreportedErrors.length === 1) {
154+
return unreportedErrors[0];
155+
}
156+
157+
const combinedError = new AggregateError(unreportedErrors);
158+
return locatedError(combinedError, undefined, unreportedErrors[0].path);
159+
}
160+
}
161+
162+
return null;
163+
}

packages/delegate/src/resolveExternalValue/handleList.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

packages/delegate/src/resolveExternalValue/handleNull.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/delegate/src/resolveExternalValue/handleObject.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/delegate/src/resolveExternalValue/index.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)