Repository navigation
Conversation
|
I've seen this requested couple of times, but I am not sure it's such a good idea. When is this kind of object preferred over a defined ObjectType? I think the great thing about graphql is that you can be sure that the data your get is in the shape you requested. With such arbitrary objects you are removing that benefit and removing all the typechecking related to it. |
|
Also if we do go this way, I'd say it should be a scalar type, because then one could ensure it's validity as JS object in parse/serialize methods. |
|
Hello, Sometimes you need your backend to save data that are absolute client's app data. It could be
This kind of type is just to isolate UI data that the server hasn't to understand... when it just has to store and provide it... I agree that the intend is really near scalar types but you don't have to serialize/deserialize it to go through validation... By the way, I'am starting to think that maybe a Raw Type may be Type free (I am thinking out loud) and leave validation to the real case implementations... In fact the serialization forces you to have a correct object to serialize... Leave validation to the one consuming it... the client... and the server just has to prevent malicious code according storage system... eventually... I agree that devs have to be very cautious about using that kind of types, and should not miss the intend of such a type, but maybe graphql could provide a config interface forcing a validate method to warn the server dev that no validation is made on raw type... That's why I think scalar clarifies the intend... but to give object into answer is really nicer once the aim is well understood... |
|
IMO the biggest use case for something like this is sending JSON blobs in a few cases where I actually do want to send shapeless data to my front end. However, to my mind, that's much better handled by sending the data across the wire as a string blob and deserializing semantically in Relay, per: meta/relay#91 I feel like "raw" is very much a misnomer here because, while JS objects are primitives in JS, there's nothing special about them except the JSON-ness. JSON objects are quite awkward in static type systems, while there's nothing special about strings as keys in many languages outside of JS. What additional problems get solved by adding this to the GraphQL spec, rather than having clients that can do generic hydration of data into types beyond the GraphQL type system? |
|
You talk about JSON blobs as if it is a brand new concept to graphql. But I agree about 2 things :
This doesn't solve much I agree, but at least the double JSON parsing that you have to deal with relay like implementations where extend scalar types is not quite simple.. (Not sure about that, but https://gist.github.com/taion/df126b9252825927b037 seems quite verbose for something that is already done at the very beginning...) |
|
By the way Scalar types are greatly handled in graphql-js! |
|
What do you think about NestedMaps as type name? |
|
But NestedMaps doesn't mean lists... so I prefer RawObject, but I don't really care... As you like... |
|
+1 |
1 similar comment
|
👍 |
|
I will leave our example use case for this feature here. We have a machine learning service that returns both a prediction and a "feature map", a JSON object containing keys and numeric values that were used as inputs to the prediction. This map is fairly large (currently around 120 key/value pairs), evolving, and not at all important to the API spec. From both the server and client perspective it is simply an opaque object that can be stored and optionally sent back to the server later for debugging purposes. We could certainly change the API to make this a serialized JSON string, but it feels like a workaround. I understand the argument about this opening the door to semi-opaque (and half-documented) APIs, but would ultimately still prefer the flexibility if possible. |
|
I feel like your example is quite different from what @Sandreu is proposing. Among other things, you actually have something that is easily and usefully expressible in a type system as something like |
|
My primary criticism is that calling this a "raw object" makes no sense, since it's only anything like a "raw object" in JS. That's not to say there wouldn't be value in something like a |
|
Hello @jhurliman, I understand your feeling, and I know that you are a tiny use case where this would help you, but I agree with you, If you want to use a JSON representation of a "scalar" data, GraphQL should be your friend since it is its native transport applicative protocol However, your use case is quite easy to solve and as @taion said, the use of a list of key and value objects can easily do the job: Still, I agree with you : change a JSON representation to another even if GraphQL knows JSON very well because of specs limitations is very frustrating ! |
|
I already told how I feel about a stringified JSON object into a stringified JSON object... :) |
|
I don't quite get how @jhurliman case in different from @Sandreu case. I think both can be represented with a Map data type that @taion proposed. Do we really need unstructured raw object or just a way to represent arbitrary Maps? I can certainly see the need for the latter, but I still feel iffy about former. |
|
Difference is nesting, in fact I propose the ability to use Javascript objects as a primitive type... without any field control. |
|
I do feel Maps and Lists can work for the most things that you've mentioned above. Note that not every language has unstructured nested JSON-like thing like JavaScript. |
|
Ok, I think we are saying the same thing :) JSON-like handles Maps but Maps are only a piece of what JSON-like brings :) |
|
There is discussion of adding a map type @ graphql/graphql-spec#101 |
|
Is there any movement on this? Could really do with 'ad-hoc' JSON objects (Raw Objects)! I'm currently stringifying to GraphQLString fields but that is not ideal. |
|
👍 to @freiksenet's point - a JSON object is not at all "raw" outside of JS. To me, this really just seems like a potentially application-specific custom scalar type. Maybe the real question is one of having a better way to hydrate this on the client. |
|
@taion, in the context of other languages I agree and the same requirement may surface for those language specific types too. So maybe what we looking for is a way to:
EDIT: In other words allow unchecked language specific 'raw' objects |
|
@clinton, at least on Graphql server side, this is already possible with custom scalar type. The only problem is Relay support. On Tue, Oct 20, 2015 at 4:50 PM, Clinton Wood [email protected]
|
|
@freiksenet, Ah!, ok, I'm not using Relay, I must have missed the custom scalar type - thanks for the heads-up! |
|
Exactly - custom scalar types are IMO the way to go. This concept of "raw" is terribly unrobust. |
|
@taion, @freiksenet if you referring to |
|
Hello, Ok, Raw may be a bad name, but we're not here to talk about the name.... And I agree that serialization is a good way to solve it, and it is the way I do today... And I don't really see why this concept is more "unrobust" than a simple string ! GraphQL is not here to protect you from malicious code... And if you're not carefull with strings you can have serious damages... And if you want to talk about the robustness of JSON... again GraphQL IS already talking JSON ! In fact I don't see what is terribly unrobust here...? |
|
How do you think JSON is transmitted on the wire, if not as a serialized string? |
|
Title changed :D |
Or ...
|
|
Right - the actual issue is more on meta/relay#91, where there isn't currently a great way to hydrate custom scalar fields in Relay. Other clients may well not have the same limitation. Fixing meta/relay#91 also gives people a much better way to handle e.g. |
|
Agree about this ticket ! meta/relay#91 should be fixed ! :) But the point here, is, whatever your client library is : serialized JSON into serialized JSON IS an awkward/workaroud/strange/nothingVeryPleasant feeling :D to have to change a map to a |
|
What do you mean? It'd be the same as for every custom scalar type. You pull it off the wire and have whatever kind of deserialization logic you need. In JS it's just |
|
Is it me or do I miss something, because I have the feeling that you're both trying to say something to the other but it seems that we don't understand what the other is trying to say :) |
|
I'm not trying to make a spec for every kind of data GraphQl may provide. GraphQL IS talking JSON and nested JSON serialization feels odd ! I can't see how I can be simpler... Moreover, multiple JSON parsing may, sometimes (as in relay) not to be as easy as it should be... Perhaps we should drop this ticket... The concept may be too complicated... Or too specific to my use case... |
|
👍 |
|
Hi, I found this issue because I'm looking for ways to send thousands of objects to the front-end, to be visualized. |
|
Thanks for all of the discussion everyone! Pursuant to @Sandreu's suggestion that we drop this PR in favor of serialization/deserialization of custom scalar values, I've spent the morning drafting up a specification here: meta/relay#91 (comment) Let's pull for that solution. We're not pursuing this internally, but if someone wants to pitch in with an implementation of what I've sketched out, we'd be happy to take a look at a PR! |
|
@steveluscher It turned out that the special case of JSON scalar types "just worked" across both graphql-js and Relay as a custom scalar type with trivial serialization/deserialization handling on the GraphQL server side, and they "just work" in Relay as well. I believe @leebyron was considering adding that type to graphql-js, as it's a little bit unintuitive, but the existing solution for handling JSON scalar types is pretty good, though perhaps not broadly known. |
Hello,
Here is an implementation of a concept that doesn't exists in specs... The idea is to be able to use raw objects as leaf type.
Sometimes your server has to handle data that it doesn't care of, and sometimes theses data are objects... Either your client has to serialize/deserialize it himself, or Raw objects allow that...
This PR allow things like that :
/!\ This is only a POC to discuss the subject ! Not an "as it is" Pull Request.
Another way could be to serialize the object on a scalar type, but this would be much less pretty! :D
Let's talk !!