我是GraphQL的新手,正在尝试为泛型类创建GraphType。

这是我的数据对象:

public class UserInfo
{
        public string UserAlias { get; set; }
        public List<int> Subs { get; set; }
        public List<int> Biz { get; set; }
}



 public class EntityCollection<T>
 {
        public bool MoreRecords { get; set; }
        public string ContinuationToken { get; set; }
        public IList<T> Results { get; set; }
 }


这是我的GraphTypes:

public class UserInfoGraphType : ObjectGraphType<UserInfo>
    {
        public UserInfoGraphType()
        {
            Field(x => x.UserAlias);
            Field<ListGraphType<IntGraphType>>("biz", resolve: context=>context.Source.Biz);
            Field<ListGraphType<IntGraphType>>("subs", resolve: context => context.Source.Subs);

        }
    }

 public class EntityCollectionType<T>:  ObjectGraphType<EntityCollection<T>> where T: IObjectGraphType
    {
        public EntityCollectionType()
        {
            Field(x => x.MoreRecords);
            Field(x => x.ContinuationToken);
            Field<ListGraphType<T>>("Results");
        }
    }


这是我的查询:

public Queries(IRepository UserInfoRepository)
    {
         Field<EntityCollectionType>("userinfo",
         arguments: new QueryArguments { new QueryArgument { Name="userAlias"} },
         resolve: context => {
                  return new EntityCollection<UserInfo>();
         }

    );
    }



  触发“ userinfo”查询会给我以下错误:
  {GraphQL.ExecutionError:类型的期望值
  “ SMDPDaaS.GraphTypes.EntityCollectionType1 [SMDPDaaS.GraphTypes.UserInfoGraphType]”
  对于“ EntityCollectionType1”,但得到:
  SMDPDaaS.ModelEntity.EntityCollection1 [SMDPDaaS.ModelEntity.UserInfo]。
  在
  GraphQL.Execution.ExecutionStrategy.ValidateNodeResult(ExecutionContext
  上下文,ExecutionNode节点)
  GraphQL.Execution.ExecutionStrategy.ExecuteNodeAsync(ExecutionContext
  上下文,ExecutionNode节点)}如何制作graphql引擎映射
  SMDPDaaS.ModelEntity.EntityCollection1 [SMDPDaaS.ModelEntity.UserInfo]
  至
  SMDPDaaS.GraphTypes.EntityCollectionType`1 [SMDPDaaS.GraphTypes.UserInfoGraphType]?

最佳答案

我也刚遇到这个问题。这是我解决的方法。

public class Book
{
  public int Id { get; set; }
  public string Title { get; set; }
}

public class ResultSet<T>
{
  public T[] Results { get; set; }
  public int TotalResultsCount { get; set; }
}

public class BookType : ObjectGraphType<Book>
{
  public BookType()
  {
    Field(b => b.Id);
    Field(b => b.Title);
  }
}

public class ResultSetType<T, TType> : ObjectGraphType<ResultSet<T>>
  where TType : IGraphType
{
  public ResultSetType()
  {
    Name = $"{typeof(TType).Name}ResultSet";
    Field<ListGraphType<TType>>("results");
    Field<IntGraphType>("totalResultsCount");
  }
}


现在可以将其用作查询中的字段,如下所示:

Field<ResultSetType<Book, BookType>>(
                "books",
                arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IntGraphType>> { Name = "pageIndex" }, new QueryArgument<NonNullGraphType<IntGraphType>> { Name = "pageSize" }),
                resolve: ctx => booksRepository.ReadBooks(ctx.GetArgument<int>("pageIndex"), ctx.GetArgument<int>("pageSize"))
            );


我的查询允许分页,其中用户(或UI)可以指定一次要获得多少结果以及要查看的页面,因此它传入了两个参数,但是这是我们感兴趣的部分您将是Field<ResultSetType<Book, BookType>>("books");

07-24 09:51