问题
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 GraphQLSchemaProvider
and 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