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

Custom scalar isn't being respected #3371

Closed
solidcell opened this issue Apr 19, 2024 · 7 comments
Closed

Custom scalar isn't being respected #3371

solidcell opened this issue Apr 19, 2024 · 7 comments
Labels

Comments

@solidcell
Copy link

Summary

https://www.loom.com/share/c6faff0cb93f4c9b85342cdb9053e52c?sid=ed020aee-67eb-4831-9832-4b5645a4535a

I've defined a custom scalar BasicScalar which is seemingly not being respected/used for a field which is defined as result: BasicScalar!. Instead, the DataDict ends up having result: AnyHashable(Bool). So instead of it decoding the query so that result is BasicScalar (.bool(true)), it's decoding result as plain true and not using the BasicScalar type at all, as the GraphQL schema specifies. What am I doing wrong? My schema, operation, and custom scalar all seem to be defined accordingly, yet..

Version

1.9.0

Steps to reproduce the behavior

See the summary.

Logs

No response

Anything else?

No response

@solidcell solidcell added bug Generally incorrect behavior needs investigation labels Apr 19, 2024
@calvincestari
Copy link
Member

Hi @solidcell - is the data value being sent as regular JSON bool? Something like this:

{
  result: true
}

@solidcell
Copy link
Author

Yea. In this case it's true but it could be for example 54. I've defined init(_jsonValue value: JSONValue) so I'm not sure why it's being decoded as a plain Bool for this response.

@calvincestari
Copy link
Member

Are you able to share the definition for BasicScalar? Does the behaviour change if the value is 54 for example, i.e.: something other than a boolean value?

@calvincestari
Copy link
Member

OK, I think the issue is your implementation of _asAnyHashable. It in turn calls the private function hashable which is returning the base type such as Bool or Int. I believe it's that value that ends up getting stored in DataDict. You shouldn't need to define _asAnyHashable at all. Essentially you're turning your custom scalar back into the base type.

Here is a sample project which defines a custom scalar pretty similar to yours just with less variants to it. The test passes with the custom scalar type being returned as expected. In the custom scalar file there is a commented out portion with the same _asAnyHashable and hashable functions that you have, and if you uncomment that section you'll get failures in the test with the same error you're seeing.

tl;dr - remove your _asAnyHashable function and it should work as expected.

@solidcell
Copy link
Author

That was it, thanks! I was waiting to trim code until things started working 🤦

Appreciate your help!

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 iOS 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
Projects
None yet
Development

No branches or pull requests

2 participants