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

Select * is preventing from groupping by for Interface types #683

Open
EdwinDayot opened this issue Oct 22, 2020 · 7 comments
Open

Select * is preventing from groupping by for Interface types #683

EdwinDayot opened this issue Oct 22, 2020 · 7 comments

Comments

@EdwinDayot
Copy link
Contributor

Versions:

  • graphql-laravel Version: 5.1.4
  • Laravel Version: 8.0
  • PHP Version: 7.4

Description:

The generated SQL for a group by on an interface would raise an error as * is selected instead of the group by fields

Steps To Reproduce:

<?php

class MyQuery extends Query {

    public function type()
    {
        return Type::listOf(GraphQL::type('anInterface'));
    }

    public function resolve($root, $args, $context, $resolveInfo, $getSelectFields)
    {
        $fields = $getSelectFields();
        $select = $fields->getSelect();
        $with = $fields->getRelations();

        $myResults = MyModel::select($select)->with($with)->groupBy('my_field')->get();
    }
}
<?php

class MyInterface extends InterfaceType {
    public function fields(): array
    {
        return [
            'my_field' => ['type' => Type::string()]
        ]
    }
}
<?php

class MyType extends Type {
    public function fields(): array
    {
        return $interface->getFields();
    }
}

With this kind of configuration, the SQL request would result in:

select * from my_table group by my_field;

And of course, it would raise an SQL error: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ...

Expectations

Only the required fields should be selected

@EdwinDayot EdwinDayot added the bug label Oct 22, 2020
@illambo
Copy link
Contributor

illambo commented Oct 22, 2020

Maybe the problem is this

if (is_a($parentType, UnionType::class) || is_a($parentType, \GraphQL\Type\Definition\InterfaceType::class)) {

@EdwinDayot
Copy link
Contributor Author

EdwinDayot commented Oct 22, 2020

I think it's just a part of a larger problem. Each Interface or Union should assume that every field defined in it are available in the database, but should use the alias and query defined in the type implementing the Interface or Union.

I have been working on it with the method

protected static function handleInterfaceFields(

But it does not support aliases. (apparently)

@illambo
Copy link
Contributor

illambo commented Oct 22, 2020

Great! May the force be with you :)

@mfn
Copy link
Collaborator

mfn commented Oct 22, 2020

Added the SelectFields label, seems like another shortcoming with this part.

@crissi
Copy link
Contributor

crissi commented Dec 12, 2020

I am taking a look at this, it seems that relationships are not working well either...

@crissi
Copy link
Contributor

crissi commented Apr 22, 2021

Tried to tackle this by using webonyx QueryPlan helper, which could ease this problem quite a bit, but since it still have unresolved issues, like this: webonyx/graphql-php#831, It will have to wait for now

@mfn
Copy link
Collaborator

mfn commented Apr 23, 2021

Does https://github.com/nuwave/lighthouse support? Not sure, might be worth checking out how they do it.

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

No branches or pull requests

4 participants