In a query I got, I got a field which is nullable,...
# apollo-kotlin
s
In a query I got, I got a field which is nullable, say
newestMessage: ChatMessage
. In the business logic for this specific case all I care for is if it's null or not, not really interested in its internal state. Right now in the .graphql file I specify:
Copy code
newestMessage {
  id
}
Or even
Copy code
newestMessage {
  __typename
}
but IDE plugin is hinting to me that I am not using it and it's greyed out. I can't do
newestMessage {}
either as that's an error. I suppose there's no better way around this other than slapping a
# noinspection ApolloUnusedField
on top of the
id
field right?
w
Should
__typename
ever be unused? 🤔 I think it's always fetched anyway
s
Yeah I suppose the IDE plugin is looking at user-code, so it makes sense that it marks it as unused. But perhaps it should make an exception for this? Btw the
id
in this case is in fact the primary key too, aka in my
extra.graphqls
I got
extend interface ChatMessage @typePolicy(keyFields: "id")
, so from previous discussions here I understand that I will also fetch that one regardless of if I specify it or not. So both of them I suppose "technically" are never not asked for? Perhaps? Doing some guess-work here, that's why I came here to ask in the first place 😄
m
I think it’s always fetched anyway
It’s always fetched and might end up being used by the normalized code but this is “unsafe” access, i.e. it’s accessed at the JSON level, before it’s turned into typesafe Kotlin so the IDE doesn’t know about those usages
thank you frog 1
always
Actually, it “depends”, there’s a parameter for that
👀 1
s
> Actually, it “depends”, there’s a parameter for that Is this for
__typename
or for the ˚keyField"?
m
For
__typename
(it’s called
addTypename
(kdoc))
b
Should
__typename
ever be unused? 🤔 I think it's always fetched anyway
It's automatically added to your queries - but if you explicitly query it, then I think it makes sense to check if you actually use it in your code?
👍 1
s
I think it makes sense to check if you actually use it in your code
Yeah that's what I assumed here too. So perhaps the "# noinspection ApolloUnusedField" is the most reasonable approach for my use case?
b
I think so yes 👍
👍 1
m
I’m not sure why GraphQL forbids empty selection sets, that would solve this issue
yes black 2
s
A bit unrelated perhaps, but regarding:
For
__typename
(it’s called
addTypename
(kdoc))
I see two interesting things here: • "ifFragments" (deprecated) • Default value: "ifFragments" The default is the deprecated one? And afaik there's no reference to this in the 3.x->4.x migration guide. is this something that we should optimally not leave as the deprecated option? I've never seen it before so I've never given it any thought before.
m
I’m a bit anxious at changing this default but don’t want people to explicitely use “ifFragments” either, hence the current situation
There are 2 situations: • normalized cache users want
"always"
because if an interface is introduced, a missing
__typename
might introduce a cache miss • other users want
"ifPolymorphic"
because
__typename
wastes bandwidth and looks not cool in the JSON
s
Who wants
ifAbstract
? 😄
m
Probably me cause it’s easy to implement 😄
😅 1
But could probably be deprecated
s
So for current normalized-cache users who are just using the default "ifFragments", it's the default behavior, but it's technically introducing some cache misses that could be avoided with "always" right? And is the "always" that much heavier for it to not be a safe bet to use as a default? Or are you worried that people will experience behavior change (even if it means "more correct" cache hits) that people may rely on?
m
it’s technically introducing some cache misses that could be avoided with “always” right?
exactly, it’s suboptimal
Or are you worried that people will experience behavior change
That. Changing that is a pain because it changes a lot of models. Typical use case is users creating their data classes manually in unit tests.
Changing to
always
will require everyone to pass a
__typename
argument to every model, this is not fun. Especially for users that are not super familiar with
GraphQL
, it feels very weird. Where does this
__typename
come from? 🤷
And is the “always” that much heavier for it to not be a safe bet to use as a default?
That’s also a concern actually. I’m not 100% about the “heaviness” of it. With gzip it should compress quite well but it’s still surprising to a non-negligeable portion of users and creates friction. See https://github.com/apollographql/apollo-kotlin/issues/1018 for an example