-
-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(federation): merge errors for shared root fields correctly (#6355)
* fix(federation): merge errors for shared root fields correctly * Tests * aggregateerror message * Spread AggregateError in errors in ExecutionResult * Fix lint --------- Co-authored-by: enisdenjo <[email protected]>
- Loading branch information
Showing
4 changed files
with
232 additions
and
8 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,8 @@ | ||
--- | ||
'@graphql-tools/federation': patch | ||
--- | ||
|
||
Handle errors coming from subgraphs correctly when a root field is shared by different subgraphs | ||
|
||
- If subgraph A returns an error for `Query.foo`, and subgraph B returns the data, ignore the error and keep it for null fields. | ||
- If both subgraphs return errors, return them as `AggregateError` then return them to the gateway result. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { GraphQLSchema, parse } from 'graphql'; | ||
import { IntrospectAndCompose, LocalGraphQLDataSource } from '@apollo/gateway'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { createDefaultExecutor } from '@graphql-tools/delegate'; | ||
import { normalizedExecutor } from '@graphql-tools/executor'; | ||
import { isAsyncIterable } from '@graphql-tools/utils'; | ||
import { getStitchedSchemaFromSupergraphSdl } from '../src/supergraph'; | ||
|
||
describe('Error handling', () => { | ||
let aResult: any; | ||
let bResult: any; | ||
const subgraphA = buildSubgraphSchema({ | ||
typeDefs: parse(/* GraphQL */ ` | ||
type Query { | ||
foo: Foo | ||
} | ||
type Foo @key(fields: "id") { | ||
id: ID! | ||
bar: String | ||
} | ||
`), | ||
resolvers: { | ||
Query: { | ||
foo() { | ||
return aResult; | ||
}, | ||
}, | ||
Foo: { | ||
__resolveReference(root) { | ||
return root; | ||
}, | ||
bar() { | ||
return 'Bar'; | ||
}, | ||
}, | ||
}, | ||
}); | ||
const subgraphB = buildSubgraphSchema({ | ||
typeDefs: parse(/* GraphQL */ ` | ||
type Query { | ||
foo: Foo | ||
} | ||
extend type Foo @key(fields: "id") { | ||
id: ID! | ||
baz: String | ||
} | ||
`), | ||
resolvers: { | ||
Query: { | ||
foo() { | ||
return bResult; | ||
}, | ||
}, | ||
Foo: { | ||
__resolveReference(root) { | ||
return root; | ||
}, | ||
baz() { | ||
return 'Baz'; | ||
}, | ||
}, | ||
}, | ||
}); | ||
let supergraph: GraphQLSchema; | ||
beforeAll(async () => { | ||
const { supergraphSdl } = await new IntrospectAndCompose({ | ||
subgraphs: [ | ||
{ | ||
name: 'A', | ||
url: 'http://localhost:4001/graphql', | ||
}, | ||
{ | ||
name: 'B', | ||
url: 'http://localhost:4002/graphql', | ||
}, | ||
], | ||
}).initialize({ | ||
getDataSource({ name }) { | ||
if (name === 'A') { | ||
return new LocalGraphQLDataSource(subgraphA); | ||
} | ||
if (name === 'B') { | ||
return new LocalGraphQLDataSource(subgraphB); | ||
} | ||
throw new Error(`Unknown subgraph: ${name}`); | ||
}, | ||
async healthCheck() {}, | ||
update() {}, | ||
}); | ||
supergraph = getStitchedSchemaFromSupergraphSdl({ | ||
supergraphSdl, | ||
onSubschemaConfig(subschemaConfig) { | ||
if (subschemaConfig.name === 'A') { | ||
subschemaConfig.executor = createDefaultExecutor(subgraphA); | ||
} else if (subschemaConfig.name === 'B') { | ||
subschemaConfig.executor = createDefaultExecutor(subgraphB); | ||
} else { | ||
throw new Error(`Unknown subgraph: ${subschemaConfig.name}`); | ||
} | ||
}, | ||
}); | ||
}); | ||
it('chooses the successful result from shared root fields', async () => { | ||
aResult = new Error('A failed'); | ||
bResult = { id: '1' }; | ||
const result = await normalizedExecutor({ | ||
schema: supergraph, | ||
document: parse(/* GraphQL */ ` | ||
query { | ||
foo { | ||
id | ||
bar | ||
baz | ||
} | ||
} | ||
`), | ||
}); | ||
if (isAsyncIterable(result)) { | ||
throw new Error('Expected result to be an ExecutionResult'); | ||
} | ||
expect(result.errors).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
foo: { | ||
id: '1', | ||
bar: null, | ||
baz: 'Baz', | ||
}, | ||
}); | ||
}); | ||
it('merges errors from shared root fields', async () => { | ||
aResult = new Error('A failed'); | ||
bResult = new Error('B failed'); | ||
const result = await normalizedExecutor({ | ||
schema: supergraph, | ||
document: parse(/* GraphQL */ ` | ||
query { | ||
foo { | ||
id | ||
bar | ||
baz | ||
} | ||
} | ||
`), | ||
}); | ||
if (isAsyncIterable(result)) { | ||
throw new Error('Expected result to be an ExecutionResult'); | ||
} | ||
expect(result.errors).toHaveLength(2); | ||
expect(result.errors).toContainEqual( | ||
expect.objectContaining({ | ||
message: 'A failed', | ||
}), | ||
); | ||
expect(result.errors).toContainEqual( | ||
expect.objectContaining({ | ||
message: 'B failed', | ||
}), | ||
); | ||
}); | ||
}); |