AriadneGraphQL - How to accept / return generic python dictionary

377 views Asked by At

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 ?

1

There are 1 answers

2
Ed. On

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.