0

我正在使用包django graphql auth来处理 Django/Graphql 项目中的身份验证。我正在尝试将其设为UPDATE_MUTATION_FIELDS可选,以便在使用 updateAccount 突变时不必输入它们,但没有成功。

我的相关部分settings.py如下所示:

GRAPHQL_AUTH = {
    'LOGIN_ALLOWED_FIELDS': ['email', 'username'],
    'ALLOW_LOGIN_NOT_VERIFIED': False,
    'REGISTER_MUTATION_FIELDS': {
        'email': 'String',
        'username': 'String',
        'display_name': 'String',
        'country': 'String',
        'birth_date': 'Date',
    },
    'REGISTER_MUTATION_FIELDS_OPTIONAL': {
        'bio': 'String',
        'gender': 'String', 
        'is_mod': 'Boolean'
    },
    'UPDATE_MUTATION_FIELDS': {
        'display_name': 'String',
        'country': 'String',
        'birth_date': 'Date',
        'gender': 'String', 
        'bio': 'String',
        'is_mod': 'Boolean'
    }

相关models.py

   class User(AbstractBaseUser):

      is_mod = models.BooleanField(default=False)  
      display_name = models.CharField(_('Full name'), max_length=50)
      country = CountryField(blank_label=_('(select country)'))
      birth_date = models.DateField()
      bio = models.CharField(max_length=150, blank=True)

所以我决定使用自定义表单将这些字段显式设置为可选:

from graphql_auth.forms import UpdateAccountForm

class UpdateUserForm(UpdateAccountForm):
    # Mark fields as not required

    class Meta(UpdateAccountForm.Meta):
        pass

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for field in (fields := self.fields):
           fields[field].required = False

我的用户突变看起来像:

class UpdateAccount(relay.UpdateAccount):
    form = UpdateUserForm

class AuthRelayMutation(graphene.ObjectType):
    # update_account = relay.UpdateAccount.Field()
    update_account = UpdateAccount.Field()

最后,我将上面的突变用作:

# All Mutation objects will be placed here
class Mutation(AuthRelayMutation, graphene.ObjectType):
    debug = graphene.Field(DjangoDebug, name='__debug')

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

当我访问 graphql URL 时,出现错误Cannot create a consistent method resolution order (MRO) for bases InputObjectType, UpdateAccountInput

我该怎么做才能使要更新的字段成为可选字段?

4

0 回答 0