graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Bug UsePaging with MongoDb : is not supported for mongo paging

Open sabuydee opened this issue 1 year ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Product

Hot Chocolate

Describe the bug

When request totalCount only is error like The provided source Services.Models.ProductCategory[] is not supported for mongo paging

image

But request with nodes is normal working

image

Steps to reproduce

  1. Create project using MongoDb
  2. Service register like
services
      .AddGraphQLServer()
      .AddAuthorization()
      .AddGlobalObjectIdentification()
      .AddMongoDbPagingProviders(defaultProvider: true)
      .SetPagingOptions(new PagingOptions()
      {
        IncludeTotalCount = true
      })
      .AddMongoDbProjections()
      .AddMongoDbSorting()
      .AddMongoDbFiltering()
      .AddQueryType<Query>()
      .AddMutationConventions(applyToAllMutations: true)
      .ModifyOptions(o => o.EnableOneOf = true)
      .AddMutationType<Mutation>();
  1. Define field in Query
[UsePaging]
  [UseProjection]
  [UseSorting]
  [UseFiltering]
  public IExecutable<ProductCategory> ProductCategories([Service] DbContext context)
  {
    var query = context.ProductCategories
      .Find(e => e.ParentProductCategoryId == null)
      .AsExecutable();
    return query;
  }
  1. Execute request follow as describe

Relevant log output

{
  "errors": [
    {
      "message": "The provided source Services.Models.ProductCategory[] is not supported for mongo paging",
      "extensions": {
        "code": "HC0025"
      }
    }
  ],
  "data": {
    "productCategories": null
  }
}

Additional Context?

No response

Version

13.0.5

sabuydee avatar Mar 08 '23 00:03 sabuydee

Hi @sabuydee Thanks for reporting this issue. We have this under test, so this has to be a combination of things. Can you provide a simple repro of the issue in a github project?

PascalSenn avatar Mar 08 '23 12:03 PascalSenn

Hi @PascalSenn this is simple repo https://github.com/sabuydee/MongoHotchocolateDebug

sabuydee avatar Mar 10 '23 00:03 sabuydee

Any word on this one? Was there a work around @sabuydee?

TWhidden avatar Apr 29 '24 04:04 TWhidden

@PascalSenn This place shouldn't throw

https://github.com/ChilliCream/graphql-platform/blob/9a08ac26af7baed29f351e266debf9fffe055c86/src/HotChocolate/MongoDb/src/Data/Paging/MongoDbOffsetPagingHandler.cs#L39

But this can be solved @sabuydee

using HotChocolate;

namespace ErpLite.Backend.Application.Common.Interfaces;

public interface IUserRepository
{
    IExecutable<User> GetExecutable();
}
public class UserRepository : IUserRepository
{
    private readonly IMongoCollection<User> _users;

    public UserRepository(IMongoDbContext context)
    {
        _users = context.GetCollection<User>("Users");
    }

    public async Task<User?> GetByIdAsync(string id)
    {
        return await _users.Find(u => u.Id == id).FirstOrDefaultAsync();
    }

    public IExecutable<User> GetExecutable()
    {
        return _users.AsExecutable(); // Notice this! return .AsExecutable, but .AsQueryable().AsExecutable()
    }
[UseOffsetPaging(typeof(User), IncludeTotalCount = true)]
    [UseProjection]
    [UseSorting]
    [UseFiltering]
    [Authorize(Policy = Permissionss.Users.View)]
    public IExecutable<User> GetUsers([Service] IUserRepository userRepository) =>
        userRepository.GetExecutable();

flier268 avatar Aug 12 '24 02:08 flier268