Hot Chocolate how ignore extra input fields

Viewed 598

It often happens that the input argument has an extra field, resulting in an error:

"The specified input object field myField does not exist."

How can I ignore extra fields?

My query:

mutation {  
  create(author: {id:0, name:"example", myField:0}){
    id
  }
}

result:

{
  "errors": [
    {
      "message": "The specified input object field `myField` does not exist.",
      "locations": [
        {
          "line": 2,
          "column": 41
        }
      ],
      "path": [
        "create"
      ],
      "extensions": {
        "field": "myField",
        "specifiedBy": "http://spec.graphql.org/October2021/#sec-Input-Object-Field-Names"
      }
    }
  ]
}

For example, when receiving data in Appollo, the client adds an additional service field, and after sending this object for saving, we immediately get an error. Well, we can tell Appollo not to add this service field, although it needs it for caching, but several times from the developer's frontend a service value is added to the object, as a result of which nothing works again. It turns out that you need to do data mapping, and write a bunch of additional code, and so every time.

For example, in js I got the author then author.myField = true; and that's all I can't save it anymore. I understand that it's not very good to pass an extra field, but it turns out that you need to upgrade the frontend service every time that interacts with graphql, as soon as the service field is added. As a result, you just have to figure out why everything broke down.

2 Answers

Maybe I’m misunderstanding you, but why is the myField property passed into the mutation at all if it is not part of the author type?

And just by guessing from the input type name Author: are you aware that input types should be separate types and not reused normal types?

You can do it in two ways, first option, BindFieldsExplicitly, with this you will tell Hot Chocolate which fields to want to expose:

public class UserFilterType : FilterInputType<User>
{
    protected override void Configure(
        IFilterInputTypeDescriptor<User> descriptor)
    {
        descriptor.BindFieldsExplicitly();
        descriptor.Field(f => f.Name);
    }
}

Or the second options is just to ignore the fields.

public class UserFilterType : FilterInputType<User>
{
    protected override void Configure(
        IFilterInputTypeDescriptor<User> descriptor)
    {
        descriptor.Field(f => f.CreatedAt).ignore();
    }
}

You can find more here: https://chillicream.com/docs/hotchocolate/fetching-data/filtering#customization

Related