@graphql-tools/delegate 10.2.20 → 10.2.21-alpha-a2dbb6d5dc18a597931ad30d27ce231af0ab421b

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
+ ## 10.2.21-alpha-a2dbb6d5dc18a597931ad30d27ce231af0ab421b
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1301](https://github.com/graphql-hive/gateway/pull/1301) [`bd1a66a`](https://github.com/graphql-hive/gateway/commit/bd1a66aad2b10351d831f781977cdd2d586c56fd) Thanks [@enisdenjo](https://github.com/enisdenjo)! - Filter selection sets recursively when finalizing gateway requests
8
+
9
+ Because abstract types can be nested.
10
+
3
11
  ## 10.2.20
4
12
 
5
13
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -945,7 +945,36 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
945
945
  const typeInfo = getTypeInfoWithType(schema, type);
946
946
  const seenNonNullableMap = /* @__PURE__ */ new WeakMap();
947
947
  const seenNullableMap = /* @__PURE__ */ new WeakMap();
948
- const filteredSelectionSet = graphql.visit(
948
+ const filteredSelectionSet = filterSelectionSet(
949
+ schema,
950
+ typeInfo,
951
+ validFragments,
952
+ selectionSet,
953
+ onOverlappingAliases,
954
+ usedFragments,
955
+ seenNonNullableMap,
956
+ seenNullableMap
957
+ );
958
+ graphql.visit(
959
+ filteredSelectionSet,
960
+ {
961
+ [graphql.Kind.VARIABLE]: (variableNode) => {
962
+ usedVariables.push(variableNode.name.value);
963
+ }
964
+ },
965
+ // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
966
+ // empty keys cannot be removed only because of typescript errors
967
+ // will hopefully be fixed in future version of graphql-js to be optional
968
+ variablesVisitorKeys
969
+ );
970
+ return {
971
+ selectionSet: filteredSelectionSet,
972
+ usedFragments,
973
+ usedVariables
974
+ };
975
+ }
976
+ function filterSelectionSet(schema, typeInfo, validFragments, selectionSet, onOverlappingAliases, usedFragments, seenNonNullableMap, seenNullableMap) {
977
+ return graphql.visit(
949
978
  selectionSet,
950
979
  graphql.visitWithTypeInfo(typeInfo, {
951
980
  [graphql.Kind.FIELD]: {
@@ -994,30 +1023,73 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
994
1023
  }
995
1024
  }
996
1025
  if (possibleTypeNames.length > 0) {
997
- return possibleTypeNames.map((possibleTypeName) => ({
998
- kind: graphql.Kind.INLINE_FRAGMENT,
999
- typeCondition: {
1000
- kind: graphql.Kind.NAMED_TYPE,
1001
- name: {
1002
- kind: graphql.Kind.NAME,
1003
- value: possibleTypeName
1004
- }
1005
- },
1006
- selectionSet: {
1007
- kind: graphql.Kind.SELECTION_SET,
1008
- selections: [node]
1026
+ return possibleTypeNames.map((possibleTypeName) => {
1027
+ if (!node.selectionSet?.selections) {
1028
+ return {
1029
+ kind: graphql.Kind.INLINE_FRAGMENT,
1030
+ typeCondition: {
1031
+ kind: graphql.Kind.NAMED_TYPE,
1032
+ name: {
1033
+ kind: graphql.Kind.NAME,
1034
+ value: possibleTypeName
1035
+ }
1036
+ },
1037
+ selectionSet: {
1038
+ kind: graphql.Kind.SELECTION_SET,
1039
+ selections: [node]
1040
+ }
1041
+ };
1042
+ }
1043
+ const possibleType = schema.getType(
1044
+ possibleTypeName
1045
+ );
1046
+ const possibleField = possibleType.getFields()[node.name.value];
1047
+ if (!possibleField) {
1048
+ return void 0;
1049
+ }
1050
+ const fieldFilteredSelectionSet = filterSelectionSet(
1051
+ schema,
1052
+ getTypeInfoWithType(schema, possibleField.type),
1053
+ validFragments,
1054
+ node.selectionSet,
1055
+ onOverlappingAliases,
1056
+ usedFragments,
1057
+ seenNonNullableMap,
1058
+ seenNullableMap
1059
+ );
1060
+ if (!fieldFilteredSelectionSet.selections.length) {
1061
+ return void 0;
1009
1062
  }
1010
- }));
1063
+ return {
1064
+ kind: graphql.Kind.INLINE_FRAGMENT,
1065
+ typeCondition: {
1066
+ kind: graphql.Kind.NAMED_TYPE,
1067
+ name: {
1068
+ kind: graphql.Kind.NAME,
1069
+ value: possibleTypeName
1070
+ }
1071
+ },
1072
+ selectionSet: {
1073
+ kind: graphql.Kind.SELECTION_SET,
1074
+ selections: [
1075
+ {
1076
+ ...node,
1077
+ selectionSet: fieldFilteredSelectionSet
1078
+ }
1079
+ ]
1080
+ }
1081
+ };
1082
+ });
1011
1083
  }
1012
1084
  }
1013
1085
  return void 0;
1014
1086
  },
1015
1087
  leave: (node) => {
1016
- const type2 = typeInfo.getType();
1017
- if (type2 == null) {
1088
+ const type = typeInfo.getType();
1089
+ if (type == null) {
1018
1090
  return null;
1019
1091
  }
1020
- const namedType = graphql.getNamedType(type2);
1092
+ const namedType = graphql.getNamedType(type);
1021
1093
  if (schema.getType(namedType.name) == null) {
1022
1094
  return null;
1023
1095
  }
@@ -1164,23 +1236,6 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
1164
1236
  // will hopefully be fixed in future version of graphql-js to be optional
1165
1237
  filteredSelectionSetVisitorKeys
1166
1238
  );
1167
- graphql.visit(
1168
- filteredSelectionSet,
1169
- {
1170
- [graphql.Kind.VARIABLE]: (variableNode) => {
1171
- usedVariables.push(variableNode.name.value);
1172
- }
1173
- },
1174
- // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
1175
- // empty keys cannot be removed only because of typescript errors
1176
- // will hopefully be fixed in future version of graphql-js to be optional
1177
- variablesVisitorKeys
1178
- );
1179
- return {
1180
- selectionSet: filteredSelectionSet,
1181
- usedFragments,
1182
- usedVariables
1183
- };
1184
1239
  }
1185
1240
  function union(...arrays) {
1186
1241
  const cache = /* @__PURE__ */ Object.create(null);
package/dist/index.js CHANGED
@@ -945,7 +945,36 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
945
945
  const typeInfo = getTypeInfoWithType(schema, type);
946
946
  const seenNonNullableMap = /* @__PURE__ */ new WeakMap();
947
947
  const seenNullableMap = /* @__PURE__ */ new WeakMap();
948
- const filteredSelectionSet = visit(
948
+ const filteredSelectionSet = filterSelectionSet(
949
+ schema,
950
+ typeInfo,
951
+ validFragments,
952
+ selectionSet,
953
+ onOverlappingAliases,
954
+ usedFragments,
955
+ seenNonNullableMap,
956
+ seenNullableMap
957
+ );
958
+ visit(
959
+ filteredSelectionSet,
960
+ {
961
+ [Kind.VARIABLE]: (variableNode) => {
962
+ usedVariables.push(variableNode.name.value);
963
+ }
964
+ },
965
+ // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
966
+ // empty keys cannot be removed only because of typescript errors
967
+ // will hopefully be fixed in future version of graphql-js to be optional
968
+ variablesVisitorKeys
969
+ );
970
+ return {
971
+ selectionSet: filteredSelectionSet,
972
+ usedFragments,
973
+ usedVariables
974
+ };
975
+ }
976
+ function filterSelectionSet(schema, typeInfo, validFragments, selectionSet, onOverlappingAliases, usedFragments, seenNonNullableMap, seenNullableMap) {
977
+ return visit(
949
978
  selectionSet,
950
979
  visitWithTypeInfo(typeInfo, {
951
980
  [Kind.FIELD]: {
@@ -994,30 +1023,73 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
994
1023
  }
995
1024
  }
996
1025
  if (possibleTypeNames.length > 0) {
997
- return possibleTypeNames.map((possibleTypeName) => ({
998
- kind: Kind.INLINE_FRAGMENT,
999
- typeCondition: {
1000
- kind: Kind.NAMED_TYPE,
1001
- name: {
1002
- kind: Kind.NAME,
1003
- value: possibleTypeName
1004
- }
1005
- },
1006
- selectionSet: {
1007
- kind: Kind.SELECTION_SET,
1008
- selections: [node]
1026
+ return possibleTypeNames.map((possibleTypeName) => {
1027
+ if (!node.selectionSet?.selections) {
1028
+ return {
1029
+ kind: Kind.INLINE_FRAGMENT,
1030
+ typeCondition: {
1031
+ kind: Kind.NAMED_TYPE,
1032
+ name: {
1033
+ kind: Kind.NAME,
1034
+ value: possibleTypeName
1035
+ }
1036
+ },
1037
+ selectionSet: {
1038
+ kind: Kind.SELECTION_SET,
1039
+ selections: [node]
1040
+ }
1041
+ };
1042
+ }
1043
+ const possibleType = schema.getType(
1044
+ possibleTypeName
1045
+ );
1046
+ const possibleField = possibleType.getFields()[node.name.value];
1047
+ if (!possibleField) {
1048
+ return void 0;
1049
+ }
1050
+ const fieldFilteredSelectionSet = filterSelectionSet(
1051
+ schema,
1052
+ getTypeInfoWithType(schema, possibleField.type),
1053
+ validFragments,
1054
+ node.selectionSet,
1055
+ onOverlappingAliases,
1056
+ usedFragments,
1057
+ seenNonNullableMap,
1058
+ seenNullableMap
1059
+ );
1060
+ if (!fieldFilteredSelectionSet.selections.length) {
1061
+ return void 0;
1009
1062
  }
1010
- }));
1063
+ return {
1064
+ kind: Kind.INLINE_FRAGMENT,
1065
+ typeCondition: {
1066
+ kind: Kind.NAMED_TYPE,
1067
+ name: {
1068
+ kind: Kind.NAME,
1069
+ value: possibleTypeName
1070
+ }
1071
+ },
1072
+ selectionSet: {
1073
+ kind: Kind.SELECTION_SET,
1074
+ selections: [
1075
+ {
1076
+ ...node,
1077
+ selectionSet: fieldFilteredSelectionSet
1078
+ }
1079
+ ]
1080
+ }
1081
+ };
1082
+ });
1011
1083
  }
1012
1084
  }
1013
1085
  return void 0;
1014
1086
  },
1015
1087
  leave: (node) => {
1016
- const type2 = typeInfo.getType();
1017
- if (type2 == null) {
1088
+ const type = typeInfo.getType();
1089
+ if (type == null) {
1018
1090
  return null;
1019
1091
  }
1020
- const namedType = getNamedType(type2);
1092
+ const namedType = getNamedType(type);
1021
1093
  if (schema.getType(namedType.name) == null) {
1022
1094
  return null;
1023
1095
  }
@@ -1164,23 +1236,6 @@ function finalizeSelectionSet(schema, type, validFragments, selectionSet, onOver
1164
1236
  // will hopefully be fixed in future version of graphql-js to be optional
1165
1237
  filteredSelectionSetVisitorKeys
1166
1238
  );
1167
- visit(
1168
- filteredSelectionSet,
1169
- {
1170
- [Kind.VARIABLE]: (variableNode) => {
1171
- usedVariables.push(variableNode.name.value);
1172
- }
1173
- },
1174
- // visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
1175
- // empty keys cannot be removed only because of typescript errors
1176
- // will hopefully be fixed in future version of graphql-js to be optional
1177
- variablesVisitorKeys
1178
- );
1179
- return {
1180
- selectionSet: filteredSelectionSet,
1181
- usedFragments,
1182
- usedVariables
1183
- };
1184
1239
  }
1185
1240
  function union(...arrays) {
1186
1241
  const cache = /* @__PURE__ */ Object.create(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/delegate",
3
- "version": "10.2.20",
3
+ "version": "10.2.21-alpha-a2dbb6d5dc18a597931ad30d27ce231af0ab421b",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {