简体   繁体   English

给定基于 GraphQL 模式的 typescript 类型,如何动态定义 GraphQL 查询字符串?

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

I have a Node application where I have generated typescript types from GraphQL schema.我有一个节点应用程序,我从 GraphQL 模式生成了 typescript 类型。 My service is going to receive GraphQL Object and attributes details in an API request.我的服务将在 API 请求中接收 GraphQL Object 和属性详细信息。 The service has to form a GraphQL query string and return in the response.该服务必须形成一个 GraphQL 查询字符串并在响应中返回。

For example, If the request is 'Object.attribute = value', then the service has to generate a query string like below:例如,如果请求是“Object.attribute = value”,则服务必须生成如下查询字符串:

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. Object(filter: {'attribute': {'val': 'value'}}){ attribute attribute1 attribute2... }请注意,我需要动态生成查询,我不想硬编码查询字符串.

I have done the same using Python's gql library.我使用 Python 的 gql 库做了同样的事情。 It has dsl that supports the creation of such strings.它具有支持创建此类字符串的 dsl。
References: Python GQL: https://gql.readthedocs.io/en/stable/ GQL DSL: https://gql.readthedocs.io/en/latest/advanced/dsl_module.html#compose-queries-dynamically参考文献:Python GQL: https://gql.readthedocs.io/en/stable/ GQL DSL: https://gql.readthedocs.io/en/latest/advanced/dsl_module.html#compose-queries-dynamically

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.我现在需要在 TypeScript 中做同样的事情,但我无法立即找到可以帮助我做这件事的图书馆。

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.我尝试在 python 中生成查询字符串。当我尝试使用 typescript 时,我找不到可以立即帮助我做同样事情的库,但是,我确实生成了 TypeScript 类型,但我不确定如何使用这些类型或他们如何帮助生成查询字符串。

I expect an example library that can help me in building the graphql query strings dynamically.我希望有一个示例库可以帮助我动态构建 graphql 查询字符串。

The "graphql" library allows you to define different graphql elements using code. “graphql”库允许您使用代码定义不同的 graphql 元素。 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.我不确定它是否可以 output 完整查询,或者您必须对其进行进一步修改,但至少用它的键、类型等定义查询主体是一个好的开始。

https://www.apollographql.com/blog/backend/schema-design/three-ways-to-represent-your-graphql-schema/#graphql-js-graphqlschema-object 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/ https://graphql.org/graphql-js/type/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM