@nmarks/graphql-codegen-per-operation-file-preset 1.0.2 → 1.0.4

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/index.js CHANGED
@@ -27,19 +27,25 @@ function extractDefinitions(document) {
27
27
  * Check if a document actually needs type imports from the schema.
28
28
  * Only returns true if the document uses:
29
29
  * - Enums
30
- * - Custom scalars (not built-in)
30
+ * - Custom scalars that aren't mapped to primitives
31
31
  * - Input types (in variables)
32
32
  *
33
33
  * Does NOT return true for just referencing object types.
34
34
  */
35
- function needsSchemaTypesImport(document, schema) {
35
+ function needsSchemaTypesImport(document, schema, config) {
36
36
  const builtInScalars = ['String', 'Int', 'Float', 'Boolean', 'ID'];
37
+ const primitiveTypes = ['any', 'string', 'number', 'boolean', 'null', 'undefined', 'void', 'never', 'unknown'];
37
38
  let needsImport = false;
39
+ // Get scalar mappings from config
40
+ const scalarMappings = config.scalars || {};
41
+ // Check if document has any variables - if so, we need Types import for Types.Exact, Types.Scalars, etc.
42
+ let hasVariables = false;
38
43
  // Collect all type names referenced in the document
39
44
  const referencedTypeNames = new Set();
40
45
  (0, graphql_1.visit)(document, {
41
46
  // Check variable types for input types or custom scalars
42
47
  VariableDefinition: (node) => {
48
+ hasVariables = true; // Any variable means we need Types import
43
49
  const typeName = getBaseTypeName(node.type);
44
50
  referencedTypeNames.add(typeName);
45
51
  },
@@ -47,9 +53,11 @@ function needsSchemaTypesImport(document, schema) {
47
53
  FragmentDefinition: (node) => {
48
54
  referencedTypeNames.add(node.typeCondition.name.value);
49
55
  },
50
- // We need to traverse into nested fields, so we'll collect all types
51
- // This is a simplified approach - we'll check all types that might be used
52
56
  });
57
+ // If operation has variables, it will use Types.Exact, Types.Scalars, Types.InputMaybe, etc.
58
+ if (hasVariables) {
59
+ return true;
60
+ }
53
61
  // Helper to recursively check if a type needs importing
54
62
  const checkType = (type) => {
55
63
  if (!type)
@@ -58,12 +66,33 @@ function needsSchemaTypesImport(document, schema) {
58
66
  while ('ofType' in type && type.ofType) {
59
67
  type = type.ofType;
60
68
  }
61
- // Enums need to be imported
69
+ // Enums always need to be imported
62
70
  if ((0, graphql_1.isEnumType)(type)) {
63
71
  return true;
64
72
  }
65
- // Custom scalars (not built-in) need to be imported
73
+ // Custom scalars need to be imported UNLESS mapped to primitives
66
74
  if ((0, graphql_1.isScalarType)(type) && !builtInScalars.includes(type.name)) {
75
+ // Check if this scalar is mapped to a primitive type
76
+ const mapping = scalarMappings[type.name];
77
+ if (mapping) {
78
+ // If mapped to a primitive type string, no import needed
79
+ if (typeof mapping === 'string' && primitiveTypes.includes(mapping)) {
80
+ return false;
81
+ }
82
+ // If mapped to an object like { input: 'any', output: 'any' }, check both
83
+ if (typeof mapping === 'object') {
84
+ const inputMapping = mapping.input || mapping.output;
85
+ const outputMapping = mapping.output || mapping.input;
86
+ if (primitiveTypes.includes(inputMapping) && primitiveTypes.includes(outputMapping)) {
87
+ return false;
88
+ }
89
+ }
90
+ }
91
+ else {
92
+ // No mapping specified, defaults to 'any', so no import needed
93
+ return false;
94
+ }
95
+ // Scalar is mapped to something non-primitive, needs import
67
96
  return true;
68
97
  }
69
98
  // Input types need to be imported
@@ -164,7 +193,7 @@ exports.preset = {
164
193
  ...source.externalFragments.map(fragment => fragment.node),
165
194
  ],
166
195
  };
167
- const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject);
196
+ const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
168
197
  // Generate the types import statement if needed
169
198
  const importStatements = [];
170
199
  if (needsTypesImport && !options.config.globalNamespace) {
package/esm/index.js CHANGED
@@ -23,19 +23,25 @@ function extractDefinitions(document) {
23
23
  * Check if a document actually needs type imports from the schema.
24
24
  * Only returns true if the document uses:
25
25
  * - Enums
26
- * - Custom scalars (not built-in)
26
+ * - Custom scalars that aren't mapped to primitives
27
27
  * - Input types (in variables)
28
28
  *
29
29
  * Does NOT return true for just referencing object types.
30
30
  */
31
- function needsSchemaTypesImport(document, schema) {
31
+ function needsSchemaTypesImport(document, schema, config) {
32
32
  const builtInScalars = ['String', 'Int', 'Float', 'Boolean', 'ID'];
33
+ const primitiveTypes = ['any', 'string', 'number', 'boolean', 'null', 'undefined', 'void', 'never', 'unknown'];
33
34
  let needsImport = false;
35
+ // Get scalar mappings from config
36
+ const scalarMappings = config.scalars || {};
37
+ // Check if document has any variables - if so, we need Types import for Types.Exact, Types.Scalars, etc.
38
+ let hasVariables = false;
34
39
  // Collect all type names referenced in the document
35
40
  const referencedTypeNames = new Set();
36
41
  visit(document, {
37
42
  // Check variable types for input types or custom scalars
38
43
  VariableDefinition: (node) => {
44
+ hasVariables = true; // Any variable means we need Types import
39
45
  const typeName = getBaseTypeName(node.type);
40
46
  referencedTypeNames.add(typeName);
41
47
  },
@@ -43,9 +49,11 @@ function needsSchemaTypesImport(document, schema) {
43
49
  FragmentDefinition: (node) => {
44
50
  referencedTypeNames.add(node.typeCondition.name.value);
45
51
  },
46
- // We need to traverse into nested fields, so we'll collect all types
47
- // This is a simplified approach - we'll check all types that might be used
48
52
  });
53
+ // If operation has variables, it will use Types.Exact, Types.Scalars, Types.InputMaybe, etc.
54
+ if (hasVariables) {
55
+ return true;
56
+ }
49
57
  // Helper to recursively check if a type needs importing
50
58
  const checkType = (type) => {
51
59
  if (!type)
@@ -54,12 +62,33 @@ function needsSchemaTypesImport(document, schema) {
54
62
  while ('ofType' in type && type.ofType) {
55
63
  type = type.ofType;
56
64
  }
57
- // Enums need to be imported
65
+ // Enums always need to be imported
58
66
  if (isEnumType(type)) {
59
67
  return true;
60
68
  }
61
- // Custom scalars (not built-in) need to be imported
69
+ // Custom scalars need to be imported UNLESS mapped to primitives
62
70
  if (isScalarType(type) && !builtInScalars.includes(type.name)) {
71
+ // Check if this scalar is mapped to a primitive type
72
+ const mapping = scalarMappings[type.name];
73
+ if (mapping) {
74
+ // If mapped to a primitive type string, no import needed
75
+ if (typeof mapping === 'string' && primitiveTypes.includes(mapping)) {
76
+ return false;
77
+ }
78
+ // If mapped to an object like { input: 'any', output: 'any' }, check both
79
+ if (typeof mapping === 'object') {
80
+ const inputMapping = mapping.input || mapping.output;
81
+ const outputMapping = mapping.output || mapping.input;
82
+ if (primitiveTypes.includes(inputMapping) && primitiveTypes.includes(outputMapping)) {
83
+ return false;
84
+ }
85
+ }
86
+ }
87
+ else {
88
+ // No mapping specified, defaults to 'any', so no import needed
89
+ return false;
90
+ }
91
+ // Scalar is mapped to something non-primitive, needs import
63
92
  return true;
64
93
  }
65
94
  // Input types need to be imported
@@ -160,7 +189,7 @@ export const preset = {
160
189
  ...source.externalFragments.map(fragment => fragment.node),
161
190
  ],
162
191
  };
163
- const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject);
192
+ const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
164
193
  // Generate the types import statement if needed
165
194
  const importStatements = [];
166
195
  if (needsTypesImport && !options.config.globalNamespace) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmarks/graphql-codegen-per-operation-file-preset",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "GraphQL Code Generator preset for generating one file per operation/fragment",
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"