Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
const enumName = (node.name as any) as string;
// In case of mapped external enum string
if (this.config.enumValues[enumName] && this.config.enumValues[enumName].sourceFile) {
return `export { ${this.config.enumValues[enumName].typeIdentifier} };\n`;
}
const enumTypeName = this.convertName(node, { useTypesPrefix: this.config.enumPrefix });
if (this.config.enumsAsTypes) {
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withComment((node.description as any) as string)
.withName(enumTypeName)
.withContent(
'\n' +
node.values
.map(enumOption => {
let enumValue: string = (enumOption.name as any) as string;
const comment = transformComment((enumOption.description as any) as string, 1);
if (this.config.enumValues[enumName] && this.config.enumValues[enumName].mappedValues && this.config.enumValues[enumName].mappedValues[enumValue]) {
enumValue = this.config.enumValues[enumName].mappedValues[enumValue];
}
return comment + indent(wrapWithSingleQuotes(enumValue));
ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string {
const entityDirective = this._getDirectiveFromAstNode(node, Directives.ENTITY);
if (entityDirective === null) {
return null;
}
const implementingInterfaces = this._buildInterfaces(node.interfaces);
const fields = this._buildFieldsTree(node.fields);
const additionalFields = this._getDirectiveArgValue(entityDirective, 'additionalFields');
this._addAdditionalFields(fields, additionalFields);
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
.withContent(implementingInterfaces.length ? implementingInterfaces.join(' & ') + ' & ' : '')
.withBlock(fields.string).string;
}
}
FragmentDefinition(node: FragmentDefinitionNode): string {
const baseName = node.name.value;
const results = [];
const convertedName = this.convertName(baseName);
const selectionSetTypes = this.buildFragmentBlock(node);
const fragmentBlock = this.printTypes(selectionSetTypes);
if (!this.config.noNamespaces) {
results.push(
new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('namespace')
.withName(convertedName)
.withBlock(fragmentBlock).string
);
} else {
results.push(fragmentBlock);
}
return results.join('\n');
}
node.values
.map(enumOption => {
const comment = transformComment((enumOption.description as any) as string, 1);
const optionName = this.convertName(enumOption, { transformUnderscore: true, useTypesPrefix: false });
let enumValue: string = (enumOption.name as any) as string;
if (this.config.enumValues[typeName] && this.config.enumValues[typeName].mappedValues && this.config.enumValues[typeName].mappedValues[enumValue]) {
enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
}
return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
})
.join(', \n')
).string;
const enumType = new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node, { useTypesPrefix: this.config.enumPrefix }))
.withComment((node.description as any) as string)
.withContent(`$Values`).string;
return [enumValues, enumType].join('\n\n');
}
}
}
return null;
})
.filter(a => a);
if (possibleTypes.length === 0) {
return null;
}
const additionalFields = this._getDirectiveArgValue(unionDirective, 'additionalFields');
const fields = new FieldsTree();
fields.addField(discriminatorField, this.scalars.String);
this._addAdditionalFields(fields, additionalFields);
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
.withContent(`(${possibleTypes.join(' | ')}) & `)
.withBlock(fields.string).string;
}
getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock {
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind(this._parsedConfig.declarationKind.arguments)
.withName(this.convertName(name))
.withComment(node.description)
.withBlock(field.arguments.map(argument => this.InputValueDefinition(argument)).join('\n'));
}
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string {
const nameAsString = (node.name as any) as string;
const baseName = this.getTypeToUse(nameAsString);
this._collectedResolvers[node.name as any] = 'GraphQLScalarType';
return new DeclarationBlock({
...this._declarationBlockConfig,
blockTransformer(block) {
return block;
},
})
.export()
.asKind('type')
.withName(
this.convertName(node, {
suffix: 'ScalarConfig',
})
)
.withBlock([indent(`...GraphQLScalarTypeConfig<${baseName}, any>`), indent(`name: '${node.name}'`)].join(', \n')).string;
}
}
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string {
const abstractEntityDirective = this._getDirectiveFromAstNode(node, Directives.ABSTRACT_ENTITY);
if (abstractEntityDirective === null) {
return null;
}
const discriminatorField = this._getDirectiveArgValue(abstractEntityDirective, 'discriminatorField');
const additionalFields = this._getDirectiveArgValue(abstractEntityDirective, 'additionalFields');
const fields = this._buildFieldsTree(node.fields);
fields.addField(discriminatorField, this.scalars.String);
this._addAdditionalFields(fields, additionalFields);
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node, { suffix: this.config.dbInterfaceSuffix }))
.withBlock(fields.string).string;
}
};
}
if (hooks) {
selectionSetTypes['use' + prefix] = {
export: 'const',
name: 'use' + this.convertName(baseName, { suffix: toPascalCase(node.operation) }),
};
}
}
const operationsBlock = this.printTypes(selectionSetTypes);
if (!this.config.noNamespaces) {
results.push(
new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('namespace')
.withName(convertedName)
.withBlock(operationsBlock).string
);
} else {
results.push(operationsBlock);
}
return results.join('\n');
}
}