Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[federation] add utility to fetch managed federation supergraph #6223

Merged
merged 18 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ package-lock.json
website/public/_redirects
website/public/sitemap.xml
website/src/pages/docs/api/

.mise.toml
73 changes: 73 additions & 0 deletions packages/federation/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
inspect,
} from '@graphql-tools/utils';
import { SubgraphBaseSDL } from './subgraph.js';
import { getStitchedSchemaFromSupergraphSdl } from './supergraph.js';
import {
filterInternalFieldsAndTypes,
getArgsFromKeysForFederation,
Expand Down Expand Up @@ -232,3 +233,75 @@ export const federationSubschemaTransformer: SubschemaConfigTransform =
},
};
};

export async function getStitchedSchemaFromManagedFederation(options: {
graphRef: string;
apiKey: string;
upLink?: string;
lastSeenId?: string;
abortSignal?: AbortSignal;
EmrysMyrddin marked this conversation as resolved.
Show resolved Hide resolved
}) {
const { upLink = 'https://gateway.apollo.dev/graphql', abortSignal, ...variables } = options;

const response = await fetch(upLink, {
method: 'POST',
signal: abortSignal,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: /* GraphQL */ `
query ($apiKey: String!, $graphRef: String!, $lastSeenId: ID) {
routerConfig(ref: $graphRef, apiKey: $apiKey, ifAfterId: $lastSeenId) {
__typename
... on FetchError {
code
message
}
... on RouterConfigResult {
id
supergraphSDL
minDelaySeconds
}
}
}
`,
variables,
}),
});

if (!response.ok) {
throw new Error(
`Failed to fetch supergraph SDL from managed federation up link '${upLink}': [${response.status} ${response.statusText}] ${await response.text()}`,
);
}

try {
const { data, errors }: ExecutionResult = await response.json();

if (errors) {
throw new Error(
`Failed to fetch supergraph SDL from managed federation up link '${upLink}': ${errors.map(({ message }) => '\n' + message).join('')}`,
);
}

if (data.routerConfig.__typename === 'FetchError') {
const { code, message } = data.routerConfig;
throw new Error(
`Failed to fetch supergraph SDL from managed federation up link '${upLink}': [${code}] ${message}`,
);
}

const { supergraphSDL, id, minDelaySeconds } = data.routerConfig;

return {
schema: getStitchedSchemaFromSupergraphSdl(supergraphSDL),
EmrysMyrddin marked this conversation as resolved.
Show resolved Hide resolved
id,
minDelaySeconds,
};
} catch (err) {
throw new Error(
`Failed to parse response from managed federation up link '${upLink}': ${await response.text()}`,
);
}
}
2 changes: 2 additions & 0 deletions packages/federation/src/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,8 @@ export function getSubschemasFromSupergraphSdl({
return subschemaMap;
}

export function getStichedSchemaFromManageFederation() {}

export function getStitchedSchemaFromSupergraphSdl(opts: GetSubschemasFromSupergraphSdlOpts) {
const subschemaMap = getSubschemasFromSupergraphSdl(opts);
const supergraphSchema = stitchSchemas({
Expand Down
Loading