question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

šŸ› useQuery doesn't work when there is both a @client directive and a normal query together

See original GitHub issue

How to reproduce the issue:

  1. Create a query with both a @client directive (with an alias) and a normal query together, eg:
query GroupsTableQuery($organizationId: ID!) {
  clientOrganization: organization(id: $organizationId) @client {
    planType
  }
  organization(id: $organizationId) {
    groups {
      id
      name
    }
  }
}
  1. Use it:
  const { data, loading, error } = useQuery(GroupsTableQuery, {
    variables: { organizationId },
  });

Intended outcome:

  • The server correctly returns data with groups, id and name.
  • Apollo resolves the @client directive.
  • useQuery returns { data } with both clientOrganization and organization.

Actual outcome:

  • The server correctly returns data with groups, id and name. āœ…
  • Apollo doesnā€™t resolve the @client directive. āŒ
  • useQuery returns { data } as undefined. āŒ

Obs:

If I have two separate queries, everything works as expected (so I know there is nothing wrong with my queries). Eg:

query ClientGroupsTableQuery($organizationId: ID!) {
  clientOrganization: organization(id: $organizationId) @client {
    planType
  }
}

query GroupsTableQuery($organizationId: ID!) {
  organization(id: $organizationId) {
    groups {
      id
      name
    }
  }
}

PS: You might be wondering why I have an organization thatā€™s resolved on the client and another thatā€™s resolved on the server, and this is because weā€™re slowly migrating our BE to GraphQL, so parts of the data is resolved on the client through our Rest API.

Versions

  System:
    OS: macOS 10.15.5
  Binaries:
    Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
  Browsers:
    Chrome: 84.0.4147.89
    Edge: 84.0.522.40
    Firefox: 78.0.2
    Safari: 13.1.1
  npmPackages:
    @apollo/client: ^3.0.0-beta.41 => 3.0.0-beta.41
    @apollo/link-error: ^2.0.0-beta.3 => 2.0.0-beta.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zmaniaczcommented, Jul 19, 2020

Seeing the same issue on Apollo Client 3.0.2. In my case the query resolves correctly and the graphql call returns correct data when watching the Network tab in dev tools. However, ā€˜dataā€™ is still returned as ā€˜undefinedā€™. Removing the field with @client fixes it and have confirmed the @client field Iā€™m using works on itā€™s own.

0reactions
hwillsoncommented, May 4, 2021

It doesnā€™t sound like there is an outstanding issue here, so closing. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local resolvers - Apollo GraphQL Docs
This query includes a mixture of both remote and local fields. isInCart is the only field marked with an @client directive, so it's...
Read more >
React Apollo: Understanding Fetch Policy with useQuery
I've been working on an Apollo client project these days. ... A cache-only query throws an error if the cache does not contain...
Read more >
Redux to Apollo: Data Access Patterns - NerdWallet
When Apollo encounters a field marked with the client directive, it knows not to send that field over the network and instead resolve...
Read more >
Introduction to queries - Microsoft Support
An Access query is very versatile and can pull information from various tables and assemble it for display in a form or report....
Read more >
Using REST API with Apollo Client by Avi Takiyar
In this tutorial, it is demonstrated how a REST API can be used in our React/NextJS app using Apollo Client and our already...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found