exposing dynamic schemas with graphql

一个人想着一个人 提交于 2020-07-07 14:20:26

问题


My application handles schema on the fly. Users can upload new domains or change the definitions of existing domains. For ex, say, I ship the product with a user schema like this :

user { 
  fn, ln, age
}
Later user can change this definition to include new properties;
user { 
  fn, ln, age, 
  salary, address
}

or they can add an entirely new domain concept.

If I use GraphQL, is there a way to change the schema dynamically ?

Another question is; Our schemas have conditions that make some parts of the schema relevant. For ex; the user schema may have something like this:

user {
  fn, ln, age, gender
  foo (if gender == Male)
  bar (if gender == Female)
}

are there provisions in GraphQL to express these. I am ok to handle it in java code, but, then the schema look ups need to be routed to my code rather than referring to a static schema registry.


回答1:


I had to solve the same kind of problem: I have a graphQL java server which can load plugins dynamically (each plugins come with its own schema definition).

You have to implement your own GraphQLSchemaProviderand override public GraphQLSchema getSchema(HttpServletRequest request). Then load / reload your schema according to refresh time / request condition (ie: user) / anything else...

For your second question, you can use Inline fragment http://graphql.org/learn/queries/#inline-fragments



来源:https://stackoverflow.com/questions/46994102/exposing-dynamic-schemas-with-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!