Relay: Relay modern: setValue of a record. Allow to pass an object.

Created on 7 May 2018  路  13Comments  路  Source: facebook/relay

Hi, relay modern doesn't allow us to set object as a value for a property of a record. I'm using JSON type for a field in my graphql type. Pic
Relay understands it as a scalar type:
Pic

Is there any chance to allow pass an object as a value to setValue?

Most helpful comment

Hi @sibelius any workaround for this?

All 13 comments

@sibelius Thanks, but I don't think it will help. It just seems a but strange that Relay does put an object as a value into its store but later doesn't allow to update it ;)

can you get the json and set it back in the store?

I can get it, but can't set it back img

hey @ErrorPro, what does your schema look like? setValue doesn't allow objects because the store is normalized. If you want to set an object, you should use setLinkedRecord, which will create another record in the store associated with the first one

@jstejada Hey, but if it wasn't a linked record will Relay understand it correctly when I set a linked record? If you look at pic above there's no ID field in the object but to create a record I need to pass a unique id, right? It seems like it's not gonna work...

I suppose JSON parse and stringify is the only available option for now for complex JSON objects.

@steida Will relay parse it back to an object when storing it? Or it will be store as a string and I'll have to parse it before using in my components?

No.

Hi @sibelius any workaround for this?

I have the same problem. Someone managed to solve without having to transform it into a String?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

graphql-type-json is great and deserves first-class support IMHO. However a workaround is clear from the code linked above. Beware and replace:

record.setValue('props', props)

with:

record._mutator.setValue(record._dataID, getStableStorageKey('props'), props)
Was this page helpful?
0 / 5 - 0 ratings