【问题标题】:Cannot declare class, because the name is already in use无法声明类,因为该名称已在使用中
【发布时间】:2020-06-21 13:54:19
【问题描述】:

我正在尝试从我的 GraphQL 获取数据,但它返回此错误:

{
  "message": "Cannot declare class App\\GraphQL\\Mutations\\AcceptBooking, because the name is already in use",
  "exception": "Symfony\\Component\\Debug\\Exception\\FatalErrorException",
  "file": "/Users/stevenvaneck/projects/hihiguide/app/Http/GraphQL/Mutations/AcceptBooking.php",
  "line": 14,
  "trace": []
}

AcceptBooking.php

<?php

namespace App\GraphQL\Mutations;

use Auth;
use App\Booking;
use App\Http\Controllers\Email\MailType;
use GraphQL\Type\Definition\ResolveInfo;
use App\Http\Controllers\Email\MailController;
use App\Jobs\Mail\Traveler\SendNewAvailableMail;
use App\Jobs\SMS\SendNewAvailableSMS;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class AcceptBooking
{
    /**
     * Return a value for the field.
     *
     * @param null $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param array $args The arguments that were passed into the field.
     * @param GraphQLContext|null $context Arbitrary data that is shared between all fields of a single query.
     * @param ResolveInfo $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     *
     * @return mixed
     */
    public function resolve($rootValue, array $args, GraphQLContext $context = null, ResolveInfo $resolveInfo)
    {

      //my code

    }
}

如果我将类的名称更改为随机名称,它再次表示我给它的新名称正在使用中。

在我的 schema.graphql 中

type Mutation {
    acceptBooking(data: AcceptInput): String @middleware(checks: ["auth:api"]) @field(resolver: "App\\Http\\GraphQL\\Mutations\\AcceptBooking@resolve")
    declineBooking(data: DeclineInput): String @middleware(checks: ["auth:api"]) @field(resolver: "App\\Http\\GraphQL\\Mutations\\DeclineBooking@resolve")
}

我在 Laravel 项目中使用 Lighthouse-php。

【问题讨论】:

  • 听起来文件被多次包含(必需)。
  • 在终端尝试composer dump-autoload
  • 请在您的问题中包含错误消息的文本。

标签: php laravel graphql laravel-lighthouse


【解决方案1】:

我的命名空间路径不正确

namespace App\GraphQL\Mutations;

必须是

namespace App\GraphQL\Http\Mutations;

【讨论】:

  • 遇到了同样的问题,因为我的班级在一个子文件夹中。上下文:Laravel 7 视图组件。我喜欢管理不同事物的子文件夹中的视图。如果您手动移动,必须小心命名空间
猜你喜欢
  • 2021-08-17
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
相关资源
最近更新 更多