Fork me on GitHub
#graphql
<
2023-06-21
>
favila22:06:11

Hello! We’re using lacinia in production extensively. We just noticed some surprising (to us) behavior that if we include a field multiple times via fragments, the resolver executes the number of times the field is “mentioned” in the query. For example:

query { myRoot { myField ...MyFieldAgain } }
fragment MyFieldAgain on Root { myField }
Our expectation is that the resolver for Root/myField would only run once. Is this just a bug or is there some good reason for this behavior? Has anyone noticed it?

favila22:06:59

This is a big deal for us because our API consumers use a pattern which assigns fragments to react components and then they build larger queries programmatically by knowing which child components will be mounted. The fragment composition can cause many fields to be repeated multiple times.

hlship23:06:17

What version of lacinia are you using?

hlship23:06:40

I did some work on fragments maybe 18 months ago.

hlship23:06:11

They are tricky to get right, without doing expensive up-front checks, and in the context of Lacinia executing leaft-to-root.

favila23:06:53

we’re using 1.2.1

favila23:06:20

I’m definitely willing to do some legwork here but need some pointing in the right direction. Is field merging something that there’s already code trying to do? Would this be something addressed in query parsing, xform-query, or in the executor? Are there tests that address field merging that I could extend?

Lennart Buit10:06:06

Some considerations, aliasing should still function and run field resolvers once:

query {
  field
  alias: field
}
But aliasing with arguments should run resolvers multiple times:
query {
  field(a: 1)
  alias: field(a: 2)
}
So arguments should be part of the uniqueness check, if that makes sense ^^.

favila13:06:49

should I make a github issue for this?