@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
40 lines (39 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.backfillDefaultValues = backfillDefaultValues;
const graphql_1 = require("graphql");
/**
* graphql v17 exposes argument and input field defaults through the new
* `default` property and deprecates `defaultValue`. Schema elements created by
* packages that still rely on `defaultValue` (e.g. `@apollo/subgraph`) end up
* with `default` left undefined, which makes `printSchemaWithDirectives` drop
* the default from the printed SDL.
*
* Backfilling `default` from `defaultValue` keeps those defaults in the SDL. It
* is a no-op on graphql v16, where `default` does not exist.
*/
function backfillDefaultValues(schema) {
for (const directive of schema.getDirectives()) {
directive.args.forEach(backfillDefaultValue);
}
for (const type of Object.values(schema.getTypeMap())) {
if ((0, graphql_1.isObjectType)(type) || (0, graphql_1.isInterfaceType)(type)) {
for (const field of Object.values(type.getFields())) {
field.args.forEach(backfillDefaultValue);
}
}
else if ((0, graphql_1.isInputObjectType)(type)) {
Object.values(type.getFields()).forEach(backfillDefaultValue);
}
}
return schema;
}
function backfillDefaultValue(element) {
const holder = element;
if (!('default' in holder) || holder.default !== undefined) {
return;
}
if (holder.defaultValue !== undefined) {
holder.default = { value: holder.defaultValue };
}
}