question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing of literal composite type containing array

See original GitHub issue

As per https://github.com/hasura/graphql-engine/issues/1041, I’m using the literal type escape hatch in order to insert a record into a table which contains a composite type.

This is a simplified sql mockup of my data:

CREATE TYPE public.value_qualifier AS
 ENUM ('qualifier1','qualifier2','qualifier3','qualifier4');

CREATE TYPE public.value_qualified AS
(
  value text,
  qualifier public.value_qualifier[]
);

CREATE TABLE public.test (
	id serial NOT NULL,
	value public.value_qualified,
	CONSTRAINT test_pkey PRIMARY KEY (id)
);

The issue arises when trying to insert data into the value column that contains more than one element in the array.

That’s a bit of a mouthful, so as a set of examples, this works using the HTTP interface that graphiQL uses (not tested on graphQL):

{
  value: "('some-text',{})"
}

This also works

{
  value: "('some-text',{qualifier1})"
}

However, this doesn’t:

{
  value: "('some-text',{qualifier1,qualifier2})"
}

The error I’m getting is something like Warning: Missing translation for key: "{"path":"$.args","error":"malformed array literal: \"{qualifier1\"","code":"data-exception"}"

Not having looked at the source code I’ve been scratching my head as to why this happens. It looks to me as if the parsing of the composite type does something like split(",", x) in a way that divides the string within the parentheses into x number of values. However, if this is the case, the parsing is breaking the array up.

It’s as if hasura is sending {qualifier1 as a value, hence the malformed array error

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sanderhahncommented, May 19, 2020

Was looking into related issue regarding escaping and found that this seems to work:

mutation {
  insert_test(objects:[{
    value: "('x',{qualifier1\\,qualifier2})"
  }]) {
    affected_rows
  }
}
hasura=> select to_json(value) from test;
                         to_json                         
--------------------------------------------------------- 
 {"value":"'x'","qualifier":["qualifier1","qualifier2"]}

Parsing can potentially be handled using:

hasura=> select * from json_populate_record(null::test, '{"value":{"value":"x","qualifier":["qualifier1","qualifier2"]}}');
 id |             value             
----+-------------------------------
    | (x,"{qualifier1,qualifier2}")
(1 row)
0reactions
eadscommented, Aug 23, 2021

Bump! My colleague @ilica and I just ran into this, and it would be pretty nice to be able to pass straight JSON into columns typed as ARRAYs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating composite literal of array of arrays - Stack Overflow
Short answer: The notation [...] can be used to construct an array literal, but cannot be used in an array declaration. In the...
Read more >
Documentation: 15: 8.16. Composite Types - PostgreSQL
A composite type represents the structure of a row or record; it is essentially just a list of field names and their data...
Read more >
Chapter 4. Composite Types - Shichao's Notes
Since a slice contains a pointer to an element of an array, passing a slice to a function permits the function to modify...
Read more >
Composite literals in Go. Literals in source code allow to…
Arrays & slices ... Elements of slice or array are indexed so key from literal must be constant integer expression. For elements without...
Read more >
Documentation - Template Literal Types - TypeScript
Inference with Template Literals · The literal used in the first argument is captured as a literal type · That literal type can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found