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.23k stars 744 forks source link

[Node(NodeResolver="...")] does not work with [UseFirstOrDefault] #3177

Closed fredrik-stock closed 2 years ago

fredrik-stock commented 3 years ago

Describe the bug When pointing [Node]'s resolution for type T towards a method tagged with [UseFirstOrDefault] that returnsIQueryable<T>, resolution fails somewhere down in InterfaceType.CompleteAbstractTypeResolver() (presumably) because none of the types in the schema match IQueryable<T>.

To Reproduce

Code

[Node(NodeResolverType = typeof(ClientResolver), NodeResolver = "GetClient")]
public class Client
{
    public int Id { get; set; }
    // <snip>
}

public class ClientResolver
{
    [UseFirstOrDefault, UseProjection]
    public static IQueryable<Client> GetClient(int id, HotChocolate.Resolvers.IResolverContext resolverContext)
        =>resolverContext.Service<IDbContextFactory<DataDbContext>>().CreateDbContext().Clients.Where(c => c.Id == id);
}

Query

{
    node(id: "Q2xpZW50Cmky") { 
        id
    }
}

Result

{
  "errors": [
    {
      "message": "Could not resolve the actual object type from `System.String` for the abstract type `Bar`.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "node"
      ]
    }
  ],
  "data": {
    "node": null
  }
}

Expected behavior Expected the resolution to apply the [UseFirstOrDefault] middleware to translate the collection from IQueryable<T> to T.

Desktop (please complete the following information):

HotChocolate

Additional context The exact same method (GetClient()) exposed directly as the query client(id: ID!) works as expected.

The method also works as a node resolver if [UseFirstOrDefault] is removed and changed to return ...Clients.FirstOrDefaultAsync(...), but then projection is no longer possible.

I do not know if [UseProjection] would be applied if the query worked, or if that middleware is also skipped during node resolution.

In addition it appears the message resource ErrorHelper_ValueCompletion_CouldNotResolveAbstractType_Message is missing the interpolation variables. System.String and Bar are not what is supplied to it.

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.