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

Composite computed fields #5162

Merged
merged 16 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
6 changes: 6 additions & 0 deletions .changeset/little-hairs-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/stitch': minor
'@graphql-tools/stitching-directives': patch
---

Adding the ability to return non-scalar types from computed fields. Computed fields can now return object types (local or stitched), interfaces, unions, or enums.
2 changes: 1 addition & 1 deletion packages/links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"@graphql-tools/delegate": "^10.0.0",
"@graphql-tools/utils": "^10.0.0",
"apollo-upload-client": "17.0.0",
"node-fetch": "^2.6.5",
"form-data": "^4.0.0",
"node-fetch": "^2.6.5",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
20 changes: 19 additions & 1 deletion packages/stitch/src/createDelegationPlanBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@ function calculateDelegationStage(

const delegationMap: Map<Subschema, SelectionSetNode> = new Map();
for (const fieldNode of fieldNodes) {
if (fieldNode.name.value === '__typename') {
const fieldName = fieldNode.name.value;
if (fieldName === '__typename') {
continue;
}

// check dependencies for computed fields are available in the source schemas
const sourcesWithUnsatisfiedDependencies = sourceSubschemas.filter(
s =>
fieldSelectionSets.get(s) != null &&
fieldSelectionSets.get(s)![fieldName] != null &&
!subschemaTypesContainSelectionSet(mergedTypeInfo, sourceSubschemas, fieldSelectionSets.get(s)![fieldName])
);
if (sourcesWithUnsatisfiedDependencies.length === sourceSubschemas.length) {
unproxiableFieldNodes.push(fieldNode);
for (const source of sourcesWithUnsatisfiedDependencies) {
if (!nonProxiableSubschemas.includes(source)) {
nonProxiableSubschemas.push(source);
}
}
continue;
}

Expand Down
8 changes: 7 additions & 1 deletion packages/stitch/src/getFieldsNotInSubschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function getFieldsNotInSubschema(
const fieldsNotInSchema = new Set<FieldNode>();
for (const [, subFieldNodes] of subFieldNodesByResponseKey) {
const fieldName = subFieldNodes[0].name.value;

if (!fields[fieldName]) {
for (const subFieldNode of subFieldNodes) {
fieldsNotInSchema.add(subFieldNode);
Expand All @@ -36,7 +37,12 @@ export function getFieldsNotInSubschema(
const fieldNodesForField = fieldNodesByField?.[gatewayType.name]?.[fieldName];
if (fieldNodesForField) {
for (const fieldNode of fieldNodesForField) {
if (!fields[fieldNode.name.value]) {
if (fieldNode.name.value !== '__typename' && !fields[fieldNode.name.value]) {
// consider node that depends on something not in the schema as not in the schema
for (const subFieldNode of subFieldNodes) {
fieldsNotInSchema.add(subFieldNode);
}

fieldsNotInSchema.add(fieldNode);
}
}
Expand Down
Loading