【发布时间】:2021-04-07 02:36:51
【问题描述】:
我创建了一个新的 .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 却得到了这个错误
我添加了一个复制库
【问题讨论】:
-
graphQlRequest的值是多少?您可以将其值的 json 表示添加到您的问题中。 -
OfirD 我为我的问题添加了价值:)
-
你能上传完整的 silution 到 Github 吗?如果人们可以克隆并使用您的存储库,那么提供帮助会更容易
-
当然,等一下
-
@Artur 你可以在这里找到它github.com/olaf-svenson/graphql-net-reproduction
标签: c# graphql graphql-dotnet