标签: laravel-lighthouse

Laravel Lighthouse - 按关系的属性对查询进行排序

使用如下所示的模式,是否有一种方法可以执行查询并按实体name的属性对结果进行排序JobType?我想要一个分页的作业列表,并按作业类型名称的字母顺序显示结果。

extend type Query @middleware(checks: ["auth:api"]) {
    jobs(orderBy: _ @orderBy): [Job!]! @paginate(defaultCount: 10, model: "App\\Job")
}

type Job {
    id: ID!
    description: String!
    job_type: JobType! @belongsTo
}

type JobType {
    id: ID!
    name: String!
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用该@builder指令,然后在构建器中使用联接以这种方式引入名称属性,但这似乎会导致实体 ID 出现一些问题,从而导致关系链接到错误的事物。

有任何想法吗?

php laravel graphql laravel-lighthouse

5
推荐指数
1
解决办法
2020
查看次数

在灯塔 graphql 文件中声明变量

我创建了一个像这样的graphql文件:

type Field {
    id: ID!
    name_en: String!
    name_fa: String!
    description: String!
    img_url: String!
    created_at: DateTime!
    updated_at: DateTime!

    subFields: [SubField] @hasMany
}

extend type Query {
    fields(input: ListInput @spread): [Field] @paginate(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field" defaultCount: 10)
    field(id: ID! @eq): Field @find(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
}

extend type Mutation {
    createField(
        name_en: String! @rules(apply: ["required"])
        name_fa: String
        description: String
        img_url: String
    ): Field @create(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")

    updateField(
        id: ID! @rules(apply: ["required", "int"])
        name_en: String @rules(apply: ["required"])
        name_fa: String
        description: String
        img_url: String
    ): Field @update(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field") …
Run Code Online (Sandbox Code Playgroud)

lighthouse laravel graphql laravel-lighthouse

5
推荐指数
1
解决办法
434
查看次数

尝试将 laravel lighthouse-php 与 Roadrunner Server 结合使用时遇到间歇性错误

  • 走鹃 1.8.1
  • 拉拉维尔:7.17.2
  • 灯塔:4.15.0

使用 NGINX 时,我的 GraphQL API/架构没有任何问题。然而,当我切换到 Roadrunner 时,我突然间歇性地收到如下错误:

"message": "Lighthouse failed while trying to load a type: MyType\n\nMake sure the type is present in your schema definition.\n",
    "exception": "Nuwave\\Lighthouse\\Exceptions\\DefinitionException",
    "file": "/var/www/wright/ams/vendor/nuwave/lighthouse/src/Schema/TypeRegistry.php",
    "line": 94
Run Code Online (Sandbox Code Playgroud)

有其他人经历过这个并找到解决方案吗?

我确实启用了 opcache,但即使禁用了它,我仍然遇到这个问题。

我运行了 php artisan lighthouse:validate-schema,它是有效的 我也运行了 php artisan lighthouse:clear-cache,但无济于事

php laravel laravel-lighthouse

5
推荐指数
1
解决办法
1865
查看次数

Laravel GraphQl 出现无法连接到 websocket 端点错误

实现一个简单的GraphQl查询(例如 simple)MutationSubscription,当我尝试将其运行subscriptiongraphql-playground

subscription {
   userCreated{
     name
     username
     password
   }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{
  "error": "Could not connect to websocket endpoint ws://127.0.0.1:8000/graphql. 
Please check if the endpoint url is correct."
}
Run Code Online (Sandbox Code Playgroud)

GraphQl 架构:

#...
type User {
    id: Int!
    name: String
    email: String
    product : [Product!]
}

type Mutation {
 createUser(
    name: String!
    password: String!
    email : String!
  ): User @field(resolver:"UserMutator@create") @broadcast(subscription: "userCreated")
}

type Subscription {
  userCreated: User
}

Run Code Online (Sandbox Code Playgroud)

UserMutator班级: …

laravel graphql laravel-lighthouse laravel-websockets

5
推荐指数
0
解决办法
353
查看次数

Laravel Lighthouse GraphQL - 在服务器端排序

嗨,我是 GraphQL 的新手,我正在尝试根据列内容对我的数据进行排序。我有一个可以发送的查询端点:

query {
  user(count:20, firstName:"Foo") {
    data {
      name
      region
      birthDate
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

结果是一个包含 20 个用户名的数组Foo。但我想通过birthDate. 我已经尝试了很多东西,但我就是想不通。我已经尝试过在 firstName之前sortorderBy之后,但我总是收到错误,例如:

{
  "errors": [
    {
      "message": "Unknown argument \"sort\" on field \"user\" of type \"Query\".",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 2,
          "column": 31
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我使用 Laravel Lighthouse 作为 GraphQL 的 wapper。我很惊讶我找不到有关如何执行此操作的任何信息。

我的查询:

type Query {
    user(firstName: String @eq): [User!]! @paginate(type: "paginator" model: "App\\User")
} …
Run Code Online (Sandbox Code Playgroud)

php laravel graphql laravel-lighthouse

4
推荐指数
1
解决办法
2237
查看次数

在 Router.php 第 366 行:传递给 Illuminate\Routing\Router::group() 的参数 1 必须是数组类型,

我已经从to更新了Laravel,现在我的应用程序无法运行。v5.7v5.8

我因为这个问题更新了它:composer require rebing/graphql-laravel failed

我解决了包不兼容问题,但现在 Laravel 崩溃了:

$ php artisan serve

In Router.php line 366:

  Argument 1 passed to Illuminate\Routing\Router::group() must be of the type array, string given, called in /var/www/masvino/Server-bak/vendor/laravel/framework/src/Illuminate/Support/Facades  
  /Facade.php on line 239
Run Code Online (Sandbox Code Playgroud)

我怀疑 Laravel 的团队改变了这种方法的坚定性:

照亮\路由\Router.php

    /**
     * Create a route group with shared attributes.
     *
     * @param  array  $attributes
     * @param  \Closure|string  $routes
     * @return void
     */
    public function group(array $attributes, $routes) // line 366
    {
        $this->updateGroupStack($attributes);

        // Once we …
Run Code Online (Sandbox Code Playgroud)

graphql-php laravel-5.7 laravel-5.8 laravel-lighthouse

4
推荐指数
2
解决办法
5010
查看次数

Laravel Lighthouse 配置 - 无法找到可发布的资源

我已将最新版本的 Lighthouse 安装到我的 Laravel 7 应用程序中,并且它在开箱即用的配置下运行良好。我现在想调整一些设置以提高安全性,因此根据我尝试使用以下命令的文档

php artisan vendor:publish --tag=lighthouse-config
Run Code Online (Sandbox Code Playgroud)

发布配置文件。但我收到以下错误:

Unable to locate publishable resources.
Publishing complete.
Run Code Online (Sandbox Code Playgroud)

没有 lighthouse.php 文件发布到 config 文件夹。任何想法如何解决这一问题?

php laravel laravel-lighthouse

4
推荐指数
1
解决办法
1281
查看次数

如何在 Laravel 缓存中缓存具有类似角色关系的 auth()->user() 以减少对 DB 的调用?

我正在构建一个使用lighthouse-php的应用程序。因为我不断地为不同的用户设置各种策略,所以我不断地在不同部分应用程序中查询具有角色关系的用户模型,因此,希望将用户存储在Redis数据库中并从那里查询。我阅读了在互联网上找到的几篇文章,例如:laravel-cache-authuser为 Laravel 创建缓存用户提供程序/caching-the-laravel-user-provider-with-a-decorator/,在这里查看了laravel-auth-user的代码,并有点理解这个概念,但很难深入理解 laravel 来找到合适的解决方案......

例如,我正在努力理解如何在 UserObserver 中的事件方法内存储User关系Role,很清楚如何使用一个模型而不是附加的关系来做到这一点。

我有一种感觉,我应该做这样的事情:

class UserObserver
{
    /**
     * @param User $user
     */
    public function saved(User $user)
    {
        $user->load('role');
        Cache::put("user.$user->id", $user, 60);
    }
}
Run Code Online (Sandbox Code Playgroud)

但通过这种方式,我对数据库进行了两次调用,而不是预先加载关系。我如何在事件参数中预加载关系。我尝试添加protected $with = ['role']以便始终加载子模型/关系。但无论如何,我都会对数据库进行更多调用以检索角色或检索用户和角色。

他是我的项目lighthouse-php中的一些简化代码示例。

模式.graphql:

type SomeType {
    someMethod(args: [String!]): [Model!] @method @can(ability: "isAdmin",  model: "App\\Models\\User")
}

type User {
    id: ID
    name: String
    role: Role @belongsTo
}

type Role {
    id: ID! …
Run Code Online (Sandbox Code Playgroud)

caching laravel graphql laravel-lighthouse

4
推荐指数
1
解决办法
2776
查看次数

使用 Laravel Lighthouse 的 Laravel 7 中的 CORS 错误

我有一个用 Laravel 和 Lighthouse-php(用于 GraphQL)构建的 API。我的客户端使用 Vue js 构建,并使用 Apollo 进行 graphQL 客户端实现。每当我提出请求时,我都会收到以下错误:

Access to fetch at 'http://localhost:8000/graphql' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Run Code Online (Sandbox Code Playgroud)

自然地,我继续安装 laravel-cors 包,但后来我意识到它是我的 Laravel 安装(7.2.2)默认提供的。这意味着它\Fruitcake\Cors\HandleCors::class已经添加到中间件数组中Kernel.php,并且 cors 配置文件已经在我的配置目录中。

经过一些谷歌搜索,我意识到我需要添加\Fruitcake\Cors\HandleCors::classroute.middleware …

cors laravel graphql vue-apollo laravel-lighthouse

3
推荐指数
1
解决办法
1871
查看次数

GraphQL 解析器使用 Laravel Lighthouse 返回字符串而不是 JSON

您好,我正在尝试创建一个返回 JSON 的解析器,我将此库与自定义标量一起使用https://github.com/mll-lab/graphql-php-scalars

我使用 JSON 标量,如下所示:

schema.graphql

input CustomInput {
    field1: String!
    field2: String!
}

type Mutation {
    getJson(data: CustomInput): JSON @field(resolver: "App\\GraphQL\\Mutations\\TestResolver@index")
}
Run Code Online (Sandbox Code Playgroud)

测试解析器.php

public function index($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
   
    $data = array('msg'=>'hellow world', 'trueorfalse'=>true);
    return \Safe\json_encode($data);
    
}
Run Code Online (Sandbox Code Playgroud)

GraphQL 游乐场

mutation{
 getJson(data: { field1: "foo", field2: "bar" })
}

------------ response------------
{
  "data": {
    "getJson": "\"{\\\"msg\\\":\\\"hello world\\\",\\\"trueorfalse\\\":true}\""
  }
}

Run Code Online (Sandbox Code Playgroud)

正如你所看到的,它返回一个字符串,而不是 JSON...我做错了什么?

php laravel graphql laravel-lighthouse

2
推荐指数
1
解决办法
5414
查看次数