Search code examples
graphqlapollohasura

Apollo GraphQL schema + codegen converting Hasura/PostgreSQL scalar array to scalar


I'm using Hasura to provide a GraphQL interface to a PostgreSQL table. If I have a table with an integer[] column, the command apollo schema:download is generating a schema.json with:

      {
        "name": "distances",
        "description": null,
        "type": {
          "kind": "SCALAR",
          "name": "_int4",
          "ofType": null
        },
        "defaultValue": null
      }

which is unfortunate because it doesn't convey the information that this field is an array. Naturally, when I use apollo codegen:generate the type is either morphed into String or just _int4 if using the --passthroughCustomScalars flag.

Is there a way to configure my graphql server (Hasura) or the apollo CLI to preserve the array type information?


Solution

  • It turns out that the _ prefix can already be an indicator of an array type. I'm not sure if other scenarios could lead to a type name _int4 but in my use-case I can safely assume that int4 comes from Integer while _int4 comes from Integer[].

    This way I can use --passthroughCustomScalars and provide type aliases for my Apollo SDK to recognize the _int4 output from apollo codegen:generate as an integer array and define the appropriate JSON encode/decode methods. Somewhat a rough edge on the code generation tooling, but manageable.