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

Type Policies in Apollo Client: Data Not Converting Properly with fetchPolicy: 'cache-and-network' #12102

Closed
Yoirishi opened this issue Oct 24, 2024 · 4 comments

Comments

@Yoirishi
Copy link

I encountered an issue with handling custom scalars. I am using autogenerated type policies to convert ISO8601 strings into Duration and DateTime objects.
Here is my type policies example:

import { InMemoryCache } from '@apollo/client';
import { DateTime, Duration } from 'ts-luxon';

const cache = new InMemoryCache({
  typePolicies: {
    BalloonEntity: {
      fields: {
        manufacturedAt: {
          read(value) {
            return value ? DateTime.fromISO(value) : value;
          },
        },
        ServiceLife: {
          read(value) {
            return value ? Duration.fromISO(value) : value;
          },
        },
      },
    },
  },
});

I use the useQuery hook to fetch data:

const {
  loading: isFetchingBalloons,
  error,
  data: orderData,
} = useQuery(fillBalloonsByOrderIdQuery, {
  onCompleted: (__data) => {
    console.log('__data received:', __data);
    console.log('Completed!', error ? 'failure' : 'success');
  },
  onError: (error) => {
    console.log(error);
  },
  variables: { orderId: orderId },
  fetchPolicy: 'cache-and-network',
});

I encountered a strange behavior where some requests from the server are handled correctly, and I see DateTime and Duration objects in the logs. However, in some cases, I see plain strings that the server returns (checked in the network tab).
As a result, the entire application crashes (because I use objects for human-readable date representations), and the plain strings naturally do not have such functions.
Yes, I understand that I can modify the prototype of strings to convert everything into the format I need upon calling the function and then return the result, and I can also check the types and do regular branching, but all this seems not correct way to fix this behaviour. What could be causing this strange behavior of apolloClient, and is there any way to fix it?

@Yoirishi
Copy link
Author

I also forgot to mention that I added logging to the read function and verified its calls in the source code. Everything appears to be correct: the raw value is the one returned by the server, the constructed objects are accurate, and the returned values are as expected. However, the data object still contains plain strings.

@phryneas
Copy link
Member

That certainly seems off. Could you try to create a minimal reproduction, e.g. as a CodeSandbox?

@Yoirishi
Copy link
Author

That certainly seems off. Could you try to create a minimal reproduction, e.g. as a CodeSandbox?

Unfortunately, I have not been able to reproduce this bug. Moreover, subsequent tests of the main system resulted in the behaviour changing to normal. Of course I'm a little concerned that this is a potentially floating bug, but for now I'll consider it an unfortunate coincidence of my operating system. I think the topic can be closed. If it happens again, I'll write back.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants