2

Sorry, i'm fairly new to graphQL and lighthouse. I'm not sure if this is a bug, or if i missed something in the docs. I used the default paginator resolver and queries worked fine. However i needed to add some parameters, so followed the documentation to build my own... builder. Two of my fields have custom types. There is no problem when using the default paginator, but using my custom builder, i get errors like this

Expected a value of type "ViewingRequestStatus" but received: 3

i'm thinking this has something to do with using the regular DB Builder instead of the eloquent one, but the paginator requires the regular DB builder.

Schema

type ViewingRequest @model(class: "\\AppViewingRequestRead"){
    uuid: String!
    requestorUuid: String!
    scheduleUuid: String!
    districtUuid: String!
    congregationUuid: String!
    siteUuid: String
    status: ViewingRequestStatus
    language: Languages
    user_uuid: String
    streamEventUuid: String!
}

type Languages{
    name: String
    id: Int
}

extend type Query {
    viewingRequests(
        streamEventUuid: String, 
        scheduleUuid: String, 
        requestorUuid: String
    ): [ViewingRequest!]! @paginate(model: "App\\ViewingRequestRead", builder: "App\\ViewingRequestRead@showRequests")
}

App\ViewingRequestRead

class ViewingRequestRead extends IncRead
{
    protected $enumCasts = [
        'status' => ViewingRequestStatus::class,
        'language' => Languages::class,

    ];

    protected $casts = [
        'status' => 'int',
        'language' => 'int'
    ];

    public function showRequests($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
    {
        return DB::table('viewing_requests')
        ->when(key_exists("streamEventUuid", $args), function ($q) use ($args) {
            return $q->where('streamEventUuid', '=', $args["streamEventUuid"]);
        })->when(key_exists("scheduleUuid", $args), function ($q) use ($args) {
            return $q->where('scheduleUuid', '=', $args["scheduleUuid"]);
        })->when(key_exists("requestorUuid", $args), function ($q) use ($args) {
            return $q->where('requestorUuid', '=', $args["requestorUuid"]);
        });

ViewingRequestEnum (IncEnum Extends BenSampo\Enum\Enum) as noted in lighthouse docs.

<?php

namespace App\Enums;

use App\Enums\IncEnum;


final class ViewingRequestStatus extends IncEnum
{
    const Archived = 0;
    const New =   1;
    const LocaleSubmitted =   2;
    const DistrictSubmitted = 3;
    const LocaleApproved =   4;
    const DistrictApproved = 5;
    const CentralApproved = 6;
    const Cancelled = 7;
    const Denied = 8;
    const Draft = 9;
}

This is registered via GraphQlServiceProvider.

Output/Logs

Click to expand

"debugMessage" => "Expected a value of type "Languages" but received: 1"
      "message" => "Internal server error"
      "extensions" => array:1 [
        "category" => "internal"
      ]
      "locations" => array:1 [
        0 => array:2 [
          "line" => 8
          "column" => 21
        ]
      ]
      "path" => array:4 [
        0 => "viewingRequests"
        1 => "data"
        2 => 2
        3 => "language"

Environment

Lighthouse Version: 4.11.0 Laravel Version: 7.5.1

aibarra
  • 409
  • 1
  • 7
  • 17
  • Well according to the error, you have problem with casting ViewingRequestStatus enum. Did you add `use CastsEnums;` in your `ViewingRequestRead` class? It seems that you missed usage of this trait, therefore attributes are still integers, and not casted to enum – lorado Apr 24 '20 at 22:31
  • yes, its there. it works fine with the regular paginator. When i add the builder. It fails. My guess is that the builder is the DB Builder, not the eloquent one. I'm not sure what happens behind the scenes when it comes to casting with the package, but that difference seems to be a part of it. I ended up going with a scope instead. I'll post my solution later today, saw similar questions and issues pop up on github as well. – aibarra Apr 25 '20 at 18:41

0 Answers0