@graphql-tools/federation
Version:
Useful tools to create and manipulate GraphQL schemas.
50 lines (49 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNamedTypeNode = exports.filterInternalFieldsAndTypes = exports.getCacheKeyFnFromKey = exports.getKeyForFederation = exports.getArgsFromKeysForFederation = void 0;
const graphql_1 = require("graphql");
const wrap_1 = require("@graphql-tools/wrap");
function getArgsFromKeysForFederation(representations) {
return { representations };
}
exports.getArgsFromKeysForFederation = getArgsFromKeysForFederation;
function getKeyForFederation(root) {
return root;
}
exports.getKeyForFederation = getKeyForFederation;
function getCacheKeyFnFromKey(key) {
const keyProps = key.split(' ');
if (keyProps.length > 1) {
return function cacheKeyFn(root) {
return keyProps.map(key => root[key]).join(' ');
};
}
return function cacheKeyFn(root) {
return root[key];
};
}
exports.getCacheKeyFnFromKey = getCacheKeyFnFromKey;
function filterInternalFieldsAndTypes(finalSchema) {
const removeEntitiesField = new wrap_1.FilterRootFields((operation, fieldName) => !(operation === 'Query' && (fieldName === '_entities' || fieldName === '_sdl')));
const removeEntityAndAny = new wrap_1.FilterTypes(type => type.name !== '_Entity' &&
type.name !== '_Any' &&
type.name !== '_FieldSet' &&
type.name !== '_Service' &&
!type.name.startsWith('link__'));
const fakeSubschemaConfig = {
schema: finalSchema,
transforms: [removeEntitiesField, removeEntityAndAny],
};
for (const transform of fakeSubschemaConfig.transforms) {
finalSchema = transform.transformSchema(finalSchema, fakeSubschemaConfig);
}
return finalSchema;
}
exports.filterInternalFieldsAndTypes = filterInternalFieldsAndTypes;
function getNamedTypeNode(typeNode) {
if (typeNode.kind !== graphql_1.Kind.NAMED_TYPE) {
return getNamedTypeNode(typeNode.type);
}
return typeNode;
}
exports.getNamedTypeNode = getNamedTypeNode;