@vtex/api
Version:
VTEX I/O API client
31 lines (30 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateSchema = void 0;
const constants_1 = require("../../../../../constants");
const utils_1 = require("../../../../../utils");
const index_1 = require("../schema/index");
const updateSchema = (graphql, executableSchema) => async function updateRunnableSchema(ctx, next) {
const { clients: { apps }, vtex: { logger }, app, } = ctx;
if (!ctx.headers[constants_1.HeaderKeys.PROVIDER]) {
await next();
return;
}
// fetches the new schema and generate a new runnable schema, updates the provider app,
if (executableSchema.hasProvider &&
(!executableSchema.provider ||
(0, utils_1.majorEqualAndGreaterThan)((0, utils_1.parseAppId)(ctx.headers[constants_1.HeaderKeys.PROVIDER]).version, (0, utils_1.parseAppId)(executableSchema.provider).version))) {
try {
const newSchema = (await apps.getAppFile(ctx.headers[constants_1.HeaderKeys.PROVIDER], 'public/schema.graphql')).data.toString('utf-8');
graphql.schema = newSchema;
const newRunnableSchema = (0, index_1.makeSchema)(graphql);
executableSchema.schema = newRunnableSchema.schema;
executableSchema.provider = ctx.headers[constants_1.HeaderKeys.PROVIDER];
}
catch (error) {
logger.error({ error, message: 'Update schema failed', app });
}
}
await next();
};
exports.updateSchema = updateSchema;