graphqlite icon indicating copy to clipboard operation
graphqlite copied to clipboard

Unhelpful error message when a query is defined twice

Open lexmes opened this issue 4 years ago • 7 comments

Lets assume we have to following setup:

class ControllerA {
  /**
   * @Query()
   * @returns string[]
   */
  allPosts() : array;
}

...

class ControllerB {
  /**
   * @Query()
   * @returns string[]
   */
  allPosts() : array;
}

Right now the error message that i get is: assert($firstDuplicate instanceof FieldDefinition). Thats not very helpful. I would suggest something along the lines of cannot redeclare query "allPosts" in controller "<FQDN>\ControllerB" as it was already defined in <FQDN>\ControllerA.

I don't know if its my setup that is causing this error. But even then its not very helpful.

The error is generated in AggregateControllerQueryProvider.php:98.

lexmes avatar Jan 15 '21 13:01 lexmes

Interesting! Do you think you could write an integration test to showcase the problem?

For instance by duplicating this test (https://github.com/thecodingmachine/graphqlite/blob/c6177437bdac274fff975757cb04a0896015cb4d/tests/Integration/EndToEndTest.php#L1533-L1547) and creating a new namespace with both controllers?

moufmouf avatar Jan 21 '21 07:01 moufmouf

Yep, will give it a go :)

lexmes avatar Jan 21 '21 12:01 lexmes

I've tried to recreate the issue here: https://github.com/lexmes/graphqlite/commit/49274e38dc6136bae8921df2567e3029fec6a2cd

But the error message there is correct. Can you think of any Circumstance under which the assert($firstDuplicate instanceof FieldDefinition) expression in https://github.com/lexmes/graphqlite/blob/master/src/AggregateControllerQueryProvider.php#L98 will yield false?

lexmes avatar Mar 03 '21 12:03 lexmes

@lexmes I'm assuming you confirmed that assertions are enabled in the environment in which you tested? That's a PHP config setting. I not really sure that we should be using assert in this particular case.

oojacoboo avatar Mar 29 '21 15:03 oojacoboo

I have seen some errors from asserts in development, it's usually when queries/fields/names are duplicated

aszenz avatar Apr 29 '22 08:04 aszenz

Assert can be problematic b/c it doesn't provide any context to the assertion. And you took the time to write it. Just take a few seconds more to throw an exception with an intelligible message that helps the developer, or yourself later on.

oojacoboo avatar Apr 29 '22 08:04 oojacoboo

@lexmes can you create a PR for this please.

oojacoboo avatar Jun 12 '22 03:06 oojacoboo