Skip to content

Non-mergeable fields error when using UnionTypes #2240

@ryan-cat

Description

@ryan-cat

I am running into an issue when using UnionTypes. I receive a non-mergeable fields error when the ObjectTypes being unioned contain the same field names, but the corresponding fields have different types. I am using HotChocolate 10.4.3. I believe this should be valid GraphQL as I have done this with several other frameworks.

Here is a small example:

public class Report {
  public int Id { get; set; }
  public string Title { get; set; }
}

public class ReportType : ObjectType {
  protected override void Configure(IObjectTypeDescriptor descriptor) {
    base.configure(descriptor);
    descriptor.IsOfType((ctx, result) => result.GetType() == typeof(Report));
    descriptor.Field("id").Type<IntType>().Resolver(x => x.Parent<Report>().Id);
    descriptor.Field("title").Type<StringType>().Resolver(x => x.Parent<Report>().Title);
  }
}

public class Finding {
  public int Id { get; set; }
  public string Title { get; set; }
  public string Description { get; set; }
}

public class FindingType : ObjectType {
  protected override void Configure(IObjectTypeDescriptor descriptor) {
    base.configure(descriptor);
    descriptor.IsOfType((ctx, result) => result.GetType() == typeof(Finding));
    descriptor.Field("id").Type<IntType>().Resolver(x => x.Parent<Finding>().Id);
    descriptor.Field("title").Type<NonNullType<StringType>>().Resolver(x => x.Parent<Finding>().Title);
    descriptor.Field("description").Type<StringType>().Resolver(x => x.Parent<Finding>().Description);
  }
}

public class Union : UnionType {
  protected override void Configure(IUnionTypeDescriptor descriptor) {
    base.configure(descriptor);
    descriptor.Type<ReportType>();
    descriptor.Type<FindingType>();
  }
}

Given the schema above, ReportType and FindingType both have an id field with the same type, but a title field with different types. Assuming that the schema above has a root query named "union" that resolves a list of type Union...

This query works fine:

query Query1 {
   union {
     ... on ReportType {
       id
       title
     }
     ... on FindingType {
       id
       description
     }
   }
}

This query does not work fine, receive a non-mergeable fields error because of the mismatching types for the title field:

query Query2 {
   union {
     ... on ReportType {
       id
       title
     }
     ... on FindingType {
       id
       title
       description
     }
   }
}

Thanks in advance for any help. Let me know if you need any more info.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions