lighthouse: [Lumen] Working with relations - Call to undefined function Nuwave\Lighthouse\resolve()

Hi all,

I’m trying to get relations working following the video tutorial (and any other info I can find), but I keep getting the following error: Call to undefined function Nuwave\Lighthouse\resolve()

I have the following configuration in place (simple Room -> Comments relation): App\GraphQL\Schemas\pms.graphql

type Room {
  id: ID! @globalId
  name: String
  available: String
  reference: String
  details: String
  location: String
  comments: [Comment!]! @hasMany
}
type Comment {
  id: ID! @globalId
  reference: String
  title: String
  content: String
  created_at: DateTime
  room: Room! @belongsTo
}

App\GraphQL\Queries\Rooms.php

<?php

namespace App\GraphQL\Queries;

use Nuwave\Lighthouse\Support\Schema\GraphQLResolver;
use Illuminate\Auth\Access\AuthorizationException;
use App\Models\Room\Room;

class Rooms extends GraphQLResolver
{

    public function resolve()
    {
        if(!$this->context || !$this->context->user){
            throw new AuthorizationException('Unauthorized');
            return null;
        }

        // query
        $roomsQuery = Room::query();

        return $roomsQuery
            ->where('reference', 'NOT LIKE', '%9999')
            ->orderBy('lft')
            ->get()
            ;
    }
}

App\GraphQL\schema.graphql

#import ./Schemas/pms.graphql

scalar DateTime @scalar(class: "DateTime")

type ResponsesPayload {
  success: Boolean!
  msg: String
  payload: String
}

type Query @group(middleware: ["auth:api", "scopes:graphql"]) {
  rooms: [Room!]!
}

As soon as I try the following query, I get the forementioned error

query getRooms {
  rooms {
    id
    name
    available
    comments {
      reference
      title
      content
    }
  }
}

I’m probably overlooking something obvious here… I still have some other questions related to this:

  • how do I declare a different model name in the .graphql type definition? (eg. I have a App\Model\Room\Comment and a App\Model\Booking\Comment model) -> can I add eg type Comment @model(model: "App\\Models\\Room\\Comment") { ... }
  • how should I go about structuring the various graphql files? I can’t seem to define multiple Query entries when I try to separate them into different files
  • how can I learn the ins and outs about the various schema elements? I went through the various test files to look for examples but I couldn’t find eg. the uses of the @model directive
  • when creating Query classes, should I extend GraphQLResolver or GraphQLQuery

Thanks again for this great library, I hope I can get rolling with it very soon!

Kind regards,

Erik

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 28 (28 by maintainers)

Most upvoted comments

Hey @4levels,

From the looks of the error it seems that Lighthouse doesn’t know where to look to get the initial set of rooms. You just need to add a @field to the Query type like so:

type Query @group(middleware: ["auth:api", "scopes:graphql"] namespace: "App\\GraphQL\\Queries") {
  rooms: [Room!]! @field(resolver: "Rooms@resolve")
}

The namespace argument isn’t required

As for the other questions

  • GraphQL only allows you to create uniquely named types. You could create an interface with the name Comment and name the others RoomComment and BookingComment or just rename the two.
  • You can extend the Query type in different files like so:
# schema.graphql
type Query {
  # ...
}
# rooms.graphql
extend type Query {
  @group(middleware: ["auth:api") {
    rooms: [Room!]!
  }
}
  • The docs are a work in progress but they list many of the directives. The @model is only used if you are using Relay with the Node query, which allows you to resolve any node just by using it’s globalId.
  • Nope, they’re just regular classes!

Hope that helps!