enviodev / hyperindex

📖 Blazing-fast multi-chain indexer
https://envio.dev
66 stars 5 forks source link

Feature request: Support for jsonb Type in Schema #226

Open pavlovdog opened 3 weeks ago

pavlovdog commented 3 weeks ago

Description

Currently, I am trying to define a GraphQL schema that includes a field capable of representing different types of objects. Here's an example schema:

type One {
  id: ID!
  text: String!
}

type Two {
  id: ID!
  text: String!
  value: BigInt!
}

union OneOrTwo = One | Two

type MyEntity {
  id: ID!
  oneOrTwo: OneOrTwo
}

Unfortunately, I encountered the following error while attempting to generate TypeScript code:

Error: Failed cli execution

Caused by:
    0: Failed parsing config
    1: Parsing schema file for config
    2: Failed converting schema doc to schema struct
    3: No type definition 'OneOrTwo' exists in schema

The workaround suggestion was to use an enum, but this doesn't allow for complex payloads, which I need.

Request

It would be great if the schema could support a jsonb type, like so:

type MyEntity {
  id: ID!
  oneOrTwo: Jsonb
}

This way, the oneOrTwo field could accept any valid JSON, significantly simplifying the table layout and improving flexibility in handling different object types.

JonoPrest commented 3 weeks ago

Hey @pavlovdog,

I've added Jsonb as a feature request on our backlog. For the mean time to unblock you could use a string field and serialize your json.

It's unlikely we will support union types natively in the near future. Perhaps I can suggest you model your data with a nullable relationship field for both your entity One and Two. You can also model an algebraic enum (with a complex payload) just using an enum tag field.

For eg.

type One {
  id: ID!
  text: String!
}

type Two {
  id: ID!
  text: String!
  value: BigInt!
}

enum Tag {
  ONE
  TWO
}

type MyEntity {
  id: ID!
  tag: Tag!
  one: One
  two: Two
}

Then based on the tag you can look up the related entity, and only set the relevant payload.