question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add helper methods to make it easier to modify the schema

See original GitHub issue

Product

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:closed
  • Created 6 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Apr 4, 2023

If it cannot be reached by writing a query, mutation or subscription.

1reaction
PascalSenncommented, Apr 4, 2023

@vlangber you are probably looking for this:

            services
                .AddGraphQLServer()
                .ModifyOptions(x => x.RemoveUnreachableTypes = true

Read more comments on GitHub >

github_iconTop Results From Across the Web

coding style - Helper Methods Placement
Now I need to create a private method, doStuffHelper , that doStuff calls with x and y. For readability I always put the...
Read more >
When to include helper methods in a model (MVC)?
A model class is responsible for knowing how to add stuff to it, modify stuff in it, remove stuff from it, and when...
Read more >
How to create an efficient helper method that return ...
The idea is to have a helper method that is able to return the object given a map of field-values pairs. This should...
Read more >
Helper Methods - Damely Tineo - Medium
Model helpers, for example, can help us alter or format our data before saving it our database. In the titleize method below, for...
Read more >
How to move your code out of the controller and into a helper ...
This video expands on what I said in my tutorial and actually shows you how to move your code out of the controller...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found