2

Is there a way to catch and modify response globally on the fly? I can do this for one query like below, but I want to do it for all queries.

apollo: {
    post: {
        query: Post,
        update(data) {
            return data.map(item => Object.assign(item, {foo: 'bar'})
        }
    }
}

It's simplified for this question, but under the hood I'd like to apply a constructor (class) to all objects...

I'm using nuxt-apollo. I searched for a way to do that in clientConfig or elsewhere so the solution may be related to apollo...

Thanks for your advice!

edit: OK, I found to do that with apollo-link, but I can't modify response. Here the code:

const constructorMiddleware = new ApolloLink((operation, forward) => {
    return forward(operation).map(response => {
      Object.keys(response.data).map(key => {
        if (!Array.isArray(response.data[key])) return;
        const newResponse = response.data[key].map(item => {
          return item.__typename === 'post'
            ? Object.assign(item, { foo: 'bar' })
            : item
        })
        console.log(newResponse)
        response.data[key] = newResponse
      })
      return response
    })
  })

I can see foo: bar in the newResponse, but the graphql returning by nuxt-apollo doesn't contains this newResponse, only original. Do ApolloLink override response? Does apollo cache change this?

edit 2: I tried to chain links and the newResponse of the constructorMiddleware is well in the next link. So the problem seems come from nuxt-apollo, or more vue-apollo...

ManUtopiK
  • 4,495
  • 3
  • 38
  • 52
  • Hi, where did you register your `constructorMiddleware`? I'm having the same kind of problem, but I don't know where to register the ApolloLink – Hammerbot Oct 07 '19 at 15:55
  • I think my [comment here](https://stackoverflow.com/questions/47211778/cleaning-unwanted-fields-from-graphql-responses#comment111218418_51380645) may be a good summary of current state. – piotr.d Jul 14 '20 at 10:31

0 Answers0