Apollo-client: Fragment is never used

Created on 11 Oct 2016  路  8Comments  路  Source: apollographql/apollo-client

Response from graphql server is: Fragment "___profile___requestIndex_1___userPhotoThumb" is never used. But the problem is in the apollo-client :(

I have next query via graphql decorator:

  query profile($slug: String!) {
    viewer {
      id,
    }
    user(slug: $slug) {
      id,
      ...userPhotoThumb,
    }
  }

On the server side rendering everything works fine. But when I try to continue on the client side I will get next response from graphql:

{
  "errors": [
    {
      "message": "Fragment \"___profile___requestIndex_1___userPhotoThumb\" is never used.",
      "locations": [
        {
          "line": 16,
          "column": 1
        }
      ]
    }
  ]
}

The problem is that request from apollo-client is

"query ___composed {
  ___profile___requestIndex_1___fieldIndex_0: viewer {
    id
    __typename
  }
  ___getViewer___requestIndex_2___fieldIndex_0: viewer {
    id
    __typename
  }
}

fragment ___profile___requestIndex_1___userPhotoThumb on User {
  ___profile___requestIndex_1___fieldIndex_1: picture
  ___profile___requestIndex_1___fieldIndex_2: slug
  ___profile___requestIndex_1___fieldIndex_3: __typename
}
"

As you can see "user" part is completely missing in the profile query. I think that user part is cached from server side rendering

Here is a complete graphql request

{
    "debugName": "___composed",
    "query": "query ___composed {\n  ___getViewer___requestIndex_0___fieldIndex_0: viewer {\n    id\n    __typename\n  }\n  ___profile___requestIndex_1___fieldIndex_0: viewer {\n    id\n    __typename\n  }\n  ___getViewer___requestIndex_2___fieldIndex_0: viewer {\n    id\n    __typename\n  }\n}\n\nfragment ___profile___requestIndex_1___userPhotoThumb on User {\n  ___profile___requestIndex_1___fieldIndex_1: picture\n  ___profile___requestIndex_1___fieldIndex_2: slug\n  ___profile___requestIndex_1___fieldIndex_3: __typename\n}\n",
    "variables": {
        "___profile___requestIndex_1___slug": "zlatko-fedor"
    }
}

I have tried apollo-client 0.4.19 and 0.4.20 with same results.
In this moment I am not able to use fragments anymore. Please help me to fix this issue.
Thank you very much

馃悶 bug

All 8 comments

@seeden This is due to query merging. If you turn off batching or use transport batching (if your server supports it) you will not see that error. In the current refactor we're moving query merging to a separate package, so if turning off batching will fix your issue, we should track this specific bug in the new repo.

I have tried to turn off batching and it is working fine but I would like to use batching because I can see a lot of requests without it. I am going to use apollo-server because I am using pure graphql server and there is no support for transport batching. Anyway where I am able to submit my issue?

update 1:
it is not working fine always but only sometimes. it looks really weird like a random behaviour

I have tried apollo-server without batching and this error is still here. But it is little bit different now.

The query in graphql request looks like this

"query profile {
  viewer {
    id
    __typename
  }
}

fragment userPhotoThumb on User {
  picture
  slug
  __typename
}
"

and response from server is

Fragment "userPhotoThumb" is never used.

But my original query looks like this

  query profile($slug: String!) {
    viewer {
      id,
    }
    user(slug: $slug) {
      id,
      firstName,
      lastName,
      name,
      description,
      ...userPhotoThumb,
    }
  }

As you can see this issue is still here and batching is turned off

Here is the response from transport batching

[{
    "data": {
        "viewer": {
            "id": "2",
            "__typename": "User"
        }
    }
}, {
    "errors": [{
        "message": "Fragment \"userPhotoThumb\" is never used.",
        "locations": [{
            "line": 8,
            "column": 1
        }]
    }]
}, {
    "data": {
        "viewer": {
            "id": "2",
            "__typename": "User"
        }
    }
}]

As you can see @helfer second request is wrong.

My serialized state from SSR looks like this

"data": {
            "User:2": {
                "id": "2",
                "__typename": "User"
            },
            "ROOT_QUERY": {
                "viewer": {
                    "type": "id",
                    "id": "User:undefined",
                    "generated": false
                },
                "user({\"slug\":\"zlatko-fedor\"})": {
                    "type": "id",
                    "id": "User:1",
                    "generated": false
                }
            },
            "User:1": {
                "id": "1",
                "firstName": "Zlatko",
                "slug": "zlatko-fedor",
                "__typename": "User"
            },
            "User:undefined": {
                "isFollowing({\"id\":\"1\"})": false,
                "__typename": "User"
            }
        },

I think that "user" part is cached and apollo-client will remove it from query completely because it has all information about this user. But it will forget to remove unused fragments.

@seeden ah, ok, makes sense. It's exactly what you suspected.
We removed the query diffing functionality, so the next version won't contain that bug any more :)

This is a result of some query manipulation we were doing before sending to the server, but we're removing that in 0.5 which should come out shortly. Follow along here: https://github.com/apollostack/apollo-client/issues/726

I can confirm it is fixed in the 0.5.0-1

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elie222 picture elie222  路  3Comments

MichaelDeBoey picture MichaelDeBoey  路  3Comments

joergbaier picture joergbaier  路  3Comments

eweilow picture eweilow  路  3Comments

canercandan picture canercandan  路  3Comments