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

Async execution is not coherent. #2167

Closed
nrbnlulu opened this issue Sep 13, 2022 · 2 comments
Closed

Async execution is not coherent. #2167

nrbnlulu opened this issue Sep 13, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@nrbnlulu
Copy link
Member

nrbnlulu commented Sep 13, 2022

Describe the Bug

Mutating a parent result in child fields would not mirror in the parent field when using async resolvers.

MRE:

Strawberry

import strawberry
from typing import List, Optional

@strawberry.type
class Worm:
    name: str = "Billy"
    age: int = 2
    
    async def eat_apple(self, apple) -> None:
        apple.is_eaten = True
        
@strawberry.type
class Apple:
    name: str
    is_eaten: bool
    worms: Optional[List[Worm]]

    @strawberry.mutation
    async def eat(self) -> "Apple":
        await self.worms[0].eat_apple(self)
        return self
 
apple1 = Apple(name="Smith", is_eaten=False, worms=[Worm()])

@strawberry.type
class Query:
    @strawberry.field
    def apple(self) -> Apple:
        return apple1


@strawberry.type
class Mutation:
    @strawberry.field
    def apple_eater(self) -> Apple:
        return apple1

schema = strawberry.Schema(query=Query, mutation=Mutation)

Mutation

mutation {
  appleEater {
    name
    isEaten
    eat{
      name
      isEaten
    }
  }
}

result

{
  "appleEater": {
    "name": "Smith",
    "isEaten": false,
    "eat": {
      "name": "Smith",
      "isEaten": true
    }
  }
}

The isEaten field is not coherent. Note that with sync revolvers there is no issue.

System Information

  • Operating system: Any
  • Strawberry version (if applicable): tested on 0.130.3

Additional Context

playground:
https://play.strawberry.rocks/?version=0.130.3&gist=579e22e3452224b747cbe7c38fa68812

Related issues:
graphql/graphql-js#221
apollographql/apollo-server#3635

@nrbnlulu nrbnlulu added the bug Something isn't working label Sep 13, 2022
@jkimbo
Copy link
Member

jkimbo commented Sep 13, 2022

@nrbnlulu sync resolvers has the same result: https://play.strawberry.rocks/?version=0.130.3&gist=94e133ccbe4799fe778faf7eb2f09b4c

You can get it to be consistent by reordering the fields in the query like so: https://play.strawberry.rocks/?version=0.130.3&gist=8cfc52e86e6240186b97221831d593b0 but that is quite brittle. Also reordering doesn't work with async resolvers because the first level sync fields are evaluated before the async ones are: https://play.strawberry.rocks/?version=0.130.3&gist=8bba3c1b981d07b3ba411e27b7ba7438

This is precisely why nested mutations are a bad idea and not recommended btw. This is not a bug in Strawberry, it's an artefact of how the GraphQL execution algorithm works.

@jkimbo jkimbo closed this as completed Sep 13, 2022
@nrbnlulu
Copy link
Member Author

nrbnlulu commented Sep 13, 2022

Thanks.
This worked for some reason https://play.strawberry.rocks/?version=0.130.3&gist=26593024c34f4f151b7185ea2ee5e5a2
I just forgot to remove the async from the other one..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants