Graphql-laravel: Example of a clash: translatable trait

Created on 29 Nov 2019  ·  9Comments  ·  Source: rebing/graphql-laravel

I have an interesting case in which things clash. I'm using laravel-translatable which has a trait that allow my attribute name from my model Species to be called normally $species->name but behind the scene it will go fetch a name in species_translations table where all translatable attributes are.

So when using my SelectFields $fields it will fail my database query while resolving species.name does not exists.

Versions:

  • graphql-laravel Version: 3.1.0
  • Laravel Version: 5.8.24
  • PHP Version: 7.1.3

Question:

How can I bridge that? How can I take advantage of SelectFields in this case?

PS: Invite link to the slack is expired

question

All 9 comments

PS: Invite link to the slack is expired

Ops, thanks! Tracking via https://github.com/rebing/graphql-laravel/issues/559

So when using my SelectFields $fields it will fail my database query while resolving species.name does not exists.

I assume to make proper use of that translation trait, one has to always fetch all columns instead of specific ones?

Yes that's correct. It's either set with $with on the model or done while querying like Species::translated()->get();.

The species.name column doesn't exist, it's species_translations.name. When doing $species->name it takes the current user locale and then go fetch species_translations.name WHERE locale = en

Hmm, can only think of two solutions:

  • not using SelectFields
  • add yet-another-option to SelectField to basically ignore individually select columns completely

ignore individually select columns completely

I once got the feedback at https://github.com/webonyx/graphql-php/issues/481#issuecomment-495967560 that it's also problematic to do this and "overfetching" is used to describe the problem space between the GraphQL consumer and the backend.
I.e. it shouldn't probably be done within the backend itself.

Personally I also see partial fetching on Eloquent models very problematic because there's no definition of field dependencies inside models and you have to replicate this business logic now in another layer (GraphQL in this space).

That's also one of the reasons why I:

  • except in special cases, always fetch all Eloquent model columns (this is GraphQL unrelated)…
    If you pass around the models in the application it becomes unmaintenable to figure out what columns are necessary in which context
  • …but also actually don't use SelectFields at all. I either build queries upfront (not so much better though) or use a data loader buffer and defer resolve relations (it's mostly about relations and relations thereof, etc.)

TL;DR: I'm not aware this is solvable with SelectFields as it exists and probably would require an option to always select all fields (I'm happy to be corrected here)

add yet-another-option to SelectField to basically ignore individually select columns completely

Thanks @mfn, this is probably gonna be my option OR just completely ignore the Eloquent model and build a custom field that resolve with a SQL query with a JOIN to translation tab depending on the locale.

this is probably gonna be my option

PRs are gladly welcome!

this is already supported if I understand your problem correctly, see here:
selectable needs to be set to false.
See example for isMe-field here, https://github.com/rebing/graphql-laravel#creating-a-query

Oi, right.

One day I need to refactor the readme and collection all the SelectFields specific docs in on area so I don't forget about the features myself 🤦‍♂

I believe this resolves the problem! Let me know otherwise

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edgarsn picture edgarsn  ·  4Comments

kirgy picture kirgy  ·  8Comments

shengslogar picture shengslogar  ·  6Comments

timothyvictor picture timothyvictor  ·  3Comments

jglover picture jglover  ·  6Comments