-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
federationToStitchingSDL.ts
166 lines (145 loc) · 5.85 KB
/
federationToStitchingSDL.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Taken from https://github.com/gmac/federation-to-stitching-sdl/blob/main/index.js
import {
DefinitionNode,
DirectiveNode,
InterfaceTypeDefinitionNode,
InterfaceTypeExtensionNode,
Kind,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
parse,
print,
SchemaDefinitionNode,
} from 'graphql';
import { stitchingDirectives, StitchingDirectivesResult } from './stitchingDirectives.js';
const extensionKind = /Extension$/;
type EntityKind =
| ObjectTypeDefinitionNode
| ObjectTypeExtensionNode
| InterfaceTypeDefinitionNode
| InterfaceTypeExtensionNode;
const entityKinds: (typeof Kind)[keyof typeof Kind][] = [
Kind.OBJECT_TYPE_DEFINITION,
Kind.OBJECT_TYPE_EXTENSION,
Kind.INTERFACE_TYPE_DEFINITION,
Kind.INTERFACE_TYPE_EXTENSION,
];
function isEntityKind(def: DefinitionNode): def is EntityKind {
return entityKinds.includes(def.kind);
}
function getQueryTypeDef(
definitions: readonly DefinitionNode[],
): ObjectTypeDefinitionNode | undefined {
const schemaDef = definitions.find(
def => def.kind === Kind.SCHEMA_DEFINITION,
) as SchemaDefinitionNode;
const typeName = schemaDef
? schemaDef.operationTypes.find(({ operation }) => operation === 'query')?.type.name.value
: 'Query';
return definitions.find(
def => def.kind === Kind.OBJECT_TYPE_DEFINITION && def.name.value === typeName,
) as ObjectTypeDefinitionNode;
}
// Federation services are actually fairly complex,
// as the `buildFederatedSchema` helper does a fair amount
// of hidden work to setup the Federation schema specification:
// https://www.apollographql.com/docs/federation/federation-spec/#federation-schema-specification
export function federationToStitchingSDL(
federationSDL: string,
stitchingConfig: StitchingDirectivesResult = stitchingDirectives(),
): string {
const doc = parse(federationSDL);
const entityTypes: string[] = [];
const baseTypeNames = doc.definitions.reduce((memo, typeDef) => {
if (!extensionKind.test(typeDef.kind) && 'name' in typeDef && typeDef.name) {
memo[typeDef.name.value] = true;
}
return memo;
}, {});
doc.definitions.forEach(typeDef => {
// Un-extend all types (remove "extends" keywords)...
// extended types are invalid GraphQL without a local base type to extend from.
// Stitching merges flat types in lieu of hierarchical extensions.
if (
extensionKind.test(typeDef.kind) &&
'name' in typeDef &&
typeDef.name &&
!baseTypeNames[typeDef.name.value]
) {
(typeDef.kind as string) = typeDef.kind.replace(extensionKind, 'Definition');
}
if (!isEntityKind(typeDef)) return;
// Find object definitions with "@key" directives;
// these are federated entities that get turned into merged types.
const keyDirs: DirectiveNode[] = [];
const otherDirs: DirectiveNode[] = [];
typeDef.directives?.forEach(dir => {
if (dir.name.value === 'key') {
keyDirs.push(dir);
} else {
otherDirs.push(dir);
}
});
if (!keyDirs.length) return;
// Setup stitching MergedTypeConfig for all federated entities:
const selectionSet = `{ ${keyDirs.map((dir: any) => dir.arguments[0].value.value).join(' ')} }`;
const keyFields = (parse(selectionSet).definitions[0] as any).selectionSet.selections.map(
(sel: any) => sel.name.value,
);
const keyDir = keyDirs[0];
(keyDir.name.value as string) = stitchingConfig.keyDirective.name;
(keyDir.arguments as any)[0].name.value = 'selectionSet';
(keyDir.arguments as any)[0].value.value = selectionSet;
(typeDef.directives as any[]) = [keyDir, ...otherDirs];
// Remove non-key "@external" fields from the type...
// the stitching query planner expects services to only publish their own fields.
// This makes "@provides" moot because the query planner can automate the logic.
(typeDef.fields as any) = typeDef.fields?.filter(fieldDef => {
return (
keyFields.includes(fieldDef.name.value) ||
!fieldDef.directives?.find(dir => dir.name.value === 'external')
);
});
// Discard remaining "@external" directives and any "@provides" directives
typeDef.fields?.forEach((fieldDef: any) => {
fieldDef.directives = fieldDef.directives.filter(
(dir: any) => !/^(external|provides)$/.test(dir.name.value),
);
fieldDef.directives.forEach((dir: any) => {
if (dir.name.value === 'requires') {
dir.name.value = stitchingConfig.computedDirective.name;
dir.arguments[0].name.value = 'selectionSet';
dir.arguments[0].value.value = `{ ${dir.arguments[0].value.value} }`;
}
});
});
if (
typeDef.kind === Kind.OBJECT_TYPE_DEFINITION ||
typeDef.kind === Kind.OBJECT_TYPE_EXTENSION
) {
entityTypes.push(typeDef.name.value);
}
});
// Federation service SDLs are incomplete because they omit the federation spec itself...
// (https://www.apollographql.com/docs/federation/federation-spec/#federation-schema-specification)
// To make federation SDLs into valid and parsable GraphQL schemas,
// we must fill in the missing details from the specification.
if (entityTypes.length) {
const queryDef = getQueryTypeDef(doc.definitions);
const entitiesSchema = parse(/* GraphQL */ `
scalar _Any
union _Entity = ${entityTypes.filter((v, i, a) => a.indexOf(v) === i).join(' | ')}
type Query { _entities(representations: [_Any!]!): [_Entity]! @${
stitchingConfig.mergeDirective.name
} }
`).definitions as unknown as DefinitionNode & { fields: any[] };
(doc.definitions as any).push(entitiesSchema[0]);
(doc.definitions as any).push(entitiesSchema[1]);
if (queryDef) {
(queryDef.fields as any).push(entitiesSchema[2].fields[0]);
} else {
(doc.definitions as any).push(entitiesSchema[2]);
}
}
return [stitchingConfig.stitchingDirectivesTypeDefs, print(doc)].join('\n');
}