strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.92k stars 516 forks source link

Execution Context errors #3439

Open patrick91 opened 5 months ago

patrick91 commented 5 months ago

What is self.execution_context.errors on an extension, is it needed? (we have errors on the result)

Related: https://github.com/StellateHQ/stellate-strawberry/pull/6

Upvote & Fund

Fund with Polar

DoctorJohn commented 2 months ago

Good point. I just spend some time looking at the code. In a nutshell: execution_context.result is not available during the execution of early extension hooks such the parsing hook.

Longer explanation:

During execution, the execution context is first created in the schema.execute and schema.execute_sync methods. At this point the execution_context.result is still None.

https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/schema/schema.py#L257-L264

Next the operation, parsing and validation hooks of all extensions are called. If they catch any errors (for example while parsing the query) they add it to execution_context.errors because executon_context.result(.errors) is not available yet.

https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/schema/execute.py#L103-L117

One all extension hooks are done execution_context.errors is copied to execution_context.result.errors:

https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/schema/execute.py#L173-L177

I agree it would be much cleaner and less error-prone if only one of them would be available to extensions.