简体   繁体   English

生成动态 GraphQL Schema

[英]Generate dynamic GraphQL Schema

I'm creating a system where the no.我正在创建一个没有的系统。 of columns in a table is not fixed.表中的列数不固定。 I'll explain with an example.我会用一个例子来解释。

Consider a people table with 3 columns.考虑一个有 3 列的人员表。

people
----------
id | name | email

This table will be exposed to a GraphQL API and I can query the table.这张表会暴露给一个GraphQL API我可以查询这张表。 In my system, the user will be able to add custom columns to the people table.在我的系统中,用户将能够向 people 表添加自定义列。 Let's say they add a nationality column.假设他们添加了一个国籍栏。 When they do this, the nationality won't be available in the API because it is not defined in the Schema.当他们这样做时,国籍在 API 中将不可用,因为它未在架构中定义。

So how can I make my schema dynamic that allows the user to query the people table with every extra column they add?那么我怎样才能使我的模式动态化,以允许用户使用他们添加的每个额外列来查询 people 表?

I can query the information_schema table or a fields table and get the extra fields for the people table and then use the GraphQLObjectType to build out my schema rather than using SDL.我可以查询 information_schema 表或字段表并获取 people 表的额外字段,然后使用 GraphQLObjectType 构建我的模式而不是使用 SDL。

this is not the answer to your question but the suggestion because creating dynamic columns in SQL doesn't look like a good idea to me.这不是您问题的答案,而是建议,因为在 SQL 中创建动态列对我来说不是一个好主意。 Instead of thinking how can you create dynamic schema I think you should rethink on your DB structure.我认为您应该重新考虑您的数据库结构,而不是考虑如何创建动态模式。

Instead of adding new columns in Person table you should have different table for your custom columns like create person_columns table like与其在Person表中添加新列,不如为自定义列创建不同的表,例如创建person_columns

person_columns
----------
id | people_id | column_name | column_value

so for every people you can have multiple columns and its corresponding values and you dont have to worry about dynamically creating your graphQL schema.因此,对于每个人,您都可以拥有多个列及其相应的值,并且您不必担心动态创建 graphQL 模式。 (depending on your requirement you can add more columns to person_columns table for more control) (根据您的要求,您可以向person_columns表添加更多列以获得更多控制)

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

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