gneric
gneric

Reputation: 3685

Can multiple HotChocolate ObjectTypes of the same Type can be registered and used in different queries?

We receive our data from the EF Core Model directly. Currently there is one registered ObjectType with configuration. Is it possible to register a second object type of the same Type with different configuration (properties to ignore, different property namings etc). Both should be used in different Query methods ? I tried but got an error that: The name 'X' was already registered by another type.

Example:

ObjectType<Doc> - this is already created that maps to the EF Core model

ObjectType<DocSimplified> - I want to create a second one with subset of props to return and different namings

getDoc() query uses the first

getDocSimplified() - this second query that I wish to return the second simplified objecttype

Upvotes: 0

Views: 419

Answers (2)

steve84
steve84

Reputation: 233

Just to add a bit more detail to Luk's answer above, in the scenario you described, if using the code-first approach, you would do something like this:

public class DocType : ObjectType<Doc>
{
}

public class SimplifiedDocType : ObjectType<Doc>
{
    protected override void Configure(IObjectTypeDescriptor<Doc> descriptor)
    {
        descriptor.Name("SimplifiedDoc");
        descriptor.Ignore(d => d.Foo); // etc ...
    }
}

public class Query
{
    [GraphQLType<DocType>]
    public Doc GetDoc(int id) { ... }

    [GraphQLType<SimplifiedDocType>]
    public Doc GetSimplifiedDoc(int id) { ... }
}

Upvotes: 0

Luk Deathrage Prochzka
Luk Deathrage Prochzka

Reputation: 366

Every object type must have unique name. However, same class can be used as base for multiple object types.

Changing name can be achieved multiple ways. First of all, if using annotation attribute then placing [GraphQLName("NewName")] on the class will change it's object type's name to NewName. When using code first approach (writing a class inheriting ObjectType) the name can be changed by calling descriptor.Name("NewName") in the Configure method.

Upvotes: 1

Related Questions