@graphql-codegen/plugin-helpers 5.0.4 → 5.1.0-alpha-20240606121041-9e14ec7dd5cde0aa2beb9e8e9f6bd4ccb65650b9

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/cjs/federation.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApolloFederation = exports.removeFederation = exports.addFederationReferencesToSchema = exports.federationSpec = void 0;
3
+ exports.checkObjectTypeFederationDetails = exports.ApolloFederation = exports.removeFederation = exports.addFederationReferencesToSchema = exports.federationSpec = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const utils_1 = require("@graphql-tools/utils");
6
6
  const graphql_1 = require("graphql");
@@ -25,17 +25,18 @@ exports.federationSpec = (0, graphql_1.parse)(/* GraphQL */ `
25
25
  function addFederationReferencesToSchema(schema) {
26
26
  return (0, utils_1.mapSchema)(schema, {
27
27
  [utils_1.MapperKind.OBJECT_TYPE]: type => {
28
- if (isFederationObjectType(type, schema)) {
29
- const typeConfig = type.toConfig();
30
- typeConfig.fields = {
31
- [resolveReferenceFieldName]: {
32
- type,
33
- },
34
- ...typeConfig.fields,
35
- };
36
- return new graphql_1.GraphQLObjectType(typeConfig);
28
+ const objectTypeFederationDetails = checkObjectTypeFederationDetails(type, schema);
29
+ if (!objectTypeFederationDetails || objectTypeFederationDetails.resolvableKeyDirectives.length === 0) {
30
+ return type;
37
31
  }
38
- return type;
32
+ const typeConfig = type.toConfig();
33
+ typeConfig.fields = {
34
+ [resolveReferenceFieldName]: {
35
+ type,
36
+ },
37
+ ...typeConfig.fields,
38
+ };
39
+ return new graphql_1.GraphQLObjectType(typeConfig);
39
40
  },
40
41
  });
41
42
  }
@@ -110,7 +111,7 @@ class ApolloFederation {
110
111
  * @param data
111
112
  */
112
113
  skipField({ fieldNode, parentType }) {
113
- if (!this.enabled || !(0, graphql_1.isObjectType)(parentType) || !isFederationObjectType(parentType, this.schema)) {
114
+ if (!this.enabled || !(0, graphql_1.isObjectType)(parentType) || !checkObjectTypeFederationDetails(parentType, this.schema)) {
114
115
  return false;
115
116
  }
116
117
  return this.isExternalAndNotProvided(fieldNode, parentType);
@@ -126,16 +127,19 @@ class ApolloFederation {
126
127
  transformParentType({ fieldNode, parentType, parentTypeSignature, }) {
127
128
  if (this.enabled &&
128
129
  (0, graphql_1.isObjectType)(parentType) &&
129
- isFederationObjectType(parentType, this.schema) &&
130
130
  (isTypeExtension(parentType, this.schema) || fieldNode.name.value === resolveReferenceFieldName)) {
131
- const keys = getDirectivesByName('key', parentType);
132
- if (keys.length) {
131
+ const objectTypeFederationDetails = checkObjectTypeFederationDetails(parentType, this.schema);
132
+ if (!objectTypeFederationDetails) {
133
+ return parentTypeSignature;
134
+ }
135
+ const { resolvableKeyDirectives } = objectTypeFederationDetails;
136
+ if (resolvableKeyDirectives.length) {
133
137
  const outputs = [`{ __typename: '${parentType.name}' } &`];
134
138
  // Look for @requires and see what the service needs and gets
135
139
  const requires = getDirectivesByName('requires', fieldNode).map(this.extractFieldSet);
136
140
  const requiredFields = this.translateFieldSet((0, merge_js_1.default)({}, ...requires), parentTypeSignature);
137
141
  // @key() @key() - "primary keys" in Federation
138
- const primaryKeys = keys.map(def => {
142
+ const primaryKeys = resolvableKeyDirectives.map(def => {
139
143
  const fields = this.extractFieldSet(def);
140
144
  return this.translateFieldSet(fields, parentTypeSignature);
141
145
  });
@@ -212,14 +216,27 @@ exports.ApolloFederation = ApolloFederation;
212
216
  * Checks if Object Type is involved in Federation. Based on `@key` directive
213
217
  * @param node Type
214
218
  */
215
- function isFederationObjectType(node, schema) {
219
+ function checkObjectTypeFederationDetails(node, schema) {
216
220
  const { name: { value: name }, directives, } = (0, graphql_1.isObjectType)(node) ? (0, utils_1.astFromObjectType)(node, schema) : node;
217
221
  const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
218
222
  const isNotRoot = !rootTypeNames.has(name);
219
223
  const isNotIntrospection = !name.startsWith('__');
220
- const hasKeyDirective = directives.some(d => d.name.value === 'key');
221
- return isNotRoot && isNotIntrospection && hasKeyDirective;
224
+ const keyDirectives = directives.filter(d => d.name.value === 'key');
225
+ const check = isNotRoot && isNotIntrospection && keyDirectives.length > 0;
226
+ if (!check) {
227
+ return false;
228
+ }
229
+ const resolvableKeyDirectives = keyDirectives.filter(d => {
230
+ for (const arg of d.arguments) {
231
+ if (arg.name.value === 'resolvable' && arg.value.kind === 'BooleanValue' && arg.value.value === false) {
232
+ return false;
233
+ }
234
+ }
235
+ return true;
236
+ });
237
+ return { resolvableKeyDirectives };
222
238
  }
239
+ exports.checkObjectTypeFederationDetails = checkObjectTypeFederationDetails;
223
240
  /**
224
241
  * Extracts directives from a node based on directive's name
225
242
  * @param name directive name
package/esm/federation.js CHANGED
@@ -21,17 +21,18 @@ export const federationSpec = parse(/* GraphQL */ `
21
21
  export function addFederationReferencesToSchema(schema) {
22
22
  return mapSchema(schema, {
23
23
  [MapperKind.OBJECT_TYPE]: type => {
24
- if (isFederationObjectType(type, schema)) {
25
- const typeConfig = type.toConfig();
26
- typeConfig.fields = {
27
- [resolveReferenceFieldName]: {
28
- type,
29
- },
30
- ...typeConfig.fields,
31
- };
32
- return new GraphQLObjectType(typeConfig);
24
+ const objectTypeFederationDetails = checkObjectTypeFederationDetails(type, schema);
25
+ if (!objectTypeFederationDetails || objectTypeFederationDetails.resolvableKeyDirectives.length === 0) {
26
+ return type;
33
27
  }
34
- return type;
28
+ const typeConfig = type.toConfig();
29
+ typeConfig.fields = {
30
+ [resolveReferenceFieldName]: {
31
+ type,
32
+ },
33
+ ...typeConfig.fields,
34
+ };
35
+ return new GraphQLObjectType(typeConfig);
35
36
  },
36
37
  });
37
38
  }
@@ -104,7 +105,7 @@ export class ApolloFederation {
104
105
  * @param data
105
106
  */
106
107
  skipField({ fieldNode, parentType }) {
107
- if (!this.enabled || !isObjectType(parentType) || !isFederationObjectType(parentType, this.schema)) {
108
+ if (!this.enabled || !isObjectType(parentType) || !checkObjectTypeFederationDetails(parentType, this.schema)) {
108
109
  return false;
109
110
  }
110
111
  return this.isExternalAndNotProvided(fieldNode, parentType);
@@ -120,16 +121,19 @@ export class ApolloFederation {
120
121
  transformParentType({ fieldNode, parentType, parentTypeSignature, }) {
121
122
  if (this.enabled &&
122
123
  isObjectType(parentType) &&
123
- isFederationObjectType(parentType, this.schema) &&
124
124
  (isTypeExtension(parentType, this.schema) || fieldNode.name.value === resolveReferenceFieldName)) {
125
- const keys = getDirectivesByName('key', parentType);
126
- if (keys.length) {
125
+ const objectTypeFederationDetails = checkObjectTypeFederationDetails(parentType, this.schema);
126
+ if (!objectTypeFederationDetails) {
127
+ return parentTypeSignature;
128
+ }
129
+ const { resolvableKeyDirectives } = objectTypeFederationDetails;
130
+ if (resolvableKeyDirectives.length) {
127
131
  const outputs = [`{ __typename: '${parentType.name}' } &`];
128
132
  // Look for @requires and see what the service needs and gets
129
133
  const requires = getDirectivesByName('requires', fieldNode).map(this.extractFieldSet);
130
134
  const requiredFields = this.translateFieldSet(merge({}, ...requires), parentTypeSignature);
131
135
  // @key() @key() - "primary keys" in Federation
132
- const primaryKeys = keys.map(def => {
136
+ const primaryKeys = resolvableKeyDirectives.map(def => {
133
137
  const fields = this.extractFieldSet(def);
134
138
  return this.translateFieldSet(fields, parentTypeSignature);
135
139
  });
@@ -205,13 +209,25 @@ export class ApolloFederation {
205
209
  * Checks if Object Type is involved in Federation. Based on `@key` directive
206
210
  * @param node Type
207
211
  */
208
- function isFederationObjectType(node, schema) {
212
+ export function checkObjectTypeFederationDetails(node, schema) {
209
213
  const { name: { value: name }, directives, } = isObjectType(node) ? astFromObjectType(node, schema) : node;
210
214
  const rootTypeNames = getRootTypeNames(schema);
211
215
  const isNotRoot = !rootTypeNames.has(name);
212
216
  const isNotIntrospection = !name.startsWith('__');
213
- const hasKeyDirective = directives.some(d => d.name.value === 'key');
214
- return isNotRoot && isNotIntrospection && hasKeyDirective;
217
+ const keyDirectives = directives.filter(d => d.name.value === 'key');
218
+ const check = isNotRoot && isNotIntrospection && keyDirectives.length > 0;
219
+ if (!check) {
220
+ return false;
221
+ }
222
+ const resolvableKeyDirectives = keyDirectives.filter(d => {
223
+ for (const arg of d.arguments) {
224
+ if (arg.name.value === 'resolvable' && arg.value.kind === 'BooleanValue' && arg.value.value === false) {
225
+ return false;
226
+ }
227
+ }
228
+ return true;
229
+ });
230
+ return { resolvableKeyDirectives };
215
231
  }
216
232
  /**
217
233
  * Extracts directives from a node based on directive's name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/plugin-helpers",
3
- "version": "5.0.4",
3
+ "version": "5.1.0-alpha-20240606121041-9e14ec7dd5cde0aa2beb9e8e9f6bd4ccb65650b9",
4
4
  "description": "GraphQL Code Generator common utils and types",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
@@ -1,4 +1,4 @@
1
- import { FieldDefinitionNode, GraphQLNamedType, GraphQLSchema } from 'graphql';
1
+ import { type ConstDirectiveNode, FieldDefinitionNode, GraphQLNamedType, GraphQLObjectType, GraphQLSchema, ObjectTypeDefinitionNode } from 'graphql';
2
2
  /**
3
3
  * Federation Spec
4
4
  */
@@ -67,3 +67,10 @@ export declare class ApolloFederation {
67
67
  private extractFieldSet;
68
68
  private createMapOfProvides;
69
69
  }
70
+ /**
71
+ * Checks if Object Type is involved in Federation. Based on `@key` directive
72
+ * @param node Type
73
+ */
74
+ export declare function checkObjectTypeFederationDetails(node: ObjectTypeDefinitionNode | GraphQLObjectType, schema: GraphQLSchema): {
75
+ resolvableKeyDirectives: readonly ConstDirectiveNode[];
76
+ } | false;
@@ -1,4 +1,4 @@
1
- import { FieldDefinitionNode, GraphQLNamedType, GraphQLSchema } from 'graphql';
1
+ import { type ConstDirectiveNode, FieldDefinitionNode, GraphQLNamedType, GraphQLObjectType, GraphQLSchema, ObjectTypeDefinitionNode } from 'graphql';
2
2
  /**
3
3
  * Federation Spec
4
4
  */
@@ -67,3 +67,10 @@ export declare class ApolloFederation {
67
67
  private extractFieldSet;
68
68
  private createMapOfProvides;
69
69
  }
70
+ /**
71
+ * Checks if Object Type is involved in Federation. Based on `@key` directive
72
+ * @param node Type
73
+ */
74
+ export declare function checkObjectTypeFederationDetails(node: ObjectTypeDefinitionNode | GraphQLObjectType, schema: GraphQLSchema): {
75
+ resolvableKeyDirectives: readonly ConstDirectiveNode[];
76
+ } | false;