graphql

favila 2023-06-21T22:40:11.908999Z

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?

favila 2023-06-23T13:45:49.878719Z

should I make a github issue for this?

favila 2023-06-21T22:41:59.116589Z

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.

hlship 2023-06-21T23:22:17.957399Z

What version of lacinia are you using?

hlship 2023-06-21T23:22:40.358979Z

I did some work on fragments maybe 18 months ago.

hlship 2023-06-21T23:23:11.861759Z

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

favila 2023-06-21T23:30:53.634319Z

we’re using 1.2.1

favila 2023-06-21T23:51:20.880549Z

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?

2023-06-22T10:54:06.459029Z

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 ^^.