Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix alias relationships not firing its own query #612

Conversation

crissi
Copy link
Contributor

@crissi crissi commented Mar 29, 2020

Summary

fixes #604

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • Existing tests have been adapted and/or new tests have been added
  • Add a CHANGELOG.md entry

Copy link
Collaborator

@mfn mfn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel confident doing a good review on this one.

This shows the absurd complexity we've to put upon us because of the concept of the SelectFields and once more shows me this approach goes into the wrong direction somehow…

Leads me to believe SelectFields should be a standalone component and not in this library TBH 😄

Mutating models scopes, adding macros to builders .. this somehow looks dangerous to me 🤷‍♀️

When I think about I would implement a dedicated PHP server with this library (i.e. never restart, a daemon) I can foresee thus causing problems. Is there no other way to solve this aliasing / double-query problem?

Not really sure how to move forward with this one TBH.


Leads me to believe SelectFields should be a standalone component and not in this library TBH 😄

To clarify this, I believe the proper approach is to use a dataloader concept to solve these things in general (that's what I'm doing in my projects, I'm not using SelectFields)

*/
public static function add(string $model, array $relationships): void
{
$model::addGlobalScope(new AddModelRelationshipScope($relationships));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Isn't that somehow dangerous (side-effects), permanently mutating the the models global scopes?

Copy link
Contributor Author

@crissi crissi Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel confident doing a good review on this one.

This shows the absurd complexity we've to put upon us because of the concept of the SelectFields and once more shows me this approach goes into the wrong direction somehow…

Leads me to believe SelectFields should be a standalone component and not in this library TBH 😄

Mutating models scopes, adding macros to builders .. this somehow looks dangerous to me 🤷‍♀️

When I think about I would implement a dedicated PHP server with this library (i.e. never restart, a daemon) I can foresee thus causing problems. Is there no other way to solve this aliasing / double-query problem?

Not really sure how to move forward with this one TBH.

Leads me to believe SelectFields should be a standalone component and not in this library TBH 😄

To clarify this, I believe the proper approach is to use a dataloader concept to solve these things in general (that's what I'm doing in my projects, I'm not using SelectFields)

I love the SelectFields and use it for 99% of my queries, makes it so easy to set up and think it is one of the main "selling points" of the libary

Copy link
Contributor Author

@crissi crissi Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Isn't that somehow dangerous (side-effects), permanently mutating the the models global scopes?

yes it is definitely a side effect, but since SelectFields are made to be used with eloquent's ->with(). The only way to fix this is to generate the relationships.

The side effect is minimized by generating relationships with generated in front of all generated relationships, so I would say this makes it as safe as possible and only applied if alias are actually used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we might need to tore the relationship down if some one uses the same relationship again

public function extend(Builder $builder): void
{
foreach ($this->relationships as [$graphqlAlias, $relationship]) {
$builder->macro($graphqlAlias, function (Builder $builder) use ($relationship) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

/**
* @var array<class-string<Model>,array<string,string>>
*/
private static $modelRelationshipsForAlias = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wish this wouldn't be static.

I understand from the code it's cleaned up, still…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah me too

@mfn
Copy link
Collaborator

mfn commented Apr 9, 2020

@crissi not sure if/how you want to move forward here.

Besides all the complexity, for me the current no-go definitely are the side-effects of mutating the models behaviour (and also the macro thing); I don't think this is acceptable to be done from such a library.

@crissi
Copy link
Contributor Author

crissi commented Apr 9, 2020

I see you point, but people would expect aliases to work since they are a feature in graphql.

could we make it an opt in feature in the config? with a good description in documentation of what the risk of the feature could be.

I want to tighten it up, after some of the cases you mention in the comments.

@mfn
Copy link
Collaborator

mfn commented Apr 10, 2020

Sorry to sound stubborn, but as long as the code mutates the models I don't want to accept this (config or not; and I think it's a bad idea to use a "config switch" for that).

@crissi
Copy link
Contributor Author

crissi commented Apr 10, 2020

Sorry to sound stubborn, but as long as the code mutates the models I don't want to accept this (config or not; and I think it's a bad idea to use a "config switch" for that).

fair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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