@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
113 lines (110 loc) • 4.49 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.composeServices = composeServices;
exports.assertCompositionSuccess = assertCompositionSuccess;
exports.assertCompositionFailure = assertCompositionFailure;
exports.compositionHasErrors = compositionHasErrors;
const graphql_1 = require("graphql");
const printer_js_1 = require("./graphql/printer.js");
const transform_supergraph_to_public_schema_js_1 = require("./graphql/transform-supergraph-to-public-schema.js");
const authenticated_js_1 = require("./specifications/authenticated.js");
const inaccessible_js_1 = require("./specifications/inaccessible.js");
const join_js_1 = require("./specifications/join.js");
const link_js_1 = require("./specifications/link.js");
const policy_js_1 = require("./specifications/policy.js");
const requires_scopes_js_1 = require("./specifications/requires-scopes.js");
const tag_js_1 = require("./specifications/tag.js");
const validate_js_1 = require("./validate.js");
function composeServices(services, __internal) {
const validationResult = (0, validate_js_1.validate)(services, __internal);
if (!validationResult.success) {
return {
errors: validationResult.errors,
};
}
const rootTypes = {
query: false,
mutation: false,
subscription: false,
};
for (const def of validationResult.supergraph) {
if (def.name.value === 'Query') {
rootTypes.query = true;
}
else if (def.name.value === 'Mutation') {
rootTypes.mutation = true;
}
else if (def.name.value === 'Subscription') {
rootTypes.subscription = true;
}
if (rootTypes.query === true &&
rootTypes.mutation === true &&
rootTypes.subscription === true) {
break;
}
}
const usedTagSpec = validationResult.specs.tag;
const usedInaccessibleSpec = validationResult.specs.inaccessible;
const usedPolicySpec = validationResult.specs.policy;
const usedRequiresScopesSpec = validationResult.specs.requiresScopes;
const usedAuthenticatedSpec = validationResult.specs.authenticated;
let _publicSdl;
return {
supergraphSdl: `
schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(url: "https://specs.apollo.dev/join/v0.3", for: EXECUTION)
${usedTagSpec ? '@link(url: "https://specs.apollo.dev/tag/v0.3")' : ''}
${usedInaccessibleSpec
? '@link(url: "https://specs.apollo.dev/inaccessible/v0.2", for: SECURITY)'
: ''}
${usedPolicySpec ? '@link(url: "https://specs.apollo.dev/policy/v0.1", for: SECURITY)' : ''}
${usedRequiresScopesSpec
? '@link(url: "https://specs.apollo.dev/requiresScopes/v0.1", for: SECURITY)'
: ''}
${usedAuthenticatedSpec
? '@link(url: "https://specs.apollo.dev/authenticated/v0.1", for: SECURITY)'
: ''}
${validationResult.links.map(link_js_1.printLink).join('\n ')}
{
${rootTypes.query ? 'query: Query' : ''}
${rootTypes.mutation ? 'mutation: Mutation' : ''}
${rootTypes.subscription ? 'subscription: Subscription' : ''}
}
${join_js_1.sdl}
${link_js_1.sdl}
${usedTagSpec ? tag_js_1.sdl : ''}
${usedInaccessibleSpec ? inaccessible_js_1.sdl : ''}
${usedPolicySpec ? policy_js_1.sdl : ''}
${usedRequiresScopesSpec ? requires_scopes_js_1.sdl : ''}
${usedAuthenticatedSpec ? authenticated_js_1.sdl : ''}
${(0, printer_js_1.print)({
kind: graphql_1.Kind.DOCUMENT,
definitions: validationResult.supergraph,
})}
`,
get publicSdl() {
if (_publicSdl) {
return _publicSdl;
}
_publicSdl = (0, printer_js_1.print)((0, transform_supergraph_to_public_schema_js_1.transformSupergraphToPublicSchema)({
kind: graphql_1.Kind.DOCUMENT,
definitions: validationResult.supergraph,
}));
return _publicSdl;
},
};
}
function assertCompositionSuccess(compositionResult, message) {
if (compositionHasErrors(compositionResult)) {
throw new Error(message || 'Unexpected test failure');
}
}
function assertCompositionFailure(compositionResult, message) {
if (!compositionHasErrors(compositionResult)) {
throw new Error(message || 'Unexpected test failure');
}
}
function compositionHasErrors(compositionResult) {
return 'errors' in compositionResult && !!compositionResult.errors;
}
;