每次我调用查询或突变时,都会进行两次网络调用,然后转到创建的项目。我试图更改中间件,但仍然遇到相同的问题。 “ apollo-cache-memory”:“ ^ 1.2.2”, “ apollo-client”:“ ^ 2.3.2”, “ apollo-link”:“ ^ 1.2.12”, “ apollo-link-context”:“ ^ 1.0.8”, “ apollo-upload-client”:“ ^ 11.0.0”,
我尝试减少中间件并更改一些软件包,但仍然遇到相同的问题。
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { appConfig } from 'config'
import { ApolloLink, from } from 'apollo-link'
import { logger } from 'libs/logger'
import { fetchKeyFromSession } from 'libs/session'
import { AUTHENTICATION_TOKEN } from 'constants/index'
import { createUploadLink } from 'apollo-upload-client'
const authMiddleware = new ApolloLink((operation, forward) => {
logger(
'GraphQL Interceptor ==>> ',
operation.query.definitions[0].selectionSet.selections[0].name.value,
window.location.pathname,
operation.query.definitions[0].selectionSet.selections[0].arguments,
window.location.pathname,
operation.query.definitions[0].selectionSet.selections[0].arguments.map(
arg => arg && arg.value && arg.value.name.value,
),
)
operation.setContext({
headers: {
Authorization: `Bearer ${fetchKeyFromSession(AUTHENTICATION_TOKEN)}`
}
})
forward(operation).subscribe({
next: result => logger('Apollo Link Result ==>> ', result),
error: error => logger('Apollo Link Error ==>> ', error.response),
})
return forward(operation)
})
const uploadLink = createUploadLink({ uri: `${appConfig.config.BASE_URL}/api/v1` })
const cache = new InMemoryCache()
const defaultOptions = {
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'all',
},
}
export const client = new ApolloClient({
link: from([authMiddleware, uploadLink]),
cache,
defaultOptions,
queryDeduplication: false
})
它根本不会引发任何错误,但是它应该只调用一次查询/变异而不是两次。因此,我在数据库中创建了两个项目。
答案 0 :(得分:1)
const authMiddleware = new ApolloLink((operation, forward) => {
logger(
'GraphQL Interceptor ==>> ',
operation.query.definitions[0].selectionSet.selections[0].name.value,
window.location.pathname,
operation.query.definitions[0].selectionSet.selections[0].arguments,
window.location.pathname,
operation.query.definitions[0].selectionSet.selections[0].arguments.map(
arg => arg && arg.value && arg.value.name.value,
),
)
operation.setContext({
headers: {
Authorization: `Bearer ${fetchKeyFromSession(AUTHENTICATION_TOKEN)}`
}
})
return forward(operation)
})
forward(operation).subscribe()导致问题将其删除,它将开始正常工作。