Graphql mutation for a column of type UUID[]

1.9k Views Asked by At

I'm using a postgraphql autogenerated mutation to create a row with graphql in a in a postgres table that has a column of datatype UUID[].

However, there doesn't seem to be a way to save this UUID[] data with graphql? Is this a datatype that graphql doesn't account for or am I wrongly forming the array?

When I go to create the row in graphiql:

mutation {
  createJob(
    input: {
      user_ids: ["5b7323ac-e235-4edb-bbf9-97495d9a42a1"], 
      instructions: "Job Instructions", 
      publishedDate: "2017-06-07"}
   }
 )
}

I get the following error:

"message": "column \"user_ids\" is of type uuid[] but expression is of type text[]"

Is a UUID technically not stored like text? I've tried different ways of forming the the UUID array but nothing seems to work

2

There are 2 best solutions below

1
On

You can probably have a workaround to this problem by creating an implicit cast:

CREATE CAST (text[] AS uuid[])
    WITH INOUT
    AS IMPLICIT ;

PostgreSQL will automatically do the conversion from text[] to uuid[]; which just seems that is the needed step.


NOTE: The above code must be executed from "the owner of type uuid[] or text[]", which usually means: postgres or the database owner.


To check that it works, you may just do:

CREATE TABLE t
(
   ids uuid[]
) ;

INSERT INTO t 
    (ids)
VALUES
    (ARRAY['5b7323ac-e235-4edb-bbf9-97495d9a42a1','5b7323ac-e235-4edb-bbf9-97495d9a42a2']),
    (ARRAY['6b7323ac-e235-4edb-bbf9-97495d9a42a1']) ;

... and then, test it with GraphQL

0
On

This was a bug in PostGraphQL; it's been fixed in version 4 (which has not been released yet, but you can install via npm install postgraphql@next)

More information: https://github.com/postgraphql/postgraphql/issues/516