ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.23k stars 745 forks source link

Enums are included in schema without being referenced when properties are ignored by ExtendObjectType #5926

Closed craigbehnke closed 4 months ago

craigbehnke commented 1 year ago

Is there an existing issue for this?

Product

Hot Chocolate

Describe the bug

I have tried to remove properties from a type within a subgraph, but while the property is hidden from the object type, the types backing those properties are still included.

I would expect that the enum, since it is not used anywhere, would not be included.

AccountTypeEnum is not used anywhere else.

Steps to reproduce

  1. Create an example base object:
    
    public class Account
    {
    public string Id {get;set;}
    public string Name {get;set;}
    public AccountTypeEnum AccountType {get;set;}
    }

public enum AccountTypeEnum { CreditCard, Checking, Savings }

2.  Extend the class to remove extra properties:
```c#
[ExtendObjectType(typeof(Account),IgnoreProperties = new[] {
    nameof(Account.AccountType),
    nameof(Account.Name)
})]
public class AccountReduction
{
}
  1. Observe the erroneous results in the following schema (abridged):
    
    type Account {
    id: String!
    }

enum AccountTypeEnum { CREDIT_CARD CHECKING SAVINGS }


4. Verify that AccountTypeEnum is not used anywhere else in the schema. (i.e. is orphaned)

### Relevant log output

_No response_

### Additional Context?

As for why I am doing this: I am building multiple subgraphs within the same repo in order to reduce cold start times. To make things easier (haha) on myself, I am also using a common library with model definitions for interacting with the database across all subgraphs.

In order to not violate the single responsibility principle, I want to only return non-@key data from one subgraph.

However, updating/adding a member to an enum defined in multiple subgraphs results in a race condition within the Apollo GraphOS/Rover schema building system, which is how I found this issue originally.

### Version

13.0.4
craigbehnke commented 1 year ago

Workaround for anyone in the future that is hitting the same problem: there is a workaround for this in the documentation already. (https://chillicream.com/docs/hotchocolate/v13/distributed-schema/schema-configuration#ignore-types)

So my startup code now reads (in part):

builder.Services
       .AddGraphQLServer()
       // All of my other configuration
       .ModifyOptions(x => x.RemoveUnreachableTypes = true);

Still, it is strange that this was the default behavior.

michaelstaib commented 4 months ago

This one is fixed with the new ObjectTypeAttribute which replaces the ExtendObjectTypeAttribute