Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache should not add __typename to @export-ed input variables #4691

Closed
OurMajesty opened this issue Apr 11, 2019 · 5 comments
Closed

Cache should not add __typename to @export-ed input variables #4691

OurMajesty opened this issue Apr 11, 2019 · 5 comments

Comments

@OurMajesty
Copy link

OurMajesty commented Apr 11, 2019

Intended outcome:
Non-scalar @export-ed variables should be send to the server without adding a __typename field, because input types do not have a __typename field.

Actual outcome:
Server responds with Field "__typename" is not defined by type LessonFilter. because the $where variable has an extra __typename field added.

How to reproduce the issue:
https://codesandbox.io/s/n58767y81p

The query:

  query GetListItems($where: LessonFilter) {
    currentFilter @client @export(as: "where") {
      title_contains
      enabled
    }
    lessonCollection(where: $where) {
      items {
        title
        slug
      }
    }
  }

currentFilter in cache is like this:

{
      __typename: "LessonFilter",
      title_contains: "SDK",
      enabled: true
}

Versions
apollo-client@2.5.1 and apollo-client@2.6.0-beta.4

Workaround for me right now is to have client resolved extra field, which is @export-ed instead of the original currentFilter and clears the extra __typename.

@supercranky
Copy link

I'm having the same problem.

@benjamn benjamn self-assigned this Apr 25, 2019
@benjamn benjamn changed the title Non-scalar @extend'ed variables having __typename in them Cache should not add __typename to @export-ed input variables May 7, 2019
@benjamn benjamn added this to the Release 2.6.0 milestone May 7, 2019
@OurMajesty
Copy link
Author

According to (updated) reproduction, the error is still there with latest apollo-client 🤔

@OurMajesty
Copy link
Author

@benjamn 2.6.2 - same error in reproduction, where's the problem? Should I recreate issue?

@Crysp
Copy link

Crysp commented Jun 10, 2019

Theirs fixes just remove __typename from query. but you should to remove __typename from resolver by yourself.

it works for me.

resolvers.js - remove from response

export const filter = (root, variables, { cache }, { field }) => {
    const data = cache.readQuery({ query: filterQuery });
    if (field.directives.some(({ name }) => name.value === 'export')) {
        delete data.filter.__typename;
    }
    return data.filter;
};

cache.js - add when writing cache

const cache = new InMemoryCache({
    dataIdFromObject: (object) => {
        // check some unique field for filter object
        if ('contractual' in object) {
            object.__typename = 'filter';
        }
        return defaultDataIdFromObject(object);
    },
});

@Jayphen
Copy link

Jayphen commented Aug 20, 2019

Do we really have to manually delete the __typename from the response as above? This seems rather hacky. I don't quite understand how this PR resolved the issue.

Here is a reproduction:
https://codesandbox.io/s/using-local-state-with-gql-server-response-ouqlu

The fix at

// If this SelectionSet is @export-ed as an input variable, it should
does not remove the __typename from nested fields.

i.e. in this:

          filters @client @export(as: "filters") {
            priceFilters {
              value
            }

It removes the __typename from filters, but not from priceFilters.

I have been getting around it by doing this

          filters @client @export(as: "filters") {
            priceFilters @export {
              value
            }

However this also seems like a silly hack…

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants