UNPKG

@graphql-tools/federation

Version:

Useful tools to create and manipulate GraphQL schemas.

42 lines (41 loc) 1.5 kB
import { Kind } from 'graphql'; import { FilterRootFields, FilterTypes } from '@graphql-tools/wrap'; export function getArgsFromKeysForFederation(representations) { return { representations }; } export function getKeyForFederation(root) { return root; } export 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]; }; } export function filterInternalFieldsAndTypes(finalSchema) { const removeEntitiesField = new FilterRootFields((operation, fieldName) => !(operation === 'Query' && (fieldName === '_entities' || fieldName === '_sdl'))); const removeEntityAndAny = new 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; } export function getNamedTypeNode(typeNode) { if (typeNode.kind !== Kind.NAMED_TYPE) { return getNamedTypeNode(typeNode.type); } return typeNode; }