cfpinto / graphql

A GraphQL query builder class
MIT License
17 stars 9 forks source link

The Fragment example produces an error: Invalid method query in `src/Entities/Fragment.php:49` #33

Open MurzNN opened 1 month ago

MurzNN commented 1 month ago

When I copy-paste the Fragment example from the readme:

$fragment = new GraphQL\Entities\Fragment('properties', 'Hero');
$fragment->use('id', 'age');
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name', $fragment)->query();
echo $fragment->query();

it throws an error:

fragment properties on Hero {
    id
    age
}
{
    ()
    {
        name
        ...properties
    }
}PHP Fatal error:  Uncaught GraphQL\Exceptions\InvalidMethodException: Invalid method query in /var/www/html/vendor/graphql/src/Entities/Fragment.php:49
Stack trace:
#0 /var/www/html/vendor/graphql/src/tests.php(12): GraphQL\Entities\Fragment->__call('query', Array)
#1 {main}
  thrown in /var/www/html/vendor/graphql/src/Entities/Fragment.php on line 49

Fatal error: Uncaught GraphQL\Exceptions\InvalidMethodException: Invalid method query in /var/www/html/vendor/graphql/src/Entities/Fragment.php on line 49

GraphQL\Exceptions\InvalidMethodException: Invalid method query in /var/www/html/vendor/graphql/src/Entities/Fragment.php on line 49

Call Stack:
    0.0004     491928   1. {main}() /var/www/html/vendor/graphql/src/tests.php:0
    0.0141     723272   2. GraphQL\Entities\Fragment->__call($method = 'query', $arguments = []) /var/www/html/vendor/graphql/src/tests.php:12
MurzNN commented 1 month ago

Seems we need to replace it with ->toString() - is it right?

This one works well (also replaced deprecated query() to parse():

$fragment = new GraphQL\Entities\Fragment('properties', 'Hero');
$fragment->use('id', 'age');
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name', $fragment)->parse();
echo $fragment->toString();

If the modified example is okay - I can prepare a PR with this change.