Skip to content

Commit

Permalink
feat(): add default type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Apr 22, 2021
1 parent 353b2b7 commit 95ae089
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions lib/graphql-ast.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export interface DefinitionsGeneratorOptions {
*/
customScalarTypeMapping?: Record<string, string | { name: string }>;

/**
* If provided, specifies a mapping of default scalar types (Int, Boolean, ID, Float, String).
* @default undefined
*/
defaultTypeMapping?: Record<
'ID' | 'Boolean' | 'Float' | 'String' | 'Int',
string
>;

/**
* If provided, specifies a custom header to add after the
* to the output file (eg. for custom type imports or comments)
Expand Down Expand Up @@ -262,7 +271,10 @@ export class GraphQLAstExplorer {
return;
}

const { name: type, required } = this.getFieldTypeDefinition(item.type);
const { name: type, required } = this.getFieldTypeDefinition(
item.type,
options,
);
if (!this.isRoot(parentRef.getName())) {
(parentRef as InterfaceDeclaration).addProperty({
name: propertyName,
Expand All @@ -286,13 +298,15 @@ export class GraphQLAstExplorer {
)} | Promise<${this.addSymbolIfRoot(type)}>`,
parameters: this.getFunctionParameters(
(item as FieldDefinitionNode).arguments,
options,
),
});
}
}

getFieldTypeDefinition(
type: TypeNode,
options: DefinitionsGeneratorOptions,
): {
name: string;
required: boolean;
Expand All @@ -307,13 +321,13 @@ export class GraphQLAstExplorer {

const typeName = get(type, 'name.value');
return {
name: this.getType(typeName) + '[]',
name: this.getType(typeName, options) + '[]',
required,
};
}
const typeName = get(type, 'name.value');
return {
name: this.getType(typeName),
name: this.getType(typeName, options),
required,
};
}
Expand All @@ -334,30 +348,36 @@ export class GraphQLAstExplorer {
return { type, required: false };
}

getType(typeName: string): string {
const defaults = this.getDefaultTypes();
getType(typeName: string, options: DefinitionsGeneratorOptions): string {
const defaults = this.getDefaultTypes(options);
const isDefault = defaults[typeName];
return isDefault ? defaults[typeName] : typeName;
}

getDefaultTypes(): { [type: string]: string } {
getDefaultTypes(
options: DefinitionsGeneratorOptions,
): { [type: string]: string } {
return {
String: 'string',
Int: 'number',
Boolean: 'boolean',
ID: 'string',
Float: 'number',
String: options.defaultTypeMapping?.String ?? 'string',
Int: options.defaultTypeMapping?.Int ?? 'number',
Boolean: options.defaultTypeMapping?.Boolean ?? 'boolean',
ID: options.defaultTypeMapping?.ID ?? 'string',
Float: options.defaultTypeMapping?.Float ?? 'number',
};
}

getFunctionParameters(
inputs: ReadonlyArray<InputValueDefinitionNode>,
options: DefinitionsGeneratorOptions,
): ParameterDeclarationStructure[] {
if (!inputs) {
return [];
}
return inputs.map((element) => {
const { name, required } = this.getFieldTypeDefinition(element.type);
const { name, required } = this.getFieldTypeDefinition(
element.type,
options,
);
return {
name: get(element, 'name.value'),
type: name,
Expand Down

0 comments on commit 95ae089

Please sign in to comment.