Skip to content

Commit

Permalink
Alternate solution to #868
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Jul 13, 2021
1 parent ae07eee commit e970261
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
18 changes: 11 additions & 7 deletions federation-js/src/composition/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
import { validateSDL } from 'graphql/validation/validate';
import { compositionRules } from './rules';
import { printSupergraphSdl } from '../service/printSupergraphSdl';
import { mapGetOrSet, mapValues } from '../utilities';
import { mapGetOrSet, mapValues, filterMap } from '../utilities';

const EmptyQueryDefinition = {
kind: Kind.OBJECT_TYPE_DEFINITION,
Expand Down Expand Up @@ -677,11 +677,7 @@ export function addFederationMetadataToSchemaNodes({
}

for (const [typeName, fieldsToDirectivesMap] of typeNameToFieldDirectivesMap.entries()) {
// It's plausible we're dealing with an incomplete schema here which might not
// account for all the types we saw when inspecting SDL. In the case that a
// type is extended with no base definition anywhere, it won't exist in the schema.
const type = schema.getType(typeName) as GraphQLObjectType | undefined;
if (!type) continue;
const type = schema.getType(typeName) as GraphQLObjectType;

for (const [
fieldName,
Expand Down Expand Up @@ -766,14 +762,22 @@ export function composeServices(services: ServiceDefinition[]): CompositionResul

schema = lexicographicSortSchema(schema);

// Now that we have a constructed schema, we can filter this map based on types
// that made it into the schema successfully. Types that didn't make it into the
// schema will have related composition errors.
const filteredTypeNameToFieldDirectivesMap = filterMap(
typeNameToFieldDirectivesMap,
(typeName) => !!schema.getType(typeName),
);

addFederationMetadataToSchemaNodes({
schema,
typeToServiceMap,
externalFields,
keyDirectivesMap,
valueTypes,
directiveDefinitionsMap,
typeNameToFieldDirectivesMap,
typeNameToFieldDirectivesMap: filteredTypeNameToFieldDirectivesMap,
});

if (errors.length > 0) {
Expand Down
14 changes: 14 additions & 0 deletions federation-js/src/utilities/filterMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Filter a Map object (not to be confused with .filter().map())
export function filterMap<K, V>(
map: Map<K, V>,
filterFn: (k: K, v: V) => boolean,
): Map<K, V> {
const filteredMap = new Map<K, V>();

for (const [key, value] of map.entries()) {
if (filterFn(key, value)) {
filteredMap.set(key, value);
}
}
return filteredMap;
}
1 change: 1 addition & 0 deletions federation-js/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { assert } from './assert';
export { isNotNullOrUndefined } from './isNotNullOrUndefined';
export { mapGetOrSet } from './mapGetOrSet';
export { mapValues } from './mapValues';
export { filterMap } from './filterMap';

0 comments on commit e970261

Please sign in to comment.