Graphql-engine: expose bytea data types as base64 strings

Created on 12 Nov 2019  路  7Comments  路  Source: hasura/graphql-engine

I think bytea data types could be better supported. For example, a value like \xba69f7b91a3b7a7f06716c40ac066b96caf457f2 is currently simply presented as \\xba69f7b91a3b7a7f06716c40ac066b96caf457f2. I think it would be better if it was base64 encoded inside the result.

server ideas

Most helpful comment

If anyone interested, I used this function in a computed field to get a bytea field as base64:

CREATE OR REPLACE FUNCTION content_base64(mm multimedia)
RETURNS TEXT AS $$
  SELECT encode(mm.content, 'base64')
$$ LANGUAGE sql STABLE;

for the content field of a table like this

create table multimedia
(
    id bigserial not null
        constraint multimedia_pkey
            primary key,
    content bytea,
    file_name varchar(255),
    mime_type varchar(255),
);

So I can now query it like this (fields names are aliased in the Hasura schema):

query {
      multimedia {
          id
          mimeType
          fileName
          contentBase64
      }
}

All 7 comments

@mitar This can be accomplished through computed fields: https://docs.hasura.io/1.0/graphql/manual/schema/computed-fields.html#computed-fields

Hm, but I cannot use that as a primary key, no? Also, can I query by computed fields, use them in where clause? Because they are not backed by the index?

@mitar Currently there's no support for computed fields in where clause.

@mitar Can you explain in more detail what is the behaviour you are expecting. Maybe a sample schema along with the request you want to run will help

I just send my migrations to @shahidhk for help with #3401. You can see there what I have.

I have some primary keys a 160bit binary blob. Which I represent as bytea data type. But the issue is that this is exposed through GraphQL as a PostgreSQL specific encoding of binary. I would prefer if that would be exposed through base64. And that then I could also do documents_by_pk and provide such base64 back.

If anyone interested, I used this function in a computed field to get a bytea field as base64:

CREATE OR REPLACE FUNCTION content_base64(mm multimedia)
RETURNS TEXT AS $$
  SELECT encode(mm.content, 'base64')
$$ LANGUAGE sql STABLE;

for the content field of a table like this

create table multimedia
(
    id bigserial not null
        constraint multimedia_pkey
            primary key,
    content bytea,
    file_name varchar(255),
    mime_type varchar(255),
);

So I can now query it like this (fields names are aliased in the Hasura schema):

query {
      multimedia {
          id
          mimeType
          fileName
          contentBase64
      }
}

@beepsoft Thank you for your solution, it is working great for me!

Was this page helpful?
0 / 5 - 0 ratings