php-graphql-oqm icon indicating copy to clipboard operation
php-graphql-oqm copied to clipboard

missing some functions with params in QueryObject classes (fix)

Open actionm opened this issue 5 years ago • 1 comments

I use this package with my private graphql endpoint, it generates more than 130 files. But the package generates the "TournamentQueryObject" class with some functions without params.

For example, the TournamentQueryObject->selectNameByLanguage() function should be selectNameByLanguage(string $lang)

It generates the query :

query {
  tournaments() {
    nameByLanguage
  }
}

But I need the query:

query {
  tournaments() {
    en:nameByLanguage ( lang : "en" )
  }
}

I think there is some uncompleted features in the package while parsing graphql schema, but I have an easy solution without breaking changes.

How to implement these features:

php:

  ->selectNameByLanguage()
    ->addPrefix('en')->addArguments(['lang' => 'en'])

Another example with the https://graphql-pokemon.now.sh endpoint

Can't generate a graphql query with the "new_id:id" prefix

query {
  pokemon(name: "Pikachu") {
    new_id:id 
    number
    name
  }
}

Can't generate a graphql query with the arguments

query {
  pokemon(name: "Pikachu") {
    new_id:id (argument_name: "argument_value")
    number
    name
  }
}

php:

->selectPokemon((new RootPokemonArgumentsObject())->setName('Pikachu'))
        ->selectId()
        ->selectNumber()
        ->selectName();

Easy solution:

php:

->selectPokemon((new RootPokemonArgumentsObject())->setName('Pikachu'))
        ->selectId()
        ->addPrefix('new_id')->addArguments(['argument_name' => 'argument_value'])
        ->selectNumber()
        ->selectName();

The addPrefix() function adds the prefix to the last selection element. The addArguments() function adds the arguments to the last selection element.

I have added these functions in the php-graphql-client pull request.

actionm avatar Aug 28 '19 17:08 actionm

@actionm I see the problem you're facing. From what you're describing, it seems that we're talking about 2 unsupported features:

  1. Support aliases for selection set
  2. Add the ability to set arguments for scalar selected fields

Both of them should be easily doable, I disagree with the way that you want to implement them though, let's discuss this.

I think that it makes more sense to add those options to the selection methods as arguments. Take for instance method selectNameByLanguage, you want to add methods to operate on the QueryObject class itself to support adding alias and arguments. The way I see it is that the selection methods themselves should contain one optional argument alias, which would be null by default, and another argument containing an optional arguments list, which would default to an empty array. This would be how the method signature will look after the fix:

public function selectNameByLanguage(string $alias = null, array $arguments = []) {` 

The alias and arguments would then be used to build the query.

That would be the quick solution. A better solution would be to parse the scalar fields parameters and generate an argument class for them but that would take longer, I'm guessing that you need a faster solution.

Let me know what you think about this.

mghoneimy avatar Aug 30 '19 18:08 mghoneimy