代码之家  ›  专栏  ›  技术社区  ›  John Ervin

架构优先。NET Graphql未解析MVE

  •  0
  • John Ervin  · 技术社区  · 2 年前

    运行ASP。NET Core 7.0&图QL 7.6.1。

    我试图用GraphQL创建一个最低可行的模式优先GraphQL示例。NET,但查询未解析。GraphQL在控制台中解决,但在API中解决不了,所以很可能是DI问题。。。

    我一直收到消息

    试图解析字段“user”时出错

    架构

    type Query {
      user: User
    }
    
    type User {
      id: String!
      name: String!
    }
    

    用户.cs

    public class User
    {
        public string Id { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
    }
    
    public class Query
    {
        [GraphQLMetadata("user")]
        public User GetUser()
        {
            return new User { Id = "1", Name = "Simpleton" };
        }
    }
    

    程序.cs

    // Get the schema information 
    FileInfo fileInfo = new(Path.Join(AppContext.BaseDirectory, "schema.gql"));
    var schemaText = await GetSchema(fileInfo);
    var schema = Schema.For(schemaText,
      sb =>
      {
          sb.Types.Include<Query>();
      });
    
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddGraphQL(b => b
        .AddSchema(schema)  // schema
        .AddSystemTextJson());  // serializer
    
    builder.Services.AddSingleton<IDataService, DataService>();
    builder.Services.AddLogging(b => b.AddConsole());
    
    var app = builder.Build();
    
    if (app.Environment.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseGraphQLPlayground();
    }
    
    app.UseGraphQL("/graphql");  // url to host GraphQL endpoint
    
    await app.RunAsync();
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   John Ervin    2 年前

    简单的修复,只需要将属性化的Query类注册为服务:

    builder.Services.AddSingleton<Query>();
    
    推荐文章