每次我调用一个查询或一个变体时,它都会进行两次网络调用,然后我就会得到已创建的项。我尝试更改中间件,但仍然遇到相同的问题。"apollo-cache-inmemory":"^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
})它根本没有抛出任何错误,但它应该只调用一次查询/突变,而不是两次。由于这个原因,我在数据库中创建了两个项目。
发布于 2019-09-24 05:37:54
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)
})转发(操作).subscribe()导致问题删除它,它将开始正常工作。
发布于 2021-09-16 13:34:55
这是subscribe的替代方案
forward(operation).forEach((data) => {
data.errors?.forEach((error) => logger('Apollo Link Error ==>> ', error.response));
});https://stackoverflow.com/questions/58070242
复制相似问题