简体   繁体   中英

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.

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

This table will be exposed to a GraphQL API and I can query the table. In my system, the user will be able to add custom columns to the people table. 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.

So how can I make my schema dynamic that allows the user to query the people table with every extra column they add?

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.

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. 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_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. (depending on your requirement you can add more columns to person_columns table for more control)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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