Add helper methods to make it easier to modify the schema
See original GitHub issueProduct
Hot Chocolate
Is your feature request related to a problem?
If you add a base classwith the AddType, Hot Chocolate will automatically add the sub classes as well.
We have two requirements:
- Possible to have more than one endpoint with different subsets of the whole schema.
- Some of the subclasses of base class should not be included in any of the schemas
From what we have been able to figure out, it is hard to modify the types in a schema as it is now. You can remove fields relatively easily with a TypeInterceptor, but it is not easy to remove a type from the schema. Yes, we can use Dynamic Schemas, but that means quite a bit of work, and it introduces a lot of complexity for something that is almost perfect with a much simpler implementation.
The solution you’d like
We see two possible solutions that we think will improve Hot Chocolate and they should be relatively easy to make:
1.) Create an overload for AddType where subclasses aren’t added automatically, so we can control which types we add to the schema. This would allow us to do something like this to build the schema:
var allTypes = StaticContext.GetAllContentTypes().Where(p => p.IsClass && !p.IsAbstract && p.GetTypeInfo().IsSubclassOf(typeToSearch)).ToList();
var filteredTypes = allTypes.Where(t => !excludedTypes.Contains(t)).ToList();
foreach (var type in filteredTypes) {
builder.AddType(type);
}
2.) Create some helper methods to alter the schema after it has been generated. Entity GraphQL have some methods you could use for inspiration that would solve our usecases (https://entitygraphql.github.io/docs/schema-creation#modifying-the-generated-schema):
schema.UpdateType<Person>(personType => {
personType.RemoveField("firstName");
personType.ReplaceField(
"lastName",
p => p.LastName.ToUpper(), // new expression to resolve the lastName field
"New description"
);
});
schema.RemoveType<TType>();
schema.RemoveType("TypeName");
// Remove a type and all fields that return that type
schema.RemoveTypeAndAllFields<Type>();
Issue Analytics
- State:
- Created 6 months ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
If it cannot be reached by writing a query, mutation or subscription.
@vlangber you are probably looking for this: