Hotchocolate: Incorrect filter SQL query operator

Created on 7 Feb 2020  ยท  2Comments  ยท  Source: ChilliCream/hotchocolate

Describe the bug
SQL query formed through EF Core, based on LINQ expression, is not correct, since it forms 'AND' operator on the place where 'OR' operator should be.
Here is GraphQL query:
graphql query { posts( first: 5 where: { title_contains: "John", OR: { title_contains: "Doe" } } ) { edges { node { id title } } } }

SQL query formed by this GraphQL query is next:
sql SELECT TOP(@__p_0) [p].[Id], [p].[Title] FROM [Posts] AS [p] WHERE ([p].[Title] IS NOT NULL AND (CHARINDEX(N'John', [p].[Title]) > 0)) AND ([p].[Title] IS NOT NULL AND (CHARINDEX(N'Doe', [p].[Title]) > 0))

To Reproduce
Create following classes:
````csharp
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseSerilog((ctx, res) =>
            {
                res.WriteTo.Console(LogEventLevel.Information);
            });
}

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(x => x.UseSqlServer("")
.EnableSensitiveDataLogging(), ServiceLifetime.Transient);
services.AddGraphQL(SchemaBuilder.New()
.AddQueryType()
.Create());
}

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseGraphQL();
    }
}

public class Query
{
[UsePaging]
[UseFiltering]
[UseSorting]
public IQueryable GetPosts([Service] PostDbContext db) => db.Posts;
}

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

public class PostDbContext : DbContext
{
    public PostDbContext(DbContextOptions<PostDbContext> options) : base(options) { }
    public virtual DbSet<Post> Posts { get; set; }
}

Using following configuration: xml


netcoreapp3.1










````

Expected behavior
Expected SQL query would be the following:
sql SELECT TOP(@__p_0) [p].[Id], [p].[Title] FROM [Posts] AS [p] WHERE ([p].[Title] IS NOT NULL AND (CHARINDEX(N'John', [p].[Title]) > 0)) OR ([p].[Title] IS NOT NULL AND (CHARINDEX(N'Doe', [p].[Title]) > 0))

โ“ question

Most helpful comment

Hi @PascalSenn
I wasn't aware it works like this. As the matter of fact, it has more sense the way you put it together. I've tested your query with my setup and it works. Thanks!

From my point of view, this issue can be closed, unless you think there is a need for a case like mine to work as i expected too.

All 2 comments

hi @rankdalibor
Thank you for putting all of this together!

The OR filters work slightly different.
Try this:

query {
  posts(
    first: 5
    where: { OR: [{ title_contains: "Doe" }, {title_contains: "John"}] }
  ) {
    edges {
      node {
        id
        title
      }
    }
  }
}

Hi @PascalSenn
I wasn't aware it works like this. As the matter of fact, it has more sense the way you put it together. I've tested your query with my setup and it works. Thanks!

From my point of view, this issue can be closed, unless you think there is a need for a case like mine to work as i expected too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IKolosynskyi picture IKolosynskyi  ยท  3Comments

marcin-janiak picture marcin-janiak  ยท  4Comments

nigel-sampson picture nigel-sampson  ยท  5Comments

PascalSenn picture PascalSenn  ยท  5Comments

louisjrdev picture louisjrdev  ยท  3Comments