1

I'm just trying to set up some basic examples. I want to go "code first", I need the C# compiler to tell me where I'm going wrong.

If I look at https://chillicream.com/docs/hotchocolate/v10/schema/object-type/

there is an example

type Person {
  id: Int!
  name: String!
  friends: [Person]
}

which allegedly is the result of this C# code

public class PersonType
    : ObjectType<Person>
{
    protected override void Configure(IObjectTypeDescriptor<Person> descriptor)
    {
        descriptor.Field(t => t.Name).Type<NonNullType<StringType>>();
        descriptor.Field("friends")
            .Type<ListType<NonNullType<StringType>>>()
            .Resolver(context =>
                context.Service<IPersonRepository>().GetFriends(
                    context.Parent<Person>().Id));
    }
}

Ok, so id is missing, but the documentation goes on to explain that hot chococolate will fill in the gaps (something I'm not a fan of...but putting that aside). then we have "name", which is a string, and this would seem to correspond with

.Type<NonNullType<StringType>>()

ok, thats believable, "friends" though seems bizarre. the "Type" is

.Type<ListType<NonNullType<StringType>>>()

where I would expect something that more obviously mapped to "[Person]"

Is the doc wrong? or is my understanding wrong?

MrD at KookerellaLtd
  • 2,412
  • 1
  • 15
  • 17

1 Answers1

2

First of, yes, the docs are wrong there. Will be fixed in this PR: https://github.com/ChilliCream/hotchocolate/pull/2890

The correct code would be

public class PersonType
    : ObjectType<Person>
{
    protected override void Configure(IObjectTypeDescriptor<Person> descriptor)
    {
        descriptor.Field(t => t.Name).Type<NonNullType<StringType>>();
        descriptor.Field("friends")
            .Type<ListType<NonNullType<PersonType>>>()
            .Resolver(context =>
                context.Service<IPersonRepository>().GetFriends(
                    context.Parent<Person>().Id));
    }
}

Ok, so id is missing, but the documentation goes on to explain that hot chococolate will fill in the gaps (something I'm not a fan of...but putting that aside).

You can partially turn this off if you do not like it. HotChocolate will always try to infer the type from the .NET if it was not set. But if you want to disable the inferring of properties you can do this in two ways.

  1. Disable it globally
schemaBuilder.ModifyOptions(x => x.DefaultBindingBehavior = BindingBehavior.Explicit)

  1. Disable it per type
public class PersonType
    : ObjectType<Person>
{
    protected override void Configure(IObjectTypeDescriptor<Person> descriptor)
    {
         descriptor.BindFieldsExplicitly();
    }
}
Pascal Senn
  • 1,712
  • 11
  • 20
  • sorry, very cheeky of me, I've come back to this, but now I want to use oracel which means I need dotnet 3.1, which I think means I need hot chocolate 10? Do you know of any good run through for EF and hot chocolate 10?...I'm really struggling with it. – MrD at KookerellaLtd Jan 18 '21 at 09:33
  • I'm basically doing an assessment of it, I've got 2 possible use cases, but this one, I just want to scaffold an oracle database, and expose it, hopefully via the reflexive mechanism, so I'm hoping almost 0 hot chocolate code. – MrD at KookerellaLtd Jan 18 '21 at 09:45
  • HotChocolate 11 does also work with netcoreapp 3.1 :) – Pascal Senn Jan 18 '21 at 16:22
  • ah...when I used 11, I get some sort of version conflict, which maybe I'll have another go and post the error. – MrD at KookerellaLtd Jan 19 '21 at 18:51
  • you're completely correct, I've upgraded to 11, and dotnet 5 and it all worked fine, tbh, its the oracle libraries that seem to cause my versioning issues, they seem to be 12 months or more behind the latest EF releases – MrD at KookerellaLtd Jan 22 '21 at 14:56