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

[BUG] documentation how to use graphql schemas in unittests #1976

Closed
devkral opened this issue Jan 22, 2024 · 1 comment · Fixed by #1981
Closed

[BUG] documentation how to use graphql schemas in unittests #1976

devkral opened this issue Jan 22, 2024 · 1 comment · Fixed by #1981
Assignees
Labels
Priority: High Important but not urgent Specification: GraphQL Specific to GraphQL Type: Bug Errors or unexpected behavior

Comments

@devkral
Copy link
Contributor

devkral commented Jan 22, 2024

Describe the bug

How can I use a graphql schema in unittests (not pytest)? I think it is nearly the same with openapi schemas, except you could extract portions of it and feed it to given. There is no method to convert a graphql schema into search strategy.

class Foo(TestCase):
  @given(dcase=schema)
  def test(self, dcase):
     dcase.call_and_validate()

fails

class Foo(TestCase):
  @given(dcase=schema["POST"]["/graphql/"])
  def test(self, dcase):
     dcase.call_and_validate()

does only extract either queries or mutations but there is no control what kind of API Operations are extracted, it seems to be random.

Ah yeah and the call_and_validate() does not check if the response is actually json and has errors but this is a different issue ;)

@devkral devkral added Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior labels Jan 22, 2024
@Stranger6667
Copy link
Member

I am going to update the Unittest support section of the docs on this matter, but it is similar to Open API - you need to use the as_strategy method to convert API operation to a strategy.

Another component of it is the way operations are accessed. In Open API it is schema[path][method], and now GraphQL follows the same pattern, but as GraphQL has just a single path & method, the access is bugged. I.e. Schemathesis breaks down a GraphQL API by query / mutation + field name, but when schema[path][method] happens, it always returns the latest one (in terms of iterating over query, mutation and fields definition order).

So, this is a bug. I'll fix it, to it works like schema["<query-type-name>"]["<field-name>"].

Thank you for pointing this out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: High Important but not urgent Specification: GraphQL Specific to GraphQL Type: Bug Errors or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants