简体   繁体   中英

Destructure a JsonObject to a GraphQL Type on Return

Vertx is a big fan of json being a first class citizen, and I'm a big fan of it as well. I have jsonb columns stored in postgres as my primary data storage mechanism. Kind of like this:

CREATE TABLE game (
    id varchar(16) not null default next_id() primary key,
    data jsonb not null
);

and I retrieve the data, work on the json object, and return it to the browser. Works great.

I'm trying to use GraphQL as well, though, but it seems that I can't convert a json object into the graphql type.eg i have this data stored in the db:

{"name": "dominion", "createdBy": "Donald X. Vaccarino", "id": 233}

and this is the graphql type:

type Game {
    id: Long,
    name: String,
    createdBy: String,
    createdAt: DateTime,
    deletedAt: DateTime
}

and this graphql query returns a list of empty items because the jsonObject (game) within the jsonArray doesn't get desctructured into the Game type:

type Query {
    allGames(secureOnly: Boolean = false): [Game]
}

but the list of games does show up if i use json for the query result type:

type Query {
    allGames(secureOnly: Boolean = false): [JSON]
}

The problem with this, though, is that there is now no type information in the graphql schema. There's no way to let the client know what properties are in the JSON object

My data fetcher has this type:

DataFetcher[CompletionStage[JsonArray]]

Any thoughts on how I can return a json in code and have the graphql response return the game type?

The default GraphQL data fetcher is PropertyDataFetcher .

If you want to support Vert.x JsonObject and JsonArray , you must configure GraphQL-Java to use VertxPropertyDataFetcher instead:

RuntimeWiring.Builder builder = RuntimeWiring.newRuntimeWiring();
builder.wiringFactory(new WiringFactory() {
  @Override
  public DataFetcher<Object> getDefaultDataFetcher(FieldWiringEnvironment environment) {
    return VertxPropertyDataFetcher.create(environment.getFieldDefinition().getName());
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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