0

when I am querying for the job_seeker profile and If there is no job_seeker data then I was getting an error JobSeeker models query does not exist. I instead want to pass empty list if there is no data. For this, i tried the following way but i am getting an error so could not pass the custom response

class JobSeekerNode(DjangoObjectType):
    class Meta:
        model = models.JobSeeker
        interfaces = (relay.Node, )

class JobSeekerQueries(ObjectType):
    job_seeker = Field(JobSeekerNode)

    def resolve_job_seeker(self, info, **kwargs):
        data = {}
        if info.context.user.is_authenticated:
            try:
                profile = Profile.objects.get(user=info.context.user)
                try:
                    job_seeker = models.JobSeeker.objects.get(profile=profile)
                    data['job_seeker'] = job_seeker
                except:
                    # when there's no row instead of passing error, pass empty list
                    data['job_seeker'] = []
                return JsonResponse(data)
            except Profile.DoesNotExist:
                return []
        return None

this is the error i get when trying to pass the custom response(empty list if there's no data)

{
  "errors": [
    {
      "message": "Received incompatible instance \"<JsonResponse status_code=200, \"application/json\">\"."
    }
  ],
  "data": {
    "job_seeker": null
  }
}

I even tried this one

def resolve_job_seeker(self, info, **kwargs):
    if info.context.user.is_authenticated:
        try:
            profile = Profile.objects.get(user=info.context.user)
            try:
                job_seeker = models.JobSeeker.objects.get(profile=profile)
                return job_seeker
            except:
                return models.JobSeeker.objects.none()
        except Profile.DoesNotExist:
            return []
    return None

still i am getting such issue

{
  "errors": [
    {
      "message": "Received incompatible instance \"<QuerySet []>\"."
    }
  ],
  "data": {
    "job_seeker": null
  }
}
Serenity
  • 3,884
  • 6
  • 44
  • 87

1 Answers1

0

if JobSeeker is a model then it should be JobSeeker.objects.filter. convention

You can't return custom things define in your query object. You are calling get method. objects.get() returns a single object instead of a list. A simpler solution would be calling filter()

job_seeker = JobSeeker.objects.filter(profile=profile)
return job_seeker

If there are not any matching profile it will return an empty list. In both cases just return the job seeker. There is no need for using an extra field like data if this is the thing you want.

def resolve_job_seeker(self, info, **kwargs):
    if info.context.user.is_authenticated:
        try:
            profile = Profile.objects.get(user=info.context.user)
            job_seeker = JobSeeker.objects.filter(profile=profile)
            return job_seeker
        except Profile.DoesNotExist:
            raise Exception("Profile is not created for this user")
    raise Exception("user not logged in")
user10095818
  • 121
  • 1
  • 1
  • 8