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 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/little-hairs-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@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.
10 changes: 5 additions & 5 deletions packages/federation/test/__snapshots__/supergraphs.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ type Query {
review(id: Int!): Review
}

type DeliveryEstimates {
estimatedDelivery: String
fastestDelivery: String
}

type Product implements ProductItf & SkuItf {
id: ID! @tag(name: "hi-from-products")
delivery(zip: String): DeliveryEstimates
Expand Down Expand Up @@ -246,6 +241,11 @@ enum ShippingClass {
OVERNIGHT
}

type DeliveryEstimates {
estimatedDelivery: String
fastestDelivery: String
}

type Panda {
name: ID!
favoriteFood: String @tag(name: "nom-nom-nom")
Expand Down
24 changes: 23 additions & 1 deletion packages/stitch/src/createDelegationPlanBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,29 @@ 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 @@ -27,6 +27,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 @@ -35,7 +36,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
Loading