-
-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(federation): avoid extra calls if keys are already fetched
- Loading branch information
Showing
6 changed files
with
233 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-tools/federation': patch | ||
'@graphql-tools/delegate': patch | ||
'@graphql-tools/stitch': patch | ||
--- | ||
|
||
Avoid extra calls if the keys are already resolved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/federation/test/getStitchedSchemaFromLocalSchemas.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ExecutionResult, GraphQLSchema } from 'graphql'; | ||
import { kebabCase } from 'lodash'; | ||
import { createDefaultExecutor } from '@graphql-tools/delegate'; | ||
import { ExecutionRequest, isPromise } from '@graphql-tools/utils'; | ||
import { getStitchedSchemaFromSupergraphSdl } from '../src/supergraph'; | ||
|
||
export interface LocalSchemaItem { | ||
name: string; | ||
schema: GraphQLSchema; | ||
} | ||
|
||
export async function getStitchedSchemaFromLocalSchemas( | ||
localSchemas: Record<string, GraphQLSchema>, | ||
onSubgraphExecute?: ( | ||
subgraph: string, | ||
executionRequest: ExecutionRequest, | ||
result: ExecutionResult | AsyncIterable<ExecutionResult>, | ||
) => void, | ||
): Promise<GraphQLSchema> { | ||
const { IntrospectAndCompose, LocalGraphQLDataSource } = await import('@apollo/gateway'); | ||
const introspectAndCompose = await new IntrospectAndCompose({ | ||
subgraphs: Object.keys(localSchemas).map(name => ({ name, url: 'http://localhost/' + name })), | ||
}).initialize({ | ||
healthCheck() { | ||
return Promise.resolve(); | ||
}, | ||
update() {}, | ||
getDataSource({ name }) { | ||
const [, localSchema] = | ||
Object.entries(localSchemas).find(([key]) => kebabCase(key) === kebabCase(name)) || []; | ||
if (localSchema) { | ||
return new LocalGraphQLDataSource(localSchema); | ||
} | ||
throw new Error(`Unknown subgraph ${name}`); | ||
}, | ||
}); | ||
function createTracedExecutor(name: string, schema: GraphQLSchema) { | ||
const executor = createDefaultExecutor(schema); | ||
return function tracedExecutor(request: ExecutionRequest) { | ||
const result = executor(request); | ||
if (onSubgraphExecute) { | ||
if (isPromise(result)) { | ||
return result.then(result => { | ||
onSubgraphExecute(name, request, result); | ||
return result; | ||
}); | ||
} | ||
onSubgraphExecute(name, request, result); | ||
} | ||
return result; | ||
}; | ||
} | ||
return getStitchedSchemaFromSupergraphSdl({ | ||
supergraphSdl: introspectAndCompose.supergraphSdl, | ||
onSubschemaConfig(subschemaConfig) { | ||
const [name, localSchema] = | ||
Object.entries(localSchemas).find( | ||
([key]) => kebabCase(key) === kebabCase(subschemaConfig.name), | ||
) || []; | ||
if (name && localSchema) { | ||
subschemaConfig.executor = createTracedExecutor(name, localSchema); | ||
} else { | ||
throw new Error(`Unknown subgraph ${subschemaConfig.name}`); | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters