问题
I have a long running import script (~4 minutes) which is kicked off with a graphql mutation. Logging on the server, I've noticed that exactly 2 minutes after I trigger the mutation, it gets retried, causing the import to be run twice.
I guess this is caused by some functionality in apollo-link
but I've had a look through the code there and can't find an option to turn it off.
Here is how I've set up apollo:
import ApolloClient from 'apollo-client'
import { ApolloLink } from 'apollo-link'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import config from 'src/config'
import { getItem } from 'src/utils/local-store'
const httpLink = createHttpLink({ uri: config.graphql })
const middlewareLink = new ApolloLink((operation, forward) => {
const token = getItem(config.jwtKey)
if (token) {
operation.setContext({
headers: {
Authorization: `Bearer ${token}`
}
})
}
return forward(operation)
})
const client = new ApolloClient({
link: middlewareLink.concat(httpLink),
cache: new InMemoryCache().restore(window.__APOLLO_STATE__ || {})
})
export default client
There is nothing fancy going on in the mutation itself:
export class ReleaseImport extends PureComponent {
// ...
handleSaveRelease = async () => {
const { save, artistId } = this.props
const { id, releaseGroupId } = this.state
await save({ variables: { release: { id, releaseGroupId }, artistId } })
}
// ...
}
const saveArtistRelease = gql`
mutation ImportSaveArtistRelease($release: ImportReleaseInput!, $artistId: Int!) {
importSaveArtistRelease(release: $release, artistId: $artistId) {
id
}
}
`
export default compose(
graphql(saveArtistRelease, {
name: 'save'
})
)(ReleaseImport)
Just looking to turn off this retry functionality. Thanks.
回答1:
I was barking up the wrong tree.
It turns out node's default timeout is 2 minutes, and if it does get to the 2 minutes, it WILL retry the request.
In my case, using Koa, the fix was simply:
// make timeout value 5 minutes
app.use(async (ctx, next) => {
ctx.req.setTimeout(5 * 60 * 1000)
await next()
})
来源:https://stackoverflow.com/questions/48661753/react-apollo-long-running-mutation-appears-to-be-retried-after-2-minutes