简体   繁体   English

使用 JSON object - GraphQL .NET 6 PostgreSQL 和 HotChocolate 查询动态模式

[英]Query dynamic schema using a JSON object - GraphQL .NET 6 PostgreSQL and HotChocolate

I need to develop a graphql server to query data directly from a JSON object. this JSON object is stored in a postgres database table as below.我需要开发一个graphql服务器直接从一个JSON object查询数据。这个JSON object存储在postgres数据库表中如下。

在此处输入图像描述

This value field can be any JSON object. I don't have control over that.这个值字段可以是任何 JSON object。我无法控制它。 it's directly coming from a SharePoint server.它直接来自 SharePoint 服务器。 I need to query this JSON object dynamically using graphql.我需要使用 graphql 动态查询这个 JSON object。

What I want here is to query JSON object and get only what I need instead of getting all the JSON data.我这里想要的是查询JSON object,只得到我需要的,而不是得到JSON的所有数据。 for example like below例如像下面

query {
  allBookings {
    id,
    listId
    widget {
      text  {
        name
        style
        onMouseUp
      }
    }
  }
}

currently, my result is this.目前,我的结果是这样的。 在此处输入图像描述

Technologies I am using我正在使用的技术

  • .NET 6 .NET 6
  • Postgresql Postgresql
  • HotChocolate热可可

this is my code so far.到目前为止,这是我的代码。

[Table("bookings")]
public class Booking
{
    [Column(name: "id")]
    public int Id { get; set; }
    [Column(name: "list_id")]
    public Guid ListId { get; set; }
    [Column(name: "value", TypeName = "jsonb")]
    [GraphQLType(typeof(AnyType))]
    public string Value { get; set; }
}

public class BookingType : ObjectType<Booking>
{
    private readonly IDbContextFactory<DemoDBContext> _factory;

    public BookingType(IDbContextFactory<DemoDBContext> factory)
    {
        _factory = factory;
    }

    [Obsolete]
    protected override void Configure(IObjectTypeDescriptor<Booking> descriptor)
    {
        descriptor.Field(t => t.Id)
            .Type<NonNullType<IntType>>();
        descriptor.Field(t => t.ListId)
            .Type<NonNullType<UuidType>>();
        descriptor.Field(t => t.Value)
            .Type<AnyType>()
            .Resolver(context =>
            {
                var db = _factory.CreateDbContext();
                var value = context.Parent<Booking>().Value;
                return value;
            });
    }
}

public class Query
{
    private readonly IDbContextFactory<DemoDBContext> _factory;

    public Query(IDbContextFactory<DemoDBContext> factory)
    {

       _factory = factory;
    }

    public async Task<IEnumerable<Booking>> GetAllBookings()
    {
        using var context = await _factory.CreateDbContextAsync();

        var bookings = await context.Bookings.ToListAsync();
        return bookings;
    }

    public async Task<Booking> GetBooking(Guid id)
    {
        using var context = await _factory.CreateDbContextAsync();

        var booking = await context.Bookings.Where(x => x.ListId == id).FirstOrDefaultAsync();

        return booking;
    }
}

I've tried different methods but I don't have a clear idea to implement this kind of behavior or even is this possible to do.我尝试过不同的方法,但我没有明确的想法来实现这种行为,甚至没有可能做到这一点。

if there's a better way of doing this please suggest me.如果有更好的方法,请建议我。 Thanks all.谢谢大家。

GraphQL will automatically filter out fields that are either not requested or not part of a model. GraphQL 将自动过滤掉未请求或不属于 model 的字段。

If you define your types as:如果您将类型定义为:

type Booking {
  id: ID!
  listID: String
  value: Widget
}
type Widget {
  text: SubWidget
}
type SubWidget {
  name: String
  style: String
  onMouseUp: String
}
query allBookings: [Booking]

In your resolver you're going to return an array of JSON objects corresponding to each Booking .在您的解析器中,您将返回与每个Booking对应的 JSON 个对象的数组。 If that JSON has fields that are not part of your type definitions they will not be returned.如果该 JSON 具有不属于您的类型定义的字段,则不会返回它们。 If some of the fields you ask for are missing then they will come back as undefined unless you make them non-nullable (ex: name: String! )如果您要求的某些字段丢失,那么它们将返回为未定义,除非您使它们不可为空(例如: name: String!

So you're most of the way there.所以你大部分时间都在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM