gpt4 book ai didi

c# - 将泛型与 GraphType 结合使用

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:04 25 4
gpt4 key购买 nike

我是 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; }
}

这是我的图形类型:

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>();
}

);
}

Firing the "userinfo" query gives me the following error: {GraphQL.ExecutionError: Expected value of type "SMDPDaaS.GraphTypes.EntityCollectionType1[SMDPDaaS.GraphTypes.UserInfoGraphType]" for "EntityCollectionType1" but got: SMDPDaaS.ModelEntity.EntityCollection1[SMDPDaaS.ModelEntity.UserInfo]. at GraphQL.Execution.ExecutionStrategy.ValidateNodeResult(ExecutionContext context, ExecutionNode node) at GraphQL.Execution.ExecutionStrategy.ExecuteNodeAsync(ExecutionContext context, ExecutionNode node)} How do I make the graphql engine map SMDPDaaS.ModelEntity.EntityCollection1[SMDPDaaS.ModelEntity.UserInfo] to 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");

关于c# - 将泛型与 GraphType 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55311464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com