nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.35k stars 438 forks source link

Can not change test schema in a single test method with UsesSchemaTest trait #1569

Closed robsontenorio closed 4 years ago

robsontenorio commented 4 years ago

Describe the bug

https://lighthouse-php.com/4.16/testing/extensions.htm

Cant override schema as described on docs, when using sucessive graphql requests.

Expected behavior/Solution

It should override schema.

Steps to reproduce

See PR https://github.com/nuwave/lighthouse/pull/1568

Lighthouse Version

v5.alpha4

spawnia commented 4 years ago

The approach described in the docs actually works, but we might clarify that changing after initialization does not work.

We could make it so the test schema is recompiled on every call to $this->graphQL(), but I am worried that might be costly in terms of performance.

robsontenorio commented 4 years ago

@spawnia can you give some directions to achieve that?

I am considering to create my own trait. Once i am working on Lighthouse Dashboard package i need exactly this behavior on my testing case. Because i need detect changes on schema on sucessive requests.

spawnia commented 4 years ago

How about splitting your test methods so they are focused and do one thing? Dynamically changing a schema in the lifecycle of a request has not come up as a real world use case, so I am hesitant to support it.

robsontenorio commented 4 years ago

In my testing scenarios the source of true is always a schema.graphl , so the internal state of dashboard (database) is filled with this info. I am following acceptance approach on tests.

But, I am not proposing this addition into Lighthouse core, because it is expensive. If you can point me how to “refresh” the schema on every single request I can put this logic in my own trait for only testing proposes.

robsontenorio commented 4 years ago

What I have tried, but no lucky:

robsontenorio commented 4 years ago

Solved by adding this to my TestCase.php

protected function rebuildTestSchema() {
        $this->app->extend(SchemaSourceProvider::class, fn () => new TestSchemaProvider($this->schema));
}

Now i can switch schemas on single test method.

$this->schema = '...initial...'

$this->graphQL(...)

$this->schema = '...changed...'

// new helper
$this->rebuildTestSchema();

// now this detects schema changes
$this->graphQL(....)