@nmarks/graphql-codegen-per-operation-file-preset 1.0.2 → 1.0.3
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 +29 -7
- package/esm/index.js +29 -7
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -27,14 +27,17 @@ 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
|
|
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 || {};
|
|
38
41
|
// Collect all type names referenced in the document
|
|
39
42
|
const referencedTypeNames = new Set();
|
|
40
43
|
(0, graphql_1.visit)(document, {
|
|
@@ -47,8 +50,6 @@ function needsSchemaTypesImport(document, schema) {
|
|
|
47
50
|
FragmentDefinition: (node) => {
|
|
48
51
|
referencedTypeNames.add(node.typeCondition.name.value);
|
|
49
52
|
},
|
|
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
53
|
});
|
|
53
54
|
// Helper to recursively check if a type needs importing
|
|
54
55
|
const checkType = (type) => {
|
|
@@ -58,12 +59,33 @@ function needsSchemaTypesImport(document, schema) {
|
|
|
58
59
|
while ('ofType' in type && type.ofType) {
|
|
59
60
|
type = type.ofType;
|
|
60
61
|
}
|
|
61
|
-
// Enums need to be imported
|
|
62
|
+
// Enums always need to be imported
|
|
62
63
|
if ((0, graphql_1.isEnumType)(type)) {
|
|
63
64
|
return true;
|
|
64
65
|
}
|
|
65
|
-
// Custom scalars
|
|
66
|
+
// Custom scalars need to be imported UNLESS mapped to primitives
|
|
66
67
|
if ((0, graphql_1.isScalarType)(type) && !builtInScalars.includes(type.name)) {
|
|
68
|
+
// Check if this scalar is mapped to a primitive type
|
|
69
|
+
const mapping = scalarMappings[type.name];
|
|
70
|
+
if (mapping) {
|
|
71
|
+
// If mapped to a primitive type string, no import needed
|
|
72
|
+
if (typeof mapping === 'string' && primitiveTypes.includes(mapping)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
// If mapped to an object like { input: 'any', output: 'any' }, check both
|
|
76
|
+
if (typeof mapping === 'object') {
|
|
77
|
+
const inputMapping = mapping.input || mapping.output;
|
|
78
|
+
const outputMapping = mapping.output || mapping.input;
|
|
79
|
+
if (primitiveTypes.includes(inputMapping) && primitiveTypes.includes(outputMapping)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// No mapping specified, defaults to 'any', so no import needed
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
// Scalar is mapped to something non-primitive, needs import
|
|
67
89
|
return true;
|
|
68
90
|
}
|
|
69
91
|
// Input types need to be imported
|
|
@@ -164,7 +186,7 @@ exports.preset = {
|
|
|
164
186
|
...source.externalFragments.map(fragment => fragment.node),
|
|
165
187
|
],
|
|
166
188
|
};
|
|
167
|
-
const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject);
|
|
189
|
+
const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
|
|
168
190
|
// Generate the types import statement if needed
|
|
169
191
|
const importStatements = [];
|
|
170
192
|
if (needsTypesImport && !options.config.globalNamespace) {
|
package/esm/index.js
CHANGED
|
@@ -23,14 +23,17 @@ 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
|
|
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 || {};
|
|
34
37
|
// Collect all type names referenced in the document
|
|
35
38
|
const referencedTypeNames = new Set();
|
|
36
39
|
visit(document, {
|
|
@@ -43,8 +46,6 @@ function needsSchemaTypesImport(document, schema) {
|
|
|
43
46
|
FragmentDefinition: (node) => {
|
|
44
47
|
referencedTypeNames.add(node.typeCondition.name.value);
|
|
45
48
|
},
|
|
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
49
|
});
|
|
49
50
|
// Helper to recursively check if a type needs importing
|
|
50
51
|
const checkType = (type) => {
|
|
@@ -54,12 +55,33 @@ function needsSchemaTypesImport(document, schema) {
|
|
|
54
55
|
while ('ofType' in type && type.ofType) {
|
|
55
56
|
type = type.ofType;
|
|
56
57
|
}
|
|
57
|
-
// Enums need to be imported
|
|
58
|
+
// Enums always need to be imported
|
|
58
59
|
if (isEnumType(type)) {
|
|
59
60
|
return true;
|
|
60
61
|
}
|
|
61
|
-
// Custom scalars
|
|
62
|
+
// Custom scalars need to be imported UNLESS mapped to primitives
|
|
62
63
|
if (isScalarType(type) && !builtInScalars.includes(type.name)) {
|
|
64
|
+
// Check if this scalar is mapped to a primitive type
|
|
65
|
+
const mapping = scalarMappings[type.name];
|
|
66
|
+
if (mapping) {
|
|
67
|
+
// If mapped to a primitive type string, no import needed
|
|
68
|
+
if (typeof mapping === 'string' && primitiveTypes.includes(mapping)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
// If mapped to an object like { input: 'any', output: 'any' }, check both
|
|
72
|
+
if (typeof mapping === 'object') {
|
|
73
|
+
const inputMapping = mapping.input || mapping.output;
|
|
74
|
+
const outputMapping = mapping.output || mapping.input;
|
|
75
|
+
if (primitiveTypes.includes(inputMapping) && primitiveTypes.includes(outputMapping)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// No mapping specified, defaults to 'any', so no import needed
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
// Scalar is mapped to something non-primitive, needs import
|
|
63
85
|
return true;
|
|
64
86
|
}
|
|
65
87
|
// Input types need to be imported
|
|
@@ -160,7 +182,7 @@ export const preset = {
|
|
|
160
182
|
...source.externalFragments.map(fragment => fragment.node),
|
|
161
183
|
],
|
|
162
184
|
};
|
|
163
|
-
const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject);
|
|
185
|
+
const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
|
|
164
186
|
// Generate the types import statement if needed
|
|
165
187
|
const importStatements = [];
|
|
166
188
|
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.
|
|
3
|
+
"version": "1.0.3",
|
|
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"
|