0

Every time I call a query or a mutation It makes two network call and I get to items created. I tried to change the middleware but still getting the same issues. "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",

I have tried reducing the middleware and changing some packages but still getting the same issue.

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
})

It's not throwing any error at all but it should call the query/mutation only once not twice. I got two items created in the database due to this.

2
  • Do you use schema stitching? Do you run multiple queries in a single request? Commented Sep 23, 2019 at 21:19
  • @KfirDadosh no I don't I just figured out that the forward(operation).subscribe is causing the issue after removing it everything started to work fine but I want to run some functions and logs for every request, response, and error but the issue is I don't find a good way to how to do it after removing the forward(operation).subscribe. Your helo will be appreciated Commented Sep 23, 2019 at 21:33

2 Answers 2

2
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)
})

The forward(operation).subscribe() is causing the issues remove it, it will started to work fine.

0

This is an alternative to subscribe:

forward(operation).forEach((data) => {
  data.errors?.forEach((error) => logger('Apollo Link Error ==>> ', error.response));
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.