1

I have recently started looking into integrating FastAPI with Ariadne to create an API. For simple type this is working fine but I am now looking to enable the user to save / retrieve any python dict using the API.

It's not clear to me how custom scalars could be leveraged to complete the operation.

I would have schema.graphql

scalar Generic

type UserData {
   name: String!
   data: Generic!
}

Where Generic could be any object which can be parsed as a Json dictionary. I wonder what would be required in terms of resolver to persist and retrieve the data for this scenario ?

dry
  • 831
  • 2
  • 8
  • 21
  • For those who are interested by the same topic graphene does offer the support for generic types. The integration is pretty seamless. – dry May 09 '22 at 09:39

1 Answers1

1

You would need to introduce your Generic as a scalar type in your service's code, with parse and serialize methods that just passed through any value (or, by your description, any dictionary / JSON object) literally. That would then allow it to pass into and out of your GraphQL service.

To avoid doubt: this means you need to influence the available types for your service, rather than using a resolver to influence the behaviour of a particular field.

Ed.
  • 1,992
  • 1
  • 13
  • 30
  • 1
    Thanks @Ed., indeed I don't even need to serialize the data for it to be read. However, when trying to load saved data through GraphQl API, the json saved produces the following error: **String cannot represent value: {'test_field__1': 'hello', 'test_field__2': [1, 2, 3], 'test_field__3': {'sub_field__1': 'test_subfield'}}** – dry Apr 25 '22 at 10:19
  • That looks like the GraphQL service thinks that's a `String` not a `Generic`. Need more information before anyone can fix this. – Ed. Apr 25 '22 at 12:43