Laravel/ Lighthouse GraphQL - 如何通过 slug 属性查找?
Laravel/ Lighthouse GraphQL - how to find by slug attribute?
我正在为 Nuxt 应用程序创建 Laravel 后端,并且正在使用 Lighthouse-PHP 通过 GraphQL 传送数据。
我正在尝试设置查询以通过 slug 而不是默认 ID 进行查找。当我在 graphql 游乐场 运行 查询时,它正确 returns 数据,但是当我在前端加载页面时,它 returns null
.
后端
schema.graphql
type Query {
forumCategoryBySlug(slug: String! @eq): ForumCategory @find
}
type ForumCategory {
id: ID!
title: String!
description: String!
slug: String!
subcategories: [ForumSubcategory!]! @hasMany
created_at: DateTime!
updated_at: DateTime!
}
ForumCategory.php 型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ForumCategory extends Model
{
protected $fillable = [
'title', 'description', 'slug'
];
public function subcategories(): HasMany
{
return $this->hasMany(ForumSubcategory::class);
}
}
前端
Index.vue
import categoryQuery from '~/apollo/queries/forum/category.gql';
export default {
apollo: {
category: {
prefetch: false,
fetchPolicy: 'network-only',
query: categoryQuery,
variables () {
return {
slug: this.$route.params.slug
};
}
}
},
}
category.gql
query Category($slug: String!) {
category: forumCategoryBySlug(slug: $slug) {
id
title
description
slug
}
}
我觉得我已经对每一件小事都进行了三次检查,但我终其一生都无法弄清楚为什么数据仍在返回 null
。任何帮助将不胜感激!
几天后想通了。本质上我需要创建一个自定义解析器。
schema.graphql
type Query {
forumCategoryBySlug(slug: String! @eq): ForumCategory
}
app/GraphQL/Queries/ForumCategoryBySlug.php
<?php
namespace App\GraphQL\Queries;
use App\Models\ForumCategory;
class ForumCategoryBySlug
{
/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$category = \App\Models\ForumCategory::findBySlug($args["slug"])->first();
return $category;
}
}
我正在为 Nuxt 应用程序创建 Laravel 后端,并且正在使用 Lighthouse-PHP 通过 GraphQL 传送数据。
我正在尝试设置查询以通过 slug 而不是默认 ID 进行查找。当我在 graphql 游乐场 运行 查询时,它正确 returns 数据,但是当我在前端加载页面时,它 returns null
.
后端
schema.graphql
type Query {
forumCategoryBySlug(slug: String! @eq): ForumCategory @find
}
type ForumCategory {
id: ID!
title: String!
description: String!
slug: String!
subcategories: [ForumSubcategory!]! @hasMany
created_at: DateTime!
updated_at: DateTime!
}
ForumCategory.php 型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ForumCategory extends Model
{
protected $fillable = [
'title', 'description', 'slug'
];
public function subcategories(): HasMany
{
return $this->hasMany(ForumSubcategory::class);
}
}
前端
Index.vue
import categoryQuery from '~/apollo/queries/forum/category.gql';
export default {
apollo: {
category: {
prefetch: false,
fetchPolicy: 'network-only',
query: categoryQuery,
variables () {
return {
slug: this.$route.params.slug
};
}
}
},
}
category.gql
query Category($slug: String!) {
category: forumCategoryBySlug(slug: $slug) {
id
title
description
slug
}
}
我觉得我已经对每一件小事都进行了三次检查,但我终其一生都无法弄清楚为什么数据仍在返回 null
。任何帮助将不胜感激!
几天后想通了。本质上我需要创建一个自定义解析器。
schema.graphql
type Query {
forumCategoryBySlug(slug: String! @eq): ForumCategory
}
app/GraphQL/Queries/ForumCategoryBySlug.php
<?php
namespace App\GraphQL\Queries;
use App\Models\ForumCategory;
class ForumCategoryBySlug
{
/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$category = \App\Models\ForumCategory::findBySlug($args["slug"])->first();
return $category;
}
}