UNPKG

@graphql-tools/federation

Version:

Useful tools to create and manipulate GraphQL schemas.

74 lines (73 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNamedTypeNode = exports.filterInternalFieldsAndTypes = exports.getCacheKeyFnFromKey = exports.getKeyFnForFederation = exports.getKeyForFederation = exports.getArgsFromKeysForFederation = void 0; const graphql_1 = require("graphql"); const utils_1 = require("@graphql-tools/utils"); function getArgsFromKeysForFederation(representations) { return { representations }; } exports.getArgsFromKeysForFederation = getArgsFromKeysForFederation; function getKeyForFederation(root) { return root; } exports.getKeyForFederation = getKeyForFederation; function getKeyFnForFederation(typeName, keys) { const allKeyProps = keys.flatMap(key => key.split(' ')); if (allKeyProps.length > 1) { const typeNameEntry = ['__typename', typeName]; return function keyFn(root) { return Object.fromEntries(allKeyProps.map(key => [key, root[key]]).concat([typeNameEntry])); }; } const keyProp = allKeyProps[0]; return function keyFn(root) { return { __typename: root['__typename'], [keyProp]: root[keyProp], }; }; } exports.getKeyFnForFederation = getKeyFnForFederation; 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; const internalTypeNames = ['_Entity', '_Any', '_FieldSet', '_Service']; function filterInternalFieldsAndTypes(finalSchema) { return (0, utils_1.mapSchema)(finalSchema, { [utils_1.MapperKind.TYPE]: type => { if (internalTypeNames.includes(type.name) || type.name.startsWith('link__')) { return null; } return type; }, [utils_1.MapperKind.COMPOSITE_FIELD]: fieldConfig => { if (fieldConfig.astNode?.directives?.some(d => d.name.value === 'inaccessible')) { return null; } return fieldConfig; }, [utils_1.MapperKind.QUERY_ROOT_FIELD]: (fieldConfig, fieldName) => { if (fieldName === '_entities') { return null; } return fieldConfig; }, }); } exports.filterInternalFieldsAndTypes = filterInternalFieldsAndTypes; function getNamedTypeNode(typeNode) { if (typeNode.kind !== graphql_1.Kind.NAMED_TYPE) { return getNamedTypeNode(typeNode.type); } return typeNode; } exports.getNamedTypeNode = getNamedTypeNode;