我在文档中尝试这部分没有成功,并且在互联网上没有找到任何好的参考。我想动态添加模式类型,并认为可以通过 GraphQLServiceProvider 中的 typeregistry 添加它们,但是当我尝试在 graphql-playground 中使用该类型时,它显示在模式错误中找不到类型。我注册的类型似乎没有加载到我不知道该怎么做的模式中。
我尝试过的事情:将提供程序添加到我的应用程序配置中。跑“作曲家转储自动加载”清除缓存和配置。
这是我的 GraphQLServiceProvider 代码
<?php
declare(strict_types=1);
namespace App\Providers;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\ServiceProvider;
use GraphQL\Type\Definition\ObjectType;
use Nuwave\Lighthouse\Schema\TypeRegistry;
class GraphQLServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @param TypeRegistry $typeRegistry
*
* @return void
*/
public function boot(TypeRegistry $typeRegistry): void
{
$typeRegistry->register(
new ObjectType([
'name' => 'Comment',
'fields' => function() use ($typeRegistry): array {
return [
'id' => [
'type' => Type::id()
],
'content' => [
'type' => Type::string()
],
'date_created' => [
'type' => Type::string()
],
'updated_at' => [
'type' => Type::string()
]
];
}
])
);
}
}
这是我的架构
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-01-01 13:00:00`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")
"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")
type Query {
users: [User!]! @paginate(defaultCount: 10)
user(id: ID @eq): User @find
posts: [Post!]! @all
comments: [Comment!]! @all
}
type User {
id: ID!
name: String!
email: String!
created_at: DateTime!
updated_at: DateTime!
}
type Post {
id: ID!
title: String!
content: String!
created_at: DateTime!
updated_at: DateTime!
}