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.27k stars 748 forks source link

Two different input types applied to the same entity #2227

Closed zivfuture closed 2 years ago

zivfuture commented 4 years ago

I have a product mutation api (add & update & ...) like this

public int AddProduct(ProductInput input) { ......}
public int UpdateProduct(ProductInput input) { ......}

public class ProductInput
{
    public int Id{get;set;}
    public string Code{get;set;}
    public string Name{get;set;}
    ......
}

As you see. many apis has same argument. but the field's nullable is diffent. For UpdateProduct api. the product's id must not be null, the other fields can be null. For AddProduct api ,It's the opposite.

I don't want to create so many same classes except diffent nullable.

Don't repeat you self.

I tried to create diffent graphql type to descripe the nullable.

#nullable enable
public class ProductInput
{
    public int? Id{get;set;}
    public string? Code{get;set;}
    public string? Name{get;set;}
    ......
}
public class ProductUpdateInputType : ObjectType<ProductInput>
{
    protected override void Configure(IObjectTypeDescriptor<ProductInput> descriptor)
    {
        descriptor.Field(x => x.ID).Type<NonNullType<IntType>>();
    }
}
public class ProductAddInputType : ObjectType<ProductInput> 
{
    protected override void Configure(IObjectTypeDescriptor<ProductInput> descriptor)
    {
        descriptor.Field(x => x.Code).Type<NonNullType<StringType>>();
        descriptor.Field(x => x.Name).Type<NonNullType<StringType>>();
        ......
    }
}
//what should id do next step? 
benmccallum commented 4 years ago

I've never tried this, but it should work. You'd need to be inheriting from InputObjectType<T> though.

Then you should just call .AddType<ProductUpdateInputType>().AddType<ProductAddInputType>() and see what happens!

zivfuture commented 4 years ago

@benmccallum i got an error when start the application

benmccallum commented 4 years ago

If you post the error you're getting we might be able to help

damikun commented 4 years ago

I think something like: the same type or key was already registered :) @zivfuture what is the error? I`m not fully sure if you can have 2x object types on same object..

zivfuture commented 4 years ago

@benmccallum @damikun
I think something like: the same type or key was already registered . yes , this is the error.

benmccallum commented 4 years ago

Looking at this again, it's worth noting that the naming conventions in HotChocolate derive the name from the backing type, in this case ProductInput, so unless you specify the name explicitly, with descriptor.Name("SomeName") and a diff for the other, there's of course going to be a conflict.

Notably this varies from GraphQL-DotNet which derives it from the wrapping type.

zivfuture commented 4 years ago

@benmccallum @benmccallum Sorry, I cat't catch your mean. where to specify the name? I try to change the api code

public class Mutation
{
    public int AddProduct(ProductInput input) { ......}
    public int UpdateProduct(ProductInput input) { ......}
}

to

public class Mutation
{
    public int AddProduct(ProductAddInputType input) { ......}   //change ProductInput  to ProductAddInputType 
    public int UpdateProduct(ProductUpdateInputType input) { ......}  //
}

. I failed. Error is HotChocolate.SchemaException:“Unable to resolve type reference `Input: GraphQL.ProductAddInputType `. - Type: Mutation”

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.