我创建了一个新的 .NET Core 项目并安装了包 GraphQL、GraphiQL 和 GraphQL.SystemTextJson。
运行应用程序时,这就是我得到的
除了异常消息 GraphiQL 无法找到架构文档。
首先,我有两个实体,用户和任务。
public sealed class User
{
public Guid Id { get; set; }
}
public sealed class Task
{
public Guid Id { get; set; }
}
并且他们都有自己的代表图类型
public sealed class UserType : ObjectGraphType<User>
{
public UserType()
{
Name = nameof(User);
Field(user => user.Id).Description("The user id.");
}
}
public sealed class TaskType : ObjectGraphType<Task>
{
public TaskType()
{
Name = nameof(Task);
Field(task => task.Id).Description("The task id.");
}
}
我创建了包含客户端可以执行的所有“操作”的查询
public sealed class GraphQLQuery : ObjectGraphType
{
private readonly List<User> _users = new List<User>();
private readonly List<Task> _tasks = new List<Task>();
public GraphQLQuery()
{
Field<ListGraphType<UserType>>(
"users",
"Returns a collection of users.",
resolve: context => _users);
Field<ListGraphType<TaskType>>(
"tasks",
"Returns a collection of tasks.",
resolve: context => _tasks);
}
}
并为模式注册该查询
public sealed class GraphQLSchema : GraphQL.Types.Schema
{
public GraphQLSchema(GraphQLQuery graphQLQuery, IServiceProvider serviceProvider) : base(serviceProvider)
{
Query = graphQLQuery;
}
}
在启动文件中,ConfigureServices
我添加了此代码以在调用之前注册所有必需的服务services.AddControllers()
serviceCollection
.AddSingleton<IDocumentExecuter, DocumentExecuter>()
.AddSingleton<IDocumentWriter, DocumentWriter>()
.AddSingleton<ISchema, GraphQLSchema>()
.AddSingleton<GraphQLQuery>()
在Configure
我最初调用的方法app.UseGraphiQl()
中。
对应的 GraphQL 请求 DTO
public sealed class GraphQLRequest
{
public string OperationName { get; set; }
public string Query { get; set; }
[JsonConverter(typeof(ObjectDictionaryConverter))]
public Dictionary<string, object> Variables { get; set; }
}
最后我实现了 API 控制器
[ApiController]
[Route("[controller]")]
public sealed class GraphQLController : Controller
{
private readonly ISchema _schema;
private readonly IDocumentExecuter _documentExecuter;
public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter)
{
_schema = schema;
_documentExecuter = documentExecuter;
}
public async Task<IActionResult> Post([FromBody] GraphQLRequest graphQlRequest)
{
ExecutionOptions executionOptions = new ExecutionOptions()
{
Schema = _schema,
Query = graphQlRequest.Query,
Inputs = graphQlRequest.Variables?.ToInputs()
};
ExecutionResult executionResult = await _documentExecuter.ExecuteAsync(executionOptions);
if (executionResult.Errors != null)
return BadRequest(executionResult);
return Ok(executionResult);
}
}
有人知道这里有什么问题吗?我看不到循环依赖等问题。
运行应用程序时,graphQlRequest
包含以下值
- 操作名称:IntrospectionQuery
- 询问:
.
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
我现在迁移到 .NET 5 却得到了这个错误
我添加了一个复制存储库