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

Same relationship aliased fails to resolve #717

Closed
edgarsn opened this issue Feb 26, 2021 · 3 comments
Closed

Same relationship aliased fails to resolve #717

edgarsn opened this issue Feb 26, 2021 · 3 comments

Comments

@edgarsn
Copy link
Contributor

edgarsn commented Feb 26, 2021

Versions:

  • graphql-laravel Version: master branch
  • PHP Version: 7.4.3

Description:

Querying same relationship twice with alias fails. I wrote test for it.

Steps To Reproduce:

See test
    public function testRelationshipQueryAlias(): void
    {
        /** @var User $user */
        $user = factory(User::class)->create();

        /** @var Post $postFlagged */
        $postFlagged = factory(Post::class)
            ->create([
                'flag' => true,
                'user_id' => $user->id,
            ]);

        /** @var Post $postNonflagged */
        $postNonflagged = factory(Post::class)
            ->create([
                'user_id' => $user->id,
            ]);

        $graphql = <<<'GRAQPHQL'
{
  users(select: true, with: true) {
    id
    name
    flagged: posts(flag: true) {
      body
      id
      title
    }
    nonflagged: posts(flag: false) {
      body
      id
      title
    }
  }
}
GRAQPHQL;

        $this->sqlCounterReset();

        $result = $this->httpGraphql($graphql);

        $expectedResult = [
            'data' => [
                'users' => [
                    [
                        'id' => (string) $user->id,
                        'name' => $user->name,
                        'flagged' => [
                            [
                                'body' => $postFlagged->body,
                                'id' => (string) $postFlagged->id,
                                'title' => $postFlagged->title,
                            ],
                        ],
                        'nonflagged' => [
                            [
                                'body' => $postNonflagged->body,
                                'id' => (string) $postNonflagged->id,
                                'title' => $postNonflagged->title,
                            ],
                        ],
                    ],
                ],
            ],
        ];
        $this->assertSame($expectedResult, $result);
    }
See output
1) Rebing\GraphQL\Tests\Database\SelectFields\QueryArgsAndContextTests\NestedRelationLoadingTest::testRelationshipQueryAlias
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
             0 => Array &3 (
                 'id' => '1'
                 'name' => 'Kade Oberbrunner'
-                'flagged' => Array &4 (
-                    0 => Array &5 (
-                        'body' => 'Sed autem eum error fugit in quod.'
-                        'id' => '1'
-                        'title' => 'Mr.'
-                    )
-                )
-                'nonflagged' => Array &6 (
-                    0 => Array &7 (
-                        'body' => 'Provident explicabo distinctio illo rerum a exercitationem et.'
-                        'id' => '2'
-                        'title' => 'Miss'
-                    )
-                )
+                'flagged' => Array &4 ()
+                'nonflagged' => Array &5 ()
             )
         )
     )
 )

If i change query to: (basically removing nonflagged)

{
  users(select: true, with: true) {
    id
    name
    flagged: posts(flag: true) {
      body
      id
      title
    }
  }
}

... and change expected result to:

$expectedResult = [
    'data' => [
        'users' => [
            [
                'id' => (string) $user->id,
                'name' => $user->name,
                'flagged' => [
                    [
                        'body' => $postFlagged->body,
                        'id' => (string) $postFlagged->id,
                        'title' => $postFlagged->title,
                    ],
                ],
            ],
        ],
    ],
];

test passes. Is it really a bug or am I missing something? :)

@edgarsn edgarsn added the bug label Feb 26, 2021
@edgarsn
Copy link
Contributor Author

edgarsn commented Feb 26, 2021

After some research, Laravel doesn't support eager load relationship aliasing unfortunatelly (I didn't pay attention previously on that one). Without that I don't think this is even possible, isn't it?

@crissi
Copy link
Contributor

crissi commented Mar 2, 2021

yes not possible easily, i made a hacked solution somewhere in closed prs.

Should be easy when/if this get implemented: laravel/ideas#2183

@mfn
Copy link
Collaborator

mfn commented Apr 5, 2021

I think @crissi is referring to #612 which was triggered by #604

Closing it for either a shortcoming we're not able to overcome and/or duplicate of #604

To OP: though I see you already made tests, I'm happy to include tests which assert the "undesired" state which in case in the future this gets fixed and those tests then break, serving as a reminder to update them.

@mfn mfn closed this as completed Apr 5, 2021
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

3 participants