-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested jsonb in insert mutation #206
Comments
I ended up writing a post-processing step to the generated code, where my jsonb type has the added classmethod below: class jsonb(sgqlc.types.Scalar):
__schema__ = amp_schema
@classmethod
def __to_graphql_input__(cls, value, *args, **kwargs):
out = super().__to_graphql_input__(value, *args, **kwargs)
if isinstance(value, dict):
import re
out = re.sub('(?<!: )"(\S*?)"', "\\1", out)
return out The previous regex had some issues removing quotes but this one seems to do the trick! |
to fix it properly I'll need to investigate if there is something better to do. Is
Which is what I'd expect "out of the box", seems we're missing the extra quotes/escaping in case it's a JSON array/object, but it is correct for strings/integer/float/booleans/null. Then, likely this should be fixed. Of course you can/should always customize your serialization as you did with |
Yes. Previously we were using the I tried manually changing the query to include the extra quotes and the only thing that actually passes is to write the query like it appears in the GQL dsl, with unquoted keys. There are definitely edgecases this cannot support... I think the only safe thing is to use |
@ryandvmartin yes, actually that's the recommended way. While you can manually write different operations with hard coded values in it, the recommendation is to use the same operation with variables. Some servers will cache based on the document hash, so when you send it again, it will get from the cache without the need to re-parse/validate the AST, saving tons of work. It's also the foundation to get Automatic Persisted Queries (which we don't support out of the box, but could be implemented manually by users). Could you elaborate/paste an example of what you mean with:
Usually scalars are encode as GraphQL basic data types, these are Int, Float, Boolean or more commonly String. Then server-side you declare your scalar's parse to execute |
Hi @ryandvmartin thanks for creating this issue Would it be possible that you share the way you did it using Cheers |
BTW thanks for your solution using |
Hi. Having a specific issue inserting data where one of the fields is jsonb (or json). I keep getting the error:
where the generated sgqlc mutation looks like:
At first glance the error seems to come from the auto-generated
jsonb
from the schema-derived sgqlc types.Building the mutation, when the jsonb field is encountered, it is dumped with
json.dumps
, which , of course, retains quoted dictionary keys as above. When I copy the failing query intoiQL
and fix the json keys to omit quotes the mutation works, e.g., changing to this is working:This could be implemented by adding the following lines to the end of
types.Scalar.__to_graphql_input__()
- no idea how robust/performant this would be..Dropping to the variable paradigm also works, though there is another quirk there on the renaming of the variable name in the mutation - if I wanted to use my schema's naming
method_kwargs
is automatically changed tomethodKwargs
in sqglc's variable setup, but the same processing is not applied to the dictionary given to the endpoint.I guess the question is should I expect the direct usage of jsonb in my
insert_task_one
mutation to work? Could it work? Would this be reasonable to add?Otherwise, thanks for the terrific project! Much appreciated
The text was updated successfully, but these errors were encountered: