strawberry-django-plus icon indicating copy to clipboard operation
strawberry-django-plus copied to clipboard

[Error] "children" lookup was already seen with a different queryset. You may need to adjust the ordering of your lookups.

Open ashleyxue529 opened this issue 1 year ago • 1 comments

I'm seeing the above error when running a mutation. It's just this specific case where I see this, most basic mutations work fine. The model setup looks like this:

@gql.django.type(Config)
class ConfigType(relay.Node):
    items: List["ItemType"]

@gql.django.type(Item)
class ItemType(relay.Node):
    label: auto
    children: List["ItemType"]

Mutation: Essentially deletes all the Items and recursively recreates them again with the new values

@gql.django.input_mutation
    def update_items(
        self,
        info: Info,
        config_id: gql.relay.GlobalID,
        items: Optional[List["ItemType"]],
    ) -> Config:
        try:
            config: Config = config_id.resolve_node(info, ensure_type=Config)

            config.items.all().delete()

            for item in items:
                parent = Item(
                    label=item.label,
                    config=config,
                )
                parent.save()
                update_items_recurse(
                    config=config, item=item, parent=parent
                )

            return cast(
                Brand,
                brand,
            )
        except Exception:
            raise RuntimeError("Unable to update navigation items")

def update_items_recurse(
    config: Config,
    item: ItemInputPartial,
    parent: Item,
):
    children = item.children
    if children is gql.UNSET or len(children) == 0:
        return

    for child in children:
        child_node = Item(label=child.label, parent=parent)
        child_node.save()
        update_items_recurse(
            config=config, item=child, parent=child_node
        )

I'm utilizing the Relay Store on the frontend to auto-update values on the Config object so I'm returning the full Config object in the mutation and re-querying the items field on the Config object on return of the mutation, but something is going wrong with the re-querying part. Maybe because I delete all objects on the config and re-create them?

const updateMutation = graphql`
  mutation ShortcutsCardNavigationItemsUpdateMutation(
    $input: UpdateNavigationItemsInput!
  ) {
    updateNavigationItems(input: $input) {
      ... on ConfigType {
        id
        items {
          label 
          children {
            label
            children { ... etc }
          }  
        }
    }
  }
`;

Hope that makes sense. In the meantime I'm going to try to write a custom resolver for the items field to see if that helps at all.

Update: If this helps, I tested this out instead and I see the same error:

@gql.django.type(Config)
class ConfigType(relay.Node):
    @gql.django.field
    def items(self) -> List["ItemType"]:
        return list(self.items.all())

Am I just doing something wrong in the original setup or is there a bug?

ashleyxue529 avatar Jan 18 '23 01:01 ashleyxue529