是否可以创建Graphql Generic Type

时间:2018-05-19 10:49:27

标签: c# asp.net-core graphql

我需要在asp.net核心中使用GraphQL的分页类型。

我正在尝试下面的代码类型:

entry=1
first entry
It's sunny today

entry=2
Second entry
It's sunny today too
Hi, how are you?

entry=3 
last entry
bye

此外,我正在尝试在using GraphQL.Types; using WebApi.Data.Entities.Models; using WebApi.Dtos; using WebApi.Helpers; using WebApi.Services; namespace WebApi.Data.Entities.Graphql.GraphQueryTypes { public class PagedResultType<T>: ObjectGraphType<T> where T : class { public PagedResultType() { Name = "PagedResultType"; Field<IntGraphType>("CurrentPage"); Field<IntGraphType>("PageCount"); Field<IntGraphType>("PageSize"); Field<IntGraphType>("RowCount"); Field<IntGraphType>("FirstRowOnPage"); Field<IntGraphType>("LastRowOnPage"); Field<ListGraphType<T>>("Results"); } } } 上创建Field

RootQuery

但它表示错误:

  

类型'T'不能用作泛型类型中的类型参数'T'   或方法'ListGraphType'。没有隐含的参考   从'T'转换为'GraphQL.Types.IGraphType'。

那么可以创建通用图形对象类型吗?

我怎样才能获得这个?

由于

2 个答案:

答案 0 :(得分:2)

我也碰到了这个问题。我尝试查看Nkosi的答案,但一定会误解它,因为我无法如上所述使它正常工作。这是我解决的方法。

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");

答案 1 :(得分:0)

ListGraphType<T>定义为

public class ListGraphType<T> : ListGraphType
    where T : IGraphType
{
    public ListGraphType()
        : base(typeof(T))
    {
    }
}

该消息暗示您缺少IGraphType约束,如果要将ListGraphType<>

public class PagedResultType<T>: ObjectGraphType<T> where T : class, IGraphType { //... }

一起使用,则应从中派生出类型参数
IGraphType

现在添加约束意味着与页面结果一起使用的任何类型参数都必须从public class ComplaintSourceDTO: ObjectGraphType<ComplaintSourceDTO> { //... }

派生

例如

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="20dp" />
            <solid android:color="#8c0000" />
        </shape>
    </item>
    <item >
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="20dp" />
            <solid android:color="#c62f2c" />
        </shape>
    </item>
</selector>