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.

Gql arguments are required if defined in keyArgs

See original GitHub issue

I’m unsure if this is intended behaviour and I can’t seem to find much documentation on this feature as the documentation is being worked on (#6711).

I am using relayStylePagination / keyArgs like so

Intended outcome:

  • keyArgs should not require the argument to be defined in the gql query, if a key arg is defined in the InMemoryCache but not the specific gql, the key arg should be ignored
  • The error message (unless this is made redundant by above change) needs to be changed to an appropriate message for keyArgs

InMemoryCache

const cache = new InMemoryCache({
    Query: {
      fields: {
        contacts: relayStylePagination(["mine", "scopes", "propertyId"])
      },
    },
  },
});

gql

query GetContacts($mine: Boolean $cursor: String, $scopes: [ScopesEnum!]) { ... }

This causes issues if you have a large application with multiple gql files defined for the same field but with different arguments - my current work around is to write a function for the keyArgs and filter them depending on the arguments like so

const filterKeyArgsToArgs = (keyArgs: any) => (args: any) =>
  args ? keyArgs.filter((keyArg: any) => args.hasOwnProperty(keyArg)) : null;

const cache = new InMemoryCache({
    Query: {
      fields: {
        contacts: relayStylePagination(filterKeyArgsToArgs(["mine", "scopes", "propertyId"]))
      },
    },
  },
});

Actual outcome:

I get the following error (which is worded for key fields) when the propertyId key arg is defined in the InMemoryCache but not in the gql

Invariant Violation: Missing field 'propertyId' while computing key fields

How to reproduce the issue:

  • Define keyargs for a field in the InMemoryCache
  • Have a gql query for that field which uses those arguments - works fine
  • Have a gql query for that field which does not use those arguments - does not work (gets above error)

Let me know if you need to create a demo

Versions

  System:
    OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
  Binaries:
    Node: 12.18.3 - /usr/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.7 - /usr/bin/npm
  Browsers:
    Chrome: 85.0.4183.83
    Firefox: 80.0.1
  npmPackages:
    @apollo/client: ^3.1.4 => 3.1.4

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
tadhglewiscommented, Sep 10, 2020

@zsaraf I believe relayStylePagination and all the other pagination helpers are more guides on how to guide your pagination helper?

Either way they should still be usable (and the fact that this affects all keyArgs! not just relayStylePagination).

Here is my solution:

const filterKeyArgsToArgs = (keyArgs: any) => (args: any) =>
  args ? keyArgs.filter((keyArg: any) => args.hasOwnProperty(keyArg)) : null;

const cache = new InMemoryCache({
    Query: {
      fields: {
        contacts: relayStylePagination(filterKeyArgsToArgs(["mine", "scopes", "propertyId"]))
      },
    },
  },
});

Basically you can pass a function into relayStylePagination / keyArgs which will receive the current arguments, then filter out the keyargs which aren’t in the arguments. Let me know if you have any trouble with it, it’s what i’ve been using in production for the past couple weeks after removing updateQuery.

3reactions
benjamncommented, Oct 1, 2020

@tadhglewis I think allowing optional arguments in keyArgs is the right solution here! Fix incoming: #7109

Read more comments on GitHub >

github_iconTop Results From Across the Web

Key arguments in Apollo Client - Apollo GraphQL Docs
If we query for User s with id s 1 and 2 , the Apollo Client cache stores ... you define keyArgs for...
Read more >
Use kwargs in a function when it contains arguments not-used ...
get("foo","bar) in sub1 ) but I want to know, if we can avoid doing so and have only one kwargs ? python ·...
Read more >
What are *args and **kwargs and How to use them - ListenData
args is a short form of arguments. With the use of *args python takes any number of arguments in user-defined function and converts...
Read more >
Passing Arguments - GraphQL
A query language for your API — GraphQL provides a complete description of the data in your API, gives clients the power to...
Read more >
API Documentation — peewee 3.15.4 documentation
String used as parameter placeholder in SQL queries. ... If permanent is specified, then the PRAGMA query will also be executed whenever a...
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