运行GraphQL查询返回ID ' 1 '的格式无效



在热巧克力研讨会之后,在第4步之后,当运行查询

query GetSpecificSpeakerById {
  a: speakerById(id: 1) {
    name
  }
  b: speakerById(id: 1) {
    name
  }
}

我得到以下错误:

The ID `1` has an invalid format.

同样,同样的错误是抛出所有查询ID作为参数,也许这可能是一个提示,检查什么,对我来说,一个人,谁只是运行车间它仍然不清楚。

基于(不接受)类似问题的答案错误" ID ' 1 '的格式无效"当查询HotChocolate时,我检查了Relay和它的配置,看起来很好。

DI

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(CreateAutomapper());
    services.AddPooledDbContextFactory<ApplicationDbContext>(options => 
        options.UseSqlite(CONNECTION_STRING).UseLoggerFactory(ApplicationDbContext.DbContextLoggerFactory));
    services
        .AddGraphQLServer()
        .AddQueryType(d => d.Name(Consts.QUERY))
            .AddTypeExtension<SpeakerQueries>()
            .AddTypeExtension<SessionQueries>()
            .AddTypeExtension<TrackQueries>()
        .AddMutationType(d => d.Name(Consts.MUTATION))
            .AddTypeExtension<SpeakerMutations>()
            .AddTypeExtension<SessionMutations>()
            .AddTypeExtension<TrackMutations>()
        .AddType<AttendeeType>()
        .AddType<SessionType>()
        .AddType<SpeakerType>()
        .AddType<TrackType>()
        .EnableRelaySupport()
        .AddDataLoader<SpeakerByIdDataLoader>()
        .AddDataLoader<SessionByIdDataLoader>();
}

扬声器类型

public class SpeakerType : ObjectType<Speaker>
{
    protected override void Configure(IObjectTypeDescriptor<Speaker> descriptor)
    {
        descriptor
            .ImplementsNode()
            .IdField(p => p.Id)
            .ResolveNode(WithDataLoader);
    }
    // implementation
}

和查询本身

[ExtendObjectType(Name = Consts.QUERY)]
public class SpeakerQueries
{
    public Task<Speaker> GetSpeakerByIdAsync(
        [ID(nameof(Speaker))] int id, 
        SpeakerByIdDataLoader dataLoader,
        CancellationToken cancellationToken) => dataLoader.LoadAsync(id, cancellationToken);
}

但是没有一点运气。还有什么需要我查一下的吗?完整的项目可以在我的GitHub上找到。

我看到你在这个项目中启用了中继支持。端点请求一个有效的中继ID。

Relay向客户端公开不透明的id。你可以在这里阅读更多内容:https://graphql.org/learn/global-object-identification/

简而言之,中继ID是类型名和ID的base64编码组合。

要在浏览器中编码或解码,您可以简单地在控制台上使用atobbtoa

所以id "U3BlYWtlcgppMQ=="包含值

"Speaker
i1"

您可以在浏览器中使用btoa("U3BlYWtlcgppMQ==")解码此值,并使用

对字符串进行编码
atob("Speaker
i1")

所以这个查询可以工作:

query GetSpecificSpeakerById {
  a: speakerById(id: "U3BlYWtlcgppMQ==") {
    id
    name
  } 
}

最新更新