graphql-laravel icon indicating copy to clipboard operation
graphql-laravel copied to clipboard

SelectFields will only fire relationship query once and therefore with wrong arguments

Open crissi opened this issue 4 years ago • 6 comments

I was looking into this https://github.com/rebing/graphql-laravel/issues/602 and fell over this bug.

With this query:

query ($proId: Int, $proId2: Int) {
  test2 {
    name

    alias1: profile(profileId: $proId2) {
      contentType
    }
    
    alias2:profile(profileId: $proId) {
      contentType
    }

  }
}
{
  "proId": 2,
  "proId2": 4,
  "id": 70
}

image

The profile relationship is only run once and therefore the different arguments given to the custom query clause is not taken into account, only the last query get the right args.

It seems a bit to hard to fix since it is not easily possible to alias eloquent relationships.

Unfortunately something like this is not possible. This would have made it a simplier fix User::with('relationship', 'relationship AS other')->get();

crissi avatar Mar 15 '20 14:03 crissi

How did it end? We ran into the same issue :( Saw the closed PR, but anything else

Pijuli avatar Jul 16 '20 10:07 Pijuli

I made a solution, but it was quite hacky, so it was not accepted, see here: https://github.com/rebing/graphql-laravel/pull/612

crissi avatar Jul 16 '20 10:07 crissi

Yes, I saw it. I can't help as I'm not into the core of it. We managed to make it work by splitting the query... but it'll probably take some more resources. Anyway. I'll keep an eye on the issue. Thanks anyway.

Pijuli avatar Jul 16 '20 10:07 Pijuli

My personal take (as I've expressed in https://github.com/rebing/graphql-laravel/pull/612#pullrequestreview-387360786 ):

I don't think the approach using SelectFields is technically appropriate here. It relies on upfront figuring out everything needed so that the appropriately, possibly nested, SQL relations and stuff can be solved. This results in incredible amount of complexity.

Although the GraphQL spec stays highly absent in talking about how to fetch the data, using dataloaders is a very common and well understood technique.

Since graphql-laravel is a thin wrapper over graphql-php, everything from e.g. https://webonyx.github.io/graphql-php/data-fetching/#solving-n1-problem should apply. And this is in fact how I've approached this in all my projects and it works very well and also is datasource agnostic, as it simply doesn't matter whether it's Eloquent, ElasticSearch, etc.

I would love to be proven wrong, but I don't think this will be solved in SelectFields and ~workarounds~ different approaches to solve this are possible.

mfn avatar Apr 05 '21 20:04 mfn

Maybe we should add a "caveat" section to the readme and note some known limitations? 🤔

mfn avatar Apr 09 '21 06:04 mfn