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

resolve func for Reverse Foreign Key Set do not work #1187

Closed
Rainshaw opened this issue Apr 15, 2021 · 2 comments
Closed

resolve func for Reverse Foreign Key Set do not work #1187

Rainshaw opened this issue Apr 15, 2021 · 2 comments
Labels

Comments

@Rainshaw
Copy link
Contributor

Rainshaw commented Apr 15, 2021

Hello, I have two models:

class Department(models.Model):
    department = models.CharField(max_length=100)
class User(models.Model):
    username = ...
    department = ForeignKey(to=Department, on_delete=models.DO_NOTHING,)

And I define two types:

class DepartmentType(DjangoObjectType):
    class Meta:
        model = Department

    def resolve_user_set(root, info, **kwargs):
        if info.context.user.is_authorization:
            return self.user_set
        return None

class UserType(DjangoObjectType):
    class Meta:
        model = User


class Query(graphene.ObjectType):
    departments = DjangoFilterConnectionField(DepartmentType)

Then I use the query without authorization:

query{
departments{
    edges{
      node{
        id
        department
        userSet{
          edges{
            node{
              id
              name
            }
          }
        }
      }
    }
  }
}

but the resolve func run without taking effact(with print func I can see that it return None). The response each have all users.

{
  "data": {
    "departments": {
      "edges": [
        {
          "node": {
            "id": "RGVwYXJ0bWVudFR5cGU6MQ==",
            "userSet": {
              "edges": [
                {
                  "node": {
                    "id": "VXNlclR5cGU6MQ=="
                  }
                },
                {
                  "node": {
                    "id": "VXNlclR5cGU6Mg=="
                  }
                },
                {
                  "node": {
                    "id": "VXNlclR5cGU6Mw=="
                  }
                }
              ]
            }
          }
        },
        {
          "node": {
            "id": "RGVwYXJ0bWVudFR5cGU6Mg==",
            "userSet": {
              "edges": [
                {
                  "node": {
                    "id": "VXNlclR5cGU6MQ=="
                  }
                },
                {
                  "node": {
                    "id": "VXNlclR5cGU6Mg=="
                  }
                },
                {
                  "node": {
                    "id": "VXNlclR5cGU6Mw=="
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

I have tried #1111 and #1133, but they all not work.... what should the resolver func return? or is this a bug?

@houdabelefqih
Copy link

@RainshawGao hey there! could you please provide more info on what is_authorization actually is?
In case the user is not "authorized", I believe the correct return type should be an empty queryset not None i.e in your case: User.objects.none()

@Rainshaw
Copy link
Contributor Author

Maybe you are right but I'll not test it, for I didn't use this project any more, so close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants