You need to enable JavaScript to run this app. Django定时任务调用GraphQL API的500报错与Mutation空值不匹配问题
优惠活动
大模型
产品
解决方案
定价
更多

Django定时任务调用GraphQL API的500报错与Mutation空值不匹配问题

Django 项目同步功能异常排查

项目基础说明

正在开发一套Django项目,可收集前端Django表单提交的注册信息(包含姓名、姓氏、邮箱等基础数据,以及标签类附加信息),并将这些信息同步到目标网站,目前遇到两类异常问题。


第一类异常:EP ID 获取定时任务运行报错

原本可正常工作的EP ID获取脚本,现在在cronjob_logs中抛出如下错误栈:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django_cron/management/commands/runcrons.py", line 71, in run_cron_with_cache_check
manager.run(force)
File "/usr/local/lib/python2.7/dist-packages/django_cron/init.py", line 215, in run
self.msg = self.cron_job.do()
File "/home/django/django_project/ogx/cron.py", line 31, in do
ep_id = get_ep_id(ep.email)
File "/home/django/django_project/ogx/graph_helper.py", line 75, in get_ep_id ''', {"query": email})
File "/usr/local/lib/python2.7/dist-packages/graphqlclient/client.py", line 11, in execute
return self._send(query, variables)
File "/usr/local/lib/python2.7/dist-packages/graphqlclient/client.py", line 34, in _send
raise e
HTTPError: HTTP Error 500: Internal Server Error

关联实现代码

  • graph_helper.py 中的 get_ep_id 实现:
def get_ep_id(email):
    client = GraphQLClient(
        '*this part I took off for confidentiality*')

    result = client.execute('''
        query Myquery($query: String!){
        allPeople(q:$query)
        {
            data
            {
              id
              full_name
                
            }
        }
    }
        
        ''', {"query": email})

    data = json.loads(result)
    if len(data['data']['allPeople']['data']) > 0:
        return data['data']['allPeople']['data'][0]['id']
    else:
        return None
  • cron.py 中对应的 FetchEPsIDs 定时任务实现:
class FetchEPsIDs(CronJobBase):
RUN_EVERY_MINS = 30  

schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'ogx.FetchEPsIDs'    # a unique code

def do(self):
    eps_query = mc_ogx_app.objects.filter(ep_id__isnull=True)
    for ep in eps_query:
        ep_id = get_ep_id(ep.email)
        ep.ep_id = ep_id
        ep.save()

第二类异常:标签同步功能失效

用于将表单收集的标签信息通过API同步到目标网站的定时任务,脚本运行无报错但未实现预期的数据同步效果。

关联实现代码

  • 定时任务 UpdateEpsTags 实现:
class UpdateEpsTags(CronJobBase):
RUN_EVERY_MINS = 30  

schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'ogx.UpdateEpsTags'    # a unique code

def do(self):
    access_token = 'taken out for confidentiality'
    eps_query = mc_ogx_app.objects.filter(ep_id__isnull=False, tags_uploaded=False)
    for ep in eps_query:
        if len(str(ep.ep_id)) >= 2:

            tags_list = []
            if ep.country_pref is not None:
                tags_list.append(ep.country_pref.tag_id)

            if ep.career_pref is not None:
                tags_list.append(ep.career_pref.tag_id)

            first_tags_list = return_user_tag_list(ep)

            tags_list = tags_list + first_tags_list

            if ep.product_ogv:
                tags_list.append([7])

            if ep.product_oge:
                tags_list.append([9])

            if ep.product_ogt:
                tags_list.append([8])
            try:
                update_ep_tags(int(ep.ep_id), tags_list, access_token,ep.chosen_ref)
                ep.tags_uploaded = True
                ep.save()
            except:
                ep.save()
  • 定时任务调用的GraphQL Mutation实现:
def update_ep_tags(person_id, tags_list, token,referral):
client = GraphQLClient(
    'taken out for confidentiality')
result = client.execute('''
    mutation Mymutation($persons: [Int]!, $tags: [Int]!,$id: ID!, $referral: String!){
        bulkTagUpdateForPeople(person_ids:$persons, tag_list_ids:$tags)
        {
            id
            full_name
            tag_lists
            {
              id
              name
            }
        }
        updatePerson(id:$id , person:
              {
                referral_type:$referral
                
              })
              {
                full_name
                referral_type
                programmes
                {
                  short_name
                }
              }
    }

    ''', {"persons": [person_id], "tags": tags_list, "id": person_id, "referral": referral})
return result

手动测试现象

手动在GraphQL客户端执行该Mutation时,抛出 Nullability mismatch on variable $id and argument id (ID / ID!) 的空值不匹配错误。当前需要排查上述所有问题的成因及解决方案。


内容的提问来源于stack exchange,提问作者Saad Mehdaoui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.24 05:06:03