apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

@defer response not interpreted correctly #12157

Open dennemark opened 12 hours ago

dennemark commented 12 hours ago

Issue Description

Hi, I am trying to do a deferred query but apollo client does not interpret response as multipart, but json.

Link to Reproduction

see below

Reproduction Steps

Service:

export const schema = gql`
  type Query {
    """
    A field that resolves fast.
    """
    fastField: String! @skipAuth

    """
    A field that resolves slowly.
    Maybe you want to @defer this field ;)
    """
    slowField: String @skipAuth
  }
`

Endpoint


const wait = (time: number) => new Promise((resolve) => setTimeout(resolve, time))

export const fastField = async () => {
  return 'I am fast'
}

export const slowField = async (_, { waitFor = 5000 }) => {
  await wait(waitFor)
  return 'I am slow'
}

Query:

  query FindFastAndSlowQuery {
    fastField
    ... on Query @defer {
      slowField
    }
  }

I get the following response:


---
Content-Type: application/json; charset=utf-8
Content-Length: 65

{"data":{"fastField":"I am fast"},"hasNext":true,"extensions":{}}
---
Content-Type: application/json; charset=utf-8
Content-Length: 115

{"incremental":[{"data":{"__typename":"Query","slowField":"I am slow"},"path":[]}],"hasNext":false,"extensions":{}}
-----

Now apollo/client throws an error, because it wants to have a json instead of multipart:

JSON.parse: unexpected non-digit at line 2 column 2 of the JSON data

I think it happens around here: https://github.com/apollographql/apollo-client/blob/77b89318040748b3ffad97938eccacd8b1fbb4bd/src/link/batch-http/batchHttpLink.ts#L182

@apollo/client version

3.11.1

phryneas commented 12 hours ago

Is your server running on graphql 17 alpha 2? We currently only support that one version of @defer until the spec has been finalized - otherwise we would have to ship a lot of different versions of HttpLink.

dennemark commented 12 hours ago

Thanks for the fast reply! I do think it is currently version 16.9.1. Since I am using it within redwood framework, I am going to contact them! This should lead in the right direction!

Cross posting here: https://community.redwoodjs.com/t/graphql-defer-directive/7639/2

Going to let you know, if this helped.