首页 > 解决方案 > 在 Graphql 我需要显示列表

问题描述

我想返回数据列表,传递 id。低于错误Graphql

{ "errorMessage": [ "System.InvalidOperationException: "System.Collections.Generic.List 1[GraphQLAPI.Domain.PatientInfo]\" value of type \"System.Collections.Generic.List1[GraphQLAPI.Domain.PatientInfo]" 不允许用于 "PatientType"。要么更改 "PatientType" 的 IsTypeOf 方法以接受此值,要么返回来自解析器的另一个值。\r\n 在 GraphQL.Execution.ExecutionStrategy.ValidateNodeResult(ExecutionContext context, ExecutionNode node) in / /src/GraphQL/Execution/ExecutionStrategy.cs:line 404\r\n at GraphQL.Execution.ExecutionStrategy .CompleteNode(ExecutionContext context, ExecutionNode node) in //src/GraphQL/Execution/ExecutionStrategy.cs:line 316" ] }

公共类 PatientQuery: ObjectGraphType { PatientInfoService infoService = new PatientInfoService();

    public PatientQuery()
    {
        Field<PatientDetailsType>(
            name: "patient",
            arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "patientid" }),
            resolve: context =>
            {
                var id = context.GetArgument<int>("patientid");
                return infoService.GetPatientById(id);
            }
            );
    }

}

infoservice.GetPatientBYid(id) 返回列表。

标签: .netgraphql

解决方案


假设 infoService.GetPatientById() 返回一个列表,则以下更改将反映在解析器中。

这一行:

...
Field<PatientDetailsType>(
...

至:

...
Field<ListGraphType<PatientDetailsType>>(
...

推荐阅读