graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

better support for postgres array/hstore types

Open 0x777 opened this issue 5 years ago • 39 comments

Currently the input for an array is a literal, i.e for a column of type text[] the input value has to be a string as follows: "{hello,world}". We should do the appropriate mapping to json arrays.

0x777 avatar Sep 03 '18 10:09 0x777

via Pum on discord

0x777 avatar Sep 03 '18 10:09 0x777

This works when the literal is typed out in the body of the query, but I can't even get the string syntax to work when the value is passed in as a variable.

mrmurphy avatar Sep 11 '18 21:09 mrmurphy

Actually, turns out that variables work as long as their type is _text and not String.

mrmurphy avatar Sep 11 '18 21:09 mrmurphy

We should also be supporting the common operators on arrays like we did for json/jsonb.

0x777 avatar Jan 19 '19 06:01 0x777

Most importantly, would like to see _contains support on arrays

jafri avatar Feb 03 '19 02:02 jafri

Till we add this, as a workaround, you can use JSONB and store JSON encoded arrays. We have a support for a bunch of operators including some that have contains type functionality.

On Sun, 3 Feb 2019 at 7:32 AM, Syed Jafri [email protected] wrote:

Most importantly, would like to see _contains support on arrays

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hasura/graphql-engine/issues/348#issuecomment-460016048, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIAWA9mqDzYSsvacwZ-bZUkajdJFSHOks5vJkMcgaJpZM4WXXKh .

coco98 avatar Feb 03 '19 02:02 coco98

@coco98 I have seen the @> contains support for JSONB, the problem is that primitive arrays provide statistics, and are thus more efficient in queries.

jafri avatar Feb 03 '19 03:02 jafri

I use this a lot with Rails https://edgeguides.rubyonrails.org/active_record_postgresql.html#array

cj avatar Mar 05 '19 19:03 cj

Is there an update on this. it's getting really hard updating the arrays, no mapping... we always need to do some hacks to accomplish those simple operations.

yassinebridi avatar Sep 22 '19 11:09 yassinebridi

Hi @yassinebridi, https://github.com/hasura/graphql-engine/pull/2243 had to be aborted to handle some edge cases. We'll be picking this up again in a couple of weeks.

dsandip avatar Sep 23 '19 07:09 dsandip

What can we do for now instead... without resorting to implementing custom server just to have a single endpoint for this exact query?

If there's none, then I guess that's how it should be.

xxRockOnxx avatar Sep 25 '19 11:09 xxRockOnxx

fyi just knowing that the right format for text[] is {hello,world} solved my problem (obvious in retrospect) — consider adding it to docs

gunar avatar Dec 24 '19 22:12 gunar

@gunar you can find this in our docs here 🙂

marionschleifer avatar Dec 25 '19 13:12 marionschleifer

@marionschleifer that's great, thank you! — your docs are impeccable.

gunar avatar Dec 27 '19 14:12 gunar

Still excitedly waiting for #2243 !

flinz avatar Mar 12 '20 13:03 flinz

@0x777 Can text[] be accessed through the console?

JorgeCeja avatar Mar 25 '20 04:03 JorgeCeja

@JorgeCeja This issue is for server. Suppose for array types in console is here: https://github.com/hasura/graphql-engine/issues/2544

tirumaraiselvan avatar Mar 25 '20 05:03 tirumaraiselvan

Array Support PLZ and Thank you! I am enjoying Hasura

aydts avatar Apr 18 '20 00:04 aydts

Any update on how we can use array and what will be the type of field for it in hasura? https://hasura.io/docs/1.0/graphql/manual/mutations/insert.html#insert-an-object-with-an-array-field This is not working and sending back as string and also there is no text[] or _text in field types in hasura.

AkshayCloudAnalogy avatar Jun 10 '20 08:06 AkshayCloudAnalogy

Is it possible or planned to have the _contains filter type for arrays? Arrays seem to work in general but not for filtering.

cpursley avatar Aug 12 '20 08:08 cpursley

@JorgeCeja , @AkshayCloudAnalogy .. there's a way to "access" the text[] through the console (I know, it's a hack 😅)

Here's a gif showing how:

  • you can write the "[]" symbol after selecting the "text" type

array

ghashi avatar Oct 27 '20 01:10 ghashi

I migrated a column from jsonb to text[], defined __text: string[] in codegen.yml and generated typescript types. The type of queried data was cool, but I found out I can't insert arrays to the field because the data should be in this array literal format... Looking forward to this issue being addressed.

rtrembecky avatar Oct 27 '20 09:10 rtrembecky

Any progress on this? I'm asking because someone is assigned.

bbigras avatar Sep 09 '21 19:09 bbigras

@rtrembecky Not sure I understand "data should be in this array literal." Is the reason the array can't be updated due to something with Hasura or the client library you use to talk to Hasura (I use React Apollo, for example)? Were you able to format the data in a way that you could update the arrray?

danstepanov avatar Sep 10 '21 19:09 danstepanov

@danstepanov this was nearly a year ago, so I don't remember well, but I believe this is a problem of Hasura - when you use text[] as a field format, you receive the data as an array, but you need to insert/update data in "array literal format" instead - "{hello,world}" as mentioned by OP here. this would require somehow migrating all arrays in mutations to this format. my goal was to make the typescript types better and be more confident in our code (not to introduce clutter), so I decided to stick to jsonb and to hope this gets solved (and we see it never did).

rtrembecky avatar Sep 10 '21 22:09 rtrembecky

Got it, thanks @rtrembecky.

The way I have gotten around this is passing in the variable as:

variables: { stringArray: `{ ${newArrayItem} }` }

within useMutation. And my actual query type definition is:

gql`
    mutation MyMutation(
        $stringArray: _text
    ) {
        REST_OF_MUTATION_HERE...
    }

This is definitely bonkers and I haven't yet set it up for if you want to keep the old values of stringArray and simply add one more item to it. But it works for now. Btw, in Hasura, my data type for stringArray is text[].

danstepanov avatar Sep 10 '21 22:09 danstepanov

This is kind of bonkers, indeed. I wonder why arrays are second class citizens in hasura and what the decision making was behind that. The fact that JSONb operators work for this use case seems like more of an accident than an intended workaround.

factoidforrest avatar Mar 24 '22 03:03 factoidforrest

Having proper support for arrays would be great. Kinda sucks that it still isn't supported even though this issue is from 2018

John0x avatar Jun 24 '22 18:06 John0x

Please make the arrays work!

zzart avatar Jul 05 '22 10:07 zzart

coming on 4 years...

maxcan avatar Jul 11 '22 16:07 maxcan