-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for Retry/Timeout and some fixes
- Loading branch information
Showing
13 changed files
with
309 additions
and
87 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-mesh/transport-common': patch | ||
'@graphql-tools/executor-http': patch | ||
'@graphql-mesh/fusion-runtime': patch | ||
'@graphql-hive/gateway-runtime': patch | ||
--- | ||
|
||
Fix Retry / Timeout combination |
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,19 @@ | ||
import { defineConfig } from '@graphql-hive/gateway'; | ||
|
||
let i = 0; | ||
export const gatewayConfig = defineConfig({ | ||
upstreamRetry: { | ||
maxRetries: 4, | ||
}, | ||
upstreamTimeout: 300, | ||
plugins(ctx) { | ||
return [ | ||
{ | ||
onFetch({ options }) { | ||
i++; | ||
ctx.logger.info(`Fetching with ${options.body} for the ${i} time`); | ||
}, | ||
}, | ||
]; | ||
}, | ||
}); |
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,11 @@ | ||
{ | ||
"name": "@e2e/retry-timeout", | ||
"private": true, | ||
"dependencies": { | ||
"@apollo/subgraph": "^2.9.3", | ||
"@graphql-hive/gateway": "workspace:*", | ||
"graphql": "16.10.0", | ||
"graphql-yoga": "^5.10.6", | ||
"tslib": "^2.8.1" | ||
} | ||
} |
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,46 @@ | ||
import { createTenv } from '@internal/e2e'; | ||
import { expect, it } from 'vitest'; | ||
|
||
const { service, gateway } = createTenv(__dirname); | ||
|
||
it('Retry & Timeout', async () => { | ||
const flakeyService = await service('flakey'); | ||
const gw = await gateway({ | ||
supergraph: { | ||
with: 'apollo', | ||
services: [flakeyService], | ||
}, | ||
}); | ||
|
||
const res = await gw.execute({ | ||
query: /* GraphQL */ ` | ||
query { | ||
product(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
expect(res).toEqual({ | ||
data: { | ||
product: { | ||
id: '1', | ||
name: 'Product 1', | ||
}, | ||
}, | ||
}); | ||
|
||
const logs = gw.getStd('both'); | ||
// The first request will fail, and the gateway will retry 2 more times | ||
expect(logs).toContain( | ||
'Fetching with {"query":"{__typename product(id:\\"1\\"){id name}}"} for the 1 time', | ||
); | ||
expect(logs).toContain( | ||
'Fetching with {"query":"{__typename product(id:\\"1\\"){id name}}"} for the 2 time', | ||
); | ||
expect(logs).toContain( | ||
'Fetching with {"query":"{__typename product(id:\\"1\\"){id name}}"} for the 3 time', | ||
); | ||
}); |
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,97 @@ | ||
import { createServer } from 'http'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { GraphQLResolverMap } from '@apollo/subgraph/dist/schema-helper'; | ||
import { Opts } from '@internal/testing'; | ||
import { parse } from 'graphql'; | ||
import { | ||
createGraphQLError, | ||
createYoga, | ||
YogaInitialContext, | ||
} from 'graphql-yoga'; | ||
|
||
const opts = Opts(process.argv); | ||
|
||
let i = 0; | ||
let lastAttempt: number | undefined; | ||
|
||
const servicePort = opts.getServicePort('flakey'); | ||
|
||
const resolvers = { | ||
Query: { | ||
product: ( | ||
_: unknown, | ||
{ id }: { id: string }, | ||
context: YogaInitialContext, | ||
) => { | ||
i++; | ||
console.log(`${i} attempt`); | ||
if (lastAttempt && Date.now() - lastAttempt < 1000) { | ||
const secondsToWait = Math.ceil( | ||
(1000 - (Date.now() - lastAttempt)) / 1000, | ||
); | ||
return createGraphQLError('You are too early, still wait...', { | ||
extensions: { | ||
http: { | ||
status: 429, | ||
headers: { | ||
'retry-after': secondsToWait.toString(), | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
lastAttempt = Date.now(); | ||
// First attempt will fail with timeout | ||
if (i === 1) { | ||
let reject: (reason: any) => void; | ||
const promise = new Promise<void>((_resolve, _reject) => { | ||
reject = _reject; | ||
}); | ||
setTimeout(() => { | ||
reject('Timeout'); | ||
}, 1000); | ||
return promise; | ||
} | ||
// Second attempt will fail with 500 | ||
if (i === 2) { | ||
return createGraphQLError('Flakiness...', { | ||
extensions: { | ||
http: { | ||
status: 503, | ||
headers: { | ||
'retry-after': '1', | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
// Third attempt will return | ||
return { | ||
id, | ||
name: 'Product ' + id, | ||
}; | ||
}, | ||
}, | ||
} as GraphQLResolverMap<unknown>; | ||
|
||
createServer( | ||
createYoga({ | ||
schema: buildSubgraphSchema({ | ||
typeDefs: parse(/* GraphQL */ ` | ||
type Query { | ||
product(id: ID!): Product | ||
} | ||
type Product { | ||
id: ID! | ||
name: String! | ||
} | ||
`), | ||
resolvers, | ||
}), | ||
}), | ||
).listen(servicePort, () => { | ||
console.log( | ||
`🚀 Flakey service ready at http://localhost:${servicePort}/graphql`, | ||
); | ||
}); |
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
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
Oops, something went wrong.