@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
80 lines (79 loc) • 2.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.scalarTypeBuilder = scalarTypeBuilder;
const ast_js_1 = require("./ast.js");
const common_js_1 = require("./common.js");
function scalarTypeBuilder() {
return {
visitSubgraphState(graph, state, typeName, type) {
const scalarTypeState = getOrCreateScalarType(state, typeName);
type.tags.forEach(tag => scalarTypeState.tags.add(tag));
if (type.inaccessible) {
scalarTypeState.inaccessible = true;
}
if (type.authenticated) {
scalarTypeState.authenticated = true;
}
if (type.policies) {
scalarTypeState.policies.push(...type.policies);
}
if (type.scopes) {
scalarTypeState.scopes.push(...type.scopes);
}
if (type.description && !scalarTypeState.description) {
scalarTypeState.description = type.description;
}
if (type.specifiedBy && !scalarTypeState.specifiedBy) {
scalarTypeState.specifiedBy = type.specifiedBy;
}
type.ast.directives.forEach(directive => {
scalarTypeState.ast.directives.push(directive);
});
scalarTypeState.byGraph.set(graph.id, {
inaccessible: type.inaccessible,
version: graph.version,
});
},
composeSupergraphNode(scalarType) {
return (0, ast_js_1.createScalarTypeNode)({
name: scalarType.name,
tags: Array.from(scalarType.tags),
inaccessible: scalarType.inaccessible,
authenticated: scalarType.authenticated,
policies: scalarType.policies,
scopes: scalarType.scopes,
description: scalarType.description,
specifiedBy: scalarType.specifiedBy,
join: {
type: Array.from(scalarType.byGraph.keys()).map(graphName => ({
graph: graphName.toUpperCase(),
})),
},
ast: {
directives: (0, common_js_1.convertToConst)(scalarType.ast.directives),
},
});
},
};
}
function getOrCreateScalarType(state, typeName) {
const existing = state.get(typeName);
if (existing) {
return existing;
}
const def = {
kind: 'scalar',
name: typeName,
tags: new Set(),
inaccessible: false,
authenticated: false,
policies: [],
scopes: [],
byGraph: new Map(),
ast: {
directives: [],
},
};
state.set(typeName, def);
return def;
}
;