@graphql-tools/delegate 12.0.11 → 12.0.12-alpha-2e265b46bae54740fecacd19c662af81a07b39f9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @graphql-tools/delegate
2
2
 
3
+ ## 12.0.12-alpha-2e265b46bae54740fecacd19c662af81a07b39f9
4
+ ### Patch Changes
5
+
6
+
7
+
8
+ - [#2106](https://github.com/graphql-hive/gateway/pull/2106) [`bcdf6a1`](https://github.com/graphql-hive/gateway/commit/bcdf6a1333a84f3ead114d676e66e609c350e084) Thanks [@ardatan](https://github.com/ardatan)! - - Optimizations to select ONLY required fields when fetching the missing fields from other subgraphs
9
+ - Do not try to resolve types from the subschemas which only have the stub types, for example if a subgraph only has `id` field as a stub, do not use that subgraph as a target subgraph for resolving the type, because it will not have any other fields than `id`.
10
+
3
11
  ## 12.0.11
4
12
  ### Patch Changes
5
13
 
package/dist/index.cjs CHANGED
@@ -598,6 +598,9 @@ function finalizeGatewayDocument(targetSchema, fragments, operations, onOverlapp
598
598
  const validFragments = [];
599
599
  const validFragmentsWithType = /* @__PURE__ */ Object.create(null);
600
600
  for (const fragment of fragments) {
601
+ if (fragment.selectionSet.selections.length === 0) {
602
+ continue;
603
+ }
601
604
  const typeName = fragment.typeCondition.name.value;
602
605
  const type = targetSchema.getType(typeName);
603
606
  if (type != null) {
@@ -1206,6 +1209,12 @@ function prepareGatewayDocument(originalDocument, transformedSchema, returnType,
1206
1209
  kind: graphql.Kind.DOCUMENT,
1207
1210
  definitions: [...operations, ...fragments, ...expandedFragments]
1208
1211
  };
1212
+ const fragmentMap = /* @__PURE__ */ Object.create(null);
1213
+ for (const fragment of expandedDocument.definitions) {
1214
+ if (fragment.kind === graphql.Kind.FRAGMENT_DEFINITION) {
1215
+ fragmentMap[fragment.name.value] = fragment;
1216
+ }
1217
+ }
1209
1218
  const visitorKeyMap = {
1210
1219
  Document: ["definitions"],
1211
1220
  OperationDefinition: ["selectionSet"],
@@ -1229,7 +1238,8 @@ function prepareGatewayDocument(originalDocument, transformedSchema, returnType,
1229
1238
  fieldNodesByField,
1230
1239
  dynamicSelectionSetsByField,
1231
1240
  infoSchema,
1232
- visitedSelections
1241
+ visitedSelections,
1242
+ fragmentMap
1233
1243
  )
1234
1244
  }),
1235
1245
  // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
@@ -1267,11 +1277,26 @@ const getExtraPossibleTypesFn = utils.memoize2(function getExtraPossibleTypes(tr
1267
1277
  return extraTypesForSubschema;
1268
1278
  };
1269
1279
  });
1270
- function visitSelectionSet(node, fragmentReplacements, transformedSchema, typeInfo, possibleTypesMap, reversePossibleTypesMap2, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField, infoSchema, visitedSelections) {
1280
+ function visitSelectionSet(node, fragmentReplacements, transformedSchema, typeInfo, possibleTypesMap, reversePossibleTypesMap2, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField, infoSchema, visitedSelections, fragmentMap) {
1271
1281
  const newSelections = /* @__PURE__ */ new Set();
1272
1282
  const maybeType = typeInfo.getParentType();
1273
1283
  if (maybeType != null) {
1274
1284
  const parentType = graphql.getNamedType(maybeType);
1285
+ if (isSelectionSetSatisfiedBySchema(
1286
+ transformedSchema,
1287
+ parentType,
1288
+ node,
1289
+ fragmentMap,
1290
+ infoSchema
1291
+ )) {
1292
+ for (const selection of node.selections) {
1293
+ addSelectionNodeToSelections(newSelections, selection);
1294
+ }
1295
+ return {
1296
+ ...node,
1297
+ selections: Array.from(newSelections)
1298
+ };
1299
+ }
1275
1300
  const parentTypeName = parentType.name;
1276
1301
  const fieldNodes = fieldNodesByType[parentTypeName];
1277
1302
  if (fieldNodes) {
@@ -1461,6 +1486,103 @@ function isFieldNodeSatisfiedBySelections(fieldNode, existing) {
1461
1486
  }
1462
1487
  return false;
1463
1488
  }
1489
+ function isSelectionSetSatisfiedBySchema(schema, type, selectionSet, fragmentsMap, infoSchema) {
1490
+ const namedType = graphql.getNamedType(type);
1491
+ if (graphql.isLeafType(namedType)) {
1492
+ return true;
1493
+ }
1494
+ for (const selection of selectionSet.selections) {
1495
+ if (selection.kind === graphql.Kind.FIELD) {
1496
+ if (graphql.isAbstractType(namedType)) {
1497
+ return false;
1498
+ }
1499
+ const fieldName = selection.name.value;
1500
+ if (fieldName === "__typename") {
1501
+ continue;
1502
+ }
1503
+ if (graphql.isObjectType(namedType) || graphql.isInterfaceType(namedType)) {
1504
+ const field = namedType.getFields()[fieldName];
1505
+ if (!field) {
1506
+ return false;
1507
+ }
1508
+ const typeInInfoSchema = infoSchema.getType(
1509
+ namedType.name
1510
+ );
1511
+ const fieldInInfoSchema = typeInInfoSchema?.getFields?.()?.[fieldName];
1512
+ const resolverInInfoSchema = fieldInInfoSchema?.resolve;
1513
+ if (resolverInInfoSchema != null && resolverInInfoSchema.name !== "defaultMergedResolver" && resolverInInfoSchema.name !== "defaultFieldResolver") {
1514
+ return false;
1515
+ }
1516
+ if (selection.selectionSet) {
1517
+ const fieldType = graphql.getNamedType(field.type);
1518
+ const satisfied = isSelectionSetSatisfiedBySchema(
1519
+ schema,
1520
+ fieldType,
1521
+ selection.selectionSet,
1522
+ fragmentsMap,
1523
+ infoSchema
1524
+ );
1525
+ if (!satisfied) {
1526
+ return false;
1527
+ }
1528
+ }
1529
+ } else {
1530
+ return false;
1531
+ }
1532
+ } else if (selection.kind === graphql.Kind.INLINE_FRAGMENT) {
1533
+ if (selection.typeCondition) {
1534
+ const typeConditionName = selection.typeCondition.name.value;
1535
+ const typeCondition = schema.getType(typeConditionName);
1536
+ if (!typeCondition) {
1537
+ return false;
1538
+ }
1539
+ const satisfied = isSelectionSetSatisfiedBySchema(
1540
+ schema,
1541
+ typeCondition,
1542
+ selection.selectionSet,
1543
+ fragmentsMap,
1544
+ infoSchema
1545
+ );
1546
+ if (!satisfied) {
1547
+ return false;
1548
+ }
1549
+ } else {
1550
+ const satisfied = isSelectionSetSatisfiedBySchema(
1551
+ schema,
1552
+ type,
1553
+ selection.selectionSet,
1554
+ fragmentsMap,
1555
+ infoSchema
1556
+ );
1557
+ if (!satisfied) {
1558
+ return false;
1559
+ }
1560
+ }
1561
+ } else if (selection.kind === graphql.Kind.FRAGMENT_SPREAD) {
1562
+ const fragmentName = selection.name.value;
1563
+ const fragment = fragmentsMap[fragmentName];
1564
+ if (!fragment) {
1565
+ return false;
1566
+ }
1567
+ const typeConditionName = fragment.typeCondition.name.value;
1568
+ const typeCondition = schema.getType(typeConditionName);
1569
+ if (!typeCondition) {
1570
+ return false;
1571
+ }
1572
+ const satisfied = isSelectionSetSatisfiedBySchema(
1573
+ schema,
1574
+ typeCondition,
1575
+ fragment.selectionSet,
1576
+ fragmentsMap,
1577
+ infoSchema
1578
+ );
1579
+ if (!satisfied) {
1580
+ return false;
1581
+ }
1582
+ }
1583
+ }
1584
+ return true;
1585
+ }
1464
1586
  function isSelectionSetSatisfied({
1465
1587
  incoming,
1466
1588
  existing
package/dist/index.js CHANGED
@@ -598,6 +598,9 @@ function finalizeGatewayDocument(targetSchema, fragments, operations, onOverlapp
598
598
  const validFragments = [];
599
599
  const validFragmentsWithType = /* @__PURE__ */ Object.create(null);
600
600
  for (const fragment of fragments) {
601
+ if (fragment.selectionSet.selections.length === 0) {
602
+ continue;
603
+ }
601
604
  const typeName = fragment.typeCondition.name.value;
602
605
  const type = targetSchema.getType(typeName);
603
606
  if (type != null) {
@@ -1206,6 +1209,12 @@ function prepareGatewayDocument(originalDocument, transformedSchema, returnType,
1206
1209
  kind: Kind.DOCUMENT,
1207
1210
  definitions: [...operations, ...fragments, ...expandedFragments]
1208
1211
  };
1212
+ const fragmentMap = /* @__PURE__ */ Object.create(null);
1213
+ for (const fragment of expandedDocument.definitions) {
1214
+ if (fragment.kind === Kind.FRAGMENT_DEFINITION) {
1215
+ fragmentMap[fragment.name.value] = fragment;
1216
+ }
1217
+ }
1209
1218
  const visitorKeyMap = {
1210
1219
  Document: ["definitions"],
1211
1220
  OperationDefinition: ["selectionSet"],
@@ -1229,7 +1238,8 @@ function prepareGatewayDocument(originalDocument, transformedSchema, returnType,
1229
1238
  fieldNodesByField,
1230
1239
  dynamicSelectionSetsByField,
1231
1240
  infoSchema,
1232
- visitedSelections
1241
+ visitedSelections,
1242
+ fragmentMap
1233
1243
  )
1234
1244
  }),
1235
1245
  // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
@@ -1267,11 +1277,26 @@ const getExtraPossibleTypesFn = memoize2(function getExtraPossibleTypes(transfor
1267
1277
  return extraTypesForSubschema;
1268
1278
  };
1269
1279
  });
1270
- function visitSelectionSet(node, fragmentReplacements, transformedSchema, typeInfo, possibleTypesMap, reversePossibleTypesMap2, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField, infoSchema, visitedSelections) {
1280
+ function visitSelectionSet(node, fragmentReplacements, transformedSchema, typeInfo, possibleTypesMap, reversePossibleTypesMap2, interfaceExtensionsMap, fieldNodesByType, fieldNodesByField, dynamicSelectionSetsByField, infoSchema, visitedSelections, fragmentMap) {
1271
1281
  const newSelections = /* @__PURE__ */ new Set();
1272
1282
  const maybeType = typeInfo.getParentType();
1273
1283
  if (maybeType != null) {
1274
1284
  const parentType = getNamedType(maybeType);
1285
+ if (isSelectionSetSatisfiedBySchema(
1286
+ transformedSchema,
1287
+ parentType,
1288
+ node,
1289
+ fragmentMap,
1290
+ infoSchema
1291
+ )) {
1292
+ for (const selection of node.selections) {
1293
+ addSelectionNodeToSelections(newSelections, selection);
1294
+ }
1295
+ return {
1296
+ ...node,
1297
+ selections: Array.from(newSelections)
1298
+ };
1299
+ }
1275
1300
  const parentTypeName = parentType.name;
1276
1301
  const fieldNodes = fieldNodesByType[parentTypeName];
1277
1302
  if (fieldNodes) {
@@ -1461,6 +1486,103 @@ function isFieldNodeSatisfiedBySelections(fieldNode, existing) {
1461
1486
  }
1462
1487
  return false;
1463
1488
  }
1489
+ function isSelectionSetSatisfiedBySchema(schema, type, selectionSet, fragmentsMap, infoSchema) {
1490
+ const namedType = getNamedType(type);
1491
+ if (isLeafType(namedType)) {
1492
+ return true;
1493
+ }
1494
+ for (const selection of selectionSet.selections) {
1495
+ if (selection.kind === Kind.FIELD) {
1496
+ if (isAbstractType(namedType)) {
1497
+ return false;
1498
+ }
1499
+ const fieldName = selection.name.value;
1500
+ if (fieldName === "__typename") {
1501
+ continue;
1502
+ }
1503
+ if (isObjectType(namedType) || isInterfaceType(namedType)) {
1504
+ const field = namedType.getFields()[fieldName];
1505
+ if (!field) {
1506
+ return false;
1507
+ }
1508
+ const typeInInfoSchema = infoSchema.getType(
1509
+ namedType.name
1510
+ );
1511
+ const fieldInInfoSchema = typeInInfoSchema?.getFields?.()?.[fieldName];
1512
+ const resolverInInfoSchema = fieldInInfoSchema?.resolve;
1513
+ if (resolverInInfoSchema != null && resolverInInfoSchema.name !== "defaultMergedResolver" && resolverInInfoSchema.name !== "defaultFieldResolver") {
1514
+ return false;
1515
+ }
1516
+ if (selection.selectionSet) {
1517
+ const fieldType = getNamedType(field.type);
1518
+ const satisfied = isSelectionSetSatisfiedBySchema(
1519
+ schema,
1520
+ fieldType,
1521
+ selection.selectionSet,
1522
+ fragmentsMap,
1523
+ infoSchema
1524
+ );
1525
+ if (!satisfied) {
1526
+ return false;
1527
+ }
1528
+ }
1529
+ } else {
1530
+ return false;
1531
+ }
1532
+ } else if (selection.kind === Kind.INLINE_FRAGMENT) {
1533
+ if (selection.typeCondition) {
1534
+ const typeConditionName = selection.typeCondition.name.value;
1535
+ const typeCondition = schema.getType(typeConditionName);
1536
+ if (!typeCondition) {
1537
+ return false;
1538
+ }
1539
+ const satisfied = isSelectionSetSatisfiedBySchema(
1540
+ schema,
1541
+ typeCondition,
1542
+ selection.selectionSet,
1543
+ fragmentsMap,
1544
+ infoSchema
1545
+ );
1546
+ if (!satisfied) {
1547
+ return false;
1548
+ }
1549
+ } else {
1550
+ const satisfied = isSelectionSetSatisfiedBySchema(
1551
+ schema,
1552
+ type,
1553
+ selection.selectionSet,
1554
+ fragmentsMap,
1555
+ infoSchema
1556
+ );
1557
+ if (!satisfied) {
1558
+ return false;
1559
+ }
1560
+ }
1561
+ } else if (selection.kind === Kind.FRAGMENT_SPREAD) {
1562
+ const fragmentName = selection.name.value;
1563
+ const fragment = fragmentsMap[fragmentName];
1564
+ if (!fragment) {
1565
+ return false;
1566
+ }
1567
+ const typeConditionName = fragment.typeCondition.name.value;
1568
+ const typeCondition = schema.getType(typeConditionName);
1569
+ if (!typeCondition) {
1570
+ return false;
1571
+ }
1572
+ const satisfied = isSelectionSetSatisfiedBySchema(
1573
+ schema,
1574
+ typeCondition,
1575
+ fragment.selectionSet,
1576
+ fragmentsMap,
1577
+ infoSchema
1578
+ );
1579
+ if (!satisfied) {
1580
+ return false;
1581
+ }
1582
+ }
1583
+ }
1584
+ return true;
1585
+ }
1464
1586
  function isSelectionSetSatisfied({
1465
1587
  incoming,
1466
1588
  existing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/delegate",
3
- "version": "12.0.11",
3
+ "version": "12.0.12-alpha-2e265b46bae54740fecacd19c662af81a07b39f9",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {