Skip to content

Commit bfa5db2

Browse files
Add generatorMode to Options type, pass an appropriate value per node type
So, when we have a normal FieldDefinition, we want to assume we want to use the output generator. When we visit the InputObjectTypeDefinition and call generateMockValue on its children, we can assume we want the input generator. We keep the other types of generator config types around for backwards compatibility. It could be a nice refactoring opportunity to map the existing config types to the input/output type, instead of what we do in this commit, which is check/switch on `value` in `getGeneratorDefinition`, but I wanted to prove to myself that we could generate distinct mocks.
1 parent 7b9e000 commit bfa5db2

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/index.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string;
1212
type Options<T = TypeNode> = {
1313
typeName: string;
1414
fieldName: string;
15+
generatorMode: 'input' | 'output';
1516
types: TypeItem[];
1617
typeNamesConvention: NamingConvention;
1718
enumValuesConvention: NamingConvention;
@@ -90,13 +91,21 @@ const hashedString = (value: string) => {
9091
return hash;
9192
};
9293

93-
const getGeneratorDefinition = (value: GeneratorDefinition | GeneratorName): GeneratorDefinition => {
94+
const getGeneratorDefinition = (
95+
value: GeneratorOptions | InputOutputGeneratorOptions,
96+
generatorMode: Options['generatorMode'],
97+
): GeneratorDefinition => {
9498
if (typeof value === 'string') {
9599
return {
96100
generator: value,
97101
arguments: [],
98102
};
99103
}
104+
105+
if ('input' in value && 'output' in value) {
106+
return getGeneratorDefinition(value[generatorMode], generatorMode);
107+
}
108+
100109
return value;
101110
};
102111

@@ -246,12 +255,18 @@ const handleValueGeneration = (
246255
if (opts.fieldGeneration) {
247256
// Check for a specific generation for the type & field
248257
if (opts.typeName in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration[opts.typeName]) {
249-
const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration[opts.typeName][opts.fieldName]);
258+
const generatorDefinition = getGeneratorDefinition(
259+
opts.fieldGeneration[opts.typeName][opts.fieldName],
260+
opts.generatorMode,
261+
);
250262
return getCustomValue(generatorDefinition, opts);
251263
}
252264
// Check for a general field generation definition
253265
if ('_all' in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration['_all']) {
254-
const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration['_all'][opts.fieldName]);
266+
const generatorDefinition = getGeneratorDefinition(
267+
opts.fieldGeneration['_all'][opts.fieldName],
268+
opts.generatorMode,
269+
);
255270
return getCustomValue(generatorDefinition, opts);
256271
}
257272
}
@@ -290,25 +305,36 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
290305
if (!opts.dynamicValues) mockValueGenerator.seed(hashedString(opts.typeName + opts.fieldName));
291306
const name = opts.currentType.name.value;
292307
const casedName = createNameConverter(opts.typeNamesConvention, opts.transformUnderscore)(name);
308+
293309
switch (name) {
294310
case 'String': {
295-
const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['String']) : null;
311+
const customScalar = opts.customScalars
312+
? getGeneratorDefinition(opts.customScalars['String'], opts.generatorMode)
313+
: null;
296314
return handleValueGeneration(opts, customScalar, mockValueGenerator.word);
297315
}
298316
case 'Float': {
299-
const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Float']) : null;
317+
const customScalar = opts.customScalars
318+
? getGeneratorDefinition(opts.customScalars['Float'], opts.generatorMode)
319+
: null;
300320
return handleValueGeneration(opts, customScalar, mockValueGenerator.float);
301321
}
302322
case 'ID': {
303-
const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['ID']) : null;
323+
const customScalar = opts.customScalars
324+
? getGeneratorDefinition(opts.customScalars['ID'], opts.generatorMode)
325+
: null;
304326
return handleValueGeneration(opts, customScalar, mockValueGenerator.uuid);
305327
}
306328
case 'Boolean': {
307-
const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Boolean']) : null;
329+
const customScalar = opts.customScalars
330+
? getGeneratorDefinition(opts.customScalars['Boolean'], opts.generatorMode)
331+
: null;
308332
return handleValueGeneration(opts, customScalar, mockValueGenerator.boolean);
309333
}
310334
case 'Int': {
311-
const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Int']) : null;
335+
const customScalar = opts.customScalars
336+
? getGeneratorDefinition(opts.customScalars['Int'], opts.generatorMode)
337+
: null;
312338
return handleValueGeneration(opts, customScalar, mockValueGenerator.integer);
313339
}
314340
default: {
@@ -345,7 +371,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
345371
});
346372
case 'scalar': {
347373
const customScalar = opts.customScalars
348-
? getGeneratorDefinition(opts.customScalars[foundType.name])
374+
? getGeneratorDefinition(opts.customScalars[foundType.name], opts.generatorMode)
349375
: null;
350376

351377
// it's a scalar, let's use a string as a value if there is no custom
@@ -559,9 +585,13 @@ type GeneratorDefinition = {
559585
};
560586
};
561587
type GeneratorOptions = GeneratorName | GeneratorDefinition;
588+
type InputOutputGeneratorOptions = {
589+
input: GeneratorOptions;
590+
output: GeneratorOptions;
591+
};
562592

563593
type ScalarMap = {
564-
[name: string]: GeneratorOptions;
594+
[name: string]: GeneratorOptions | InputOutputGeneratorOptions;
565595
};
566596

567597
type TypeFieldMap = {
@@ -728,6 +758,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
728758
const value = generateMockValue({
729759
typeName,
730760
fieldName,
761+
generatorMode: 'output',
731762
currentType: node.type,
732763
...sharedGenerateMockOpts,
733764
});
@@ -753,6 +784,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
753784
typeName: fieldName,
754785
fieldName: field.name.value,
755786
currentType: field.type,
787+
generatorMode: 'input',
756788
...sharedGenerateMockOpts,
757789
});
758790

@@ -764,6 +796,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
764796
typeName: fieldName,
765797
fieldName: field.name.value,
766798
currentType: field.type,
799+
generatorMode: 'input',
767800
...sharedGenerateMockOpts,
768801
});
769802

0 commit comments

Comments
 (0)