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

Printing raw query for debugging #74

Open
MarcoCazzaro opened this issue Mar 29, 2022 · 7 comments
Open

Printing raw query for debugging #74

MarcoCazzaro opened this issue Mar 29, 2022 · 7 comments

Comments

@MarcoCazzaro
Copy link

Hi! Given a populated Query or QueryBuilder object, is it possible to print the raw query that generated the request?
E.G.:

$builder = (new QueryBuilder('pokemon'))
    ->setArgument('name', 'Pikachu')
    ->selectField('id')
    ->selectField('name')
    ->selectField(
        (new QueryBuilder('attacks'))
            ->selectField(
                (new QueryBuilder('special'))
                    ->selectField('name')
                    ->selectField('type')
                    ->selectField('damage')
            )
    )
    ->selectField(
        (new QueryBuilder('evolutions'))
            ->selectField('id')
            ->selectField('name')
            ->selectField('number')
            ->selectField(
                (new QueryBuilder('attacks'))
                    ->selectField(
                        (new QueryBuilder('fast'))
                            ->selectField('name')
                            ->selectField('type')
                            ->selectField('damage')
                    )
            )
    );
try {
    $results = $client->runQuery($builder);
}
catch (QueryError $exception) {

    var_dump($builder->getRawQuery()); // <<<< THIS

    print_r($exception->getErrorDetails());
    exit;
}

This would be very useful when something goes wrong and you need the raw query to test it on a GraphQL desktop client (Altair in my case).
Thanks!

@omarherri
Copy link

I think you can achieve this easily with this command:

$builder->__toString();

@MarcoCazzaro
Copy link
Author

I'm afraid that command will just print the graphical representation of the PHP class, not the GraphQL code we can use in an external tool.

@omarherri
Copy link

No i tested this. it shows the current query generated by your builder. Just give it a try.

@MarcoCazzaro
Copy link
Author

I got this error:

PHP Fatal error:  Uncaught Error: Call to undefined method GraphQL\QueryBuilder\QueryBuilder::__toString()

@omarherri
Copy link

I see because you are using QueryBuilder. This is an example i'm using right now:

$gql = (new Query('page'))
                ->setArguments(['where' => new RawObject('{id: "'.$id.'"}')])
                ->setSelectionSet(
                    [
                        'title',
                        'publishedAt',
                        'updatedAt',
                        (new Query('content'))->setSelectionSet(['html']),
                        (new Query('seo'))
                            ->setSelectionSet(
                                [
                                    'title',
                                    'description',
                                    'noIndex',
                                    (new Query('image'))->setSelectionSet(['url'])
                                ]
                            )
                    ]
                );
dd($gql->__toString());

@MarcoCazzaro
Copy link
Author

Yeah I am using QueryBuilder widely in my setup, so I'd need this feature for QueryBuilder as well. Of course it's a "nice to have". ;)

@eiriksm
Copy link

eiriksm commented Nov 5, 2023

You could just get the query from the query builder? Like so?

catch (QueryError $exception) {

    var_dump((string) $builder->getQuery()); // ...or __toString like suggested. I like this way though

    print_r($exception->getErrorDetails());
    exit;
}

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

No branches or pull requests

3 participants