How to dynamically define GraphQL query string, given typescript types based on GraphQL schema?

How to dynamically define GraphQL query string, given typescript types based on GraphQL schema?


0

I have a Node application where I have generated typescript types from GraphQL schema.
My service is going to receive GraphQL Object and attributes details in an API request.
The service has to form a GraphQL query string and return in the response.

For example, If the request is ‘Object.attribute = value’, then the service has to generate a query string like below:

Object(filter: {'attribute': {'val': 'value'}}){      attribute      attribute1      attribute2      ... }

Please note that I need to generate the queries dynamically, I don’t want to hard code the query strings.

I have done the same using Python’s gql library. It has dsl that supports the creation of such strings.
References:

I need to do the same in the TypeScript now, but I am not able to find a library right away that can help me in doing that.

I tried generating the query strings in python.
When I tried with typescript I could not find a library that can right away help me in doing the same but, I did generate the TypeScript types, but I am not sure how to use those types OR how they can help in generating the query strings.

I expect an example library that can help me in building the graphql query strings dynamically.

1 Answer
1


0

The "graphql" library allows you to define different graphql elements using code. I’m not sure if it can output the full query or you have to do further modifications to it, but it’s a good start to at least define the query body with it’s keys, types, etc.

https://www.apollographql.com/blog/backend/schema-design/three-ways-to-represent-your-graphql-schema/#graphql-js-graphqlschema-object

https://graphql.org/graphql-js/type/



Leave a Reply

Your email address will not be published. Required fields are marked *