0

Ok so I am new to GraphQL so may be missing something here. However, when I am trying to write my tests for my queries, I am getting syntax errors which I just cannot work out. So starting from the top.

Here is my Domain Object and Context:

public class Venue : Entity<Guid>
{
    public string Name { get; private set; }

    public static Venue Create(string name)
    {
        return new Venue
        {
            Id = Guid.NewGuid(),
            Name = name,
            DateCreated = DateTime.Now,
            DateModified = DateTime.Now
        };
    }

    public void UpdateName(string name)
    {
        Name = name;
    }
}
public class Context : DbContext
{
    private readonly IEventPublisher _eventPublisher;

    public Context(IEventPublisher eventPublisher, DbContextOptions<Context> options) : base(options)
    {
        _eventPublisher = eventPublisher;
    }

    public DbSet<TestEntity> TestEntities { get; set; }
    public DbSet<Venue> Venues { get; set; }
    public DbSet<Area> Areas { get; set; }
}

Here is my GraphQL Query:

public partial class Query
{
    public Venue GetVenue([Service] Context context, Guid id) => context.Venues.SingleOrDefault(v => v.Id == id);
}

Now this all works if I just hit the GraphQL endpoint using my application or Banana Cake Pop application.

My issue comes in the tests.

So in my test project I have setup a TestService as they suggest in their YouTube tutorial:

public static class GraphQlTestServices
{
    static GraphQlTestServices()
    {
        Services = new ServiceCollection()
            .AddPooledDbContextFactory<Context>(
                options => options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Allertons.Api.Test-LocalDev;Trusted_Connection=True;ConnectRetryCount=0;MultipleActiveResultSets=True;"))
            .AddGraphQL()
            .AddQueryType<Query>()
            .AddAuthorization()
            .AddProjections()
            .AddFiltering()
            .AddSorting()
            .AddType<Venue>()
            .AddType<Area>()
            .Services
            .AddSingleton(sp => new RequestExecutorProxy(
                sp.GetRequiredService<IRequestExecutorResolver>(),
                HotChocolate.Schema.DefaultName))
            .BuildServiceProvider();

        Executor = Services.GetRequiredService<RequestExecutorProxy>();
    }
    
    public static IServiceProvider Services { get; }
    
    public static RequestExecutorProxy Executor { get; }

    public static async Task<string> ExecuteRequestAsync(
        Action<IQueryRequestBuilder> configureRequest,
        CancellationToken cancellationToken = default)
    {
        await using var scope = Services.CreateAsyncScope();

        var requestBuilder = new QueryRequestBuilder();
        requestBuilder.SetServices(scope.ServiceProvider);
        configureRequest(requestBuilder);
        var request = requestBuilder.Create();

        using var result = await Executor.ExecuteAsync(request, cancellationToken);

        result.ExpectQueryResult();

        return result.ToJson();
    }
}

And then I am using this in my tests as follows:

[Fact]
public async Task FetchVenues()
{
    var result = await GraphQlTestServices.ExecuteRequestAsync(b => b.SetQuery(@"
        venue(id: ""facd7dfc-5075-4ef5-b9a6-5514508b6bd9"") 
        {
            id
        }"));
        
    result.MatchSnapshot();
}

The snapshot created by SnapShotter from this test is:

{
  "errors": [
    {
      "message": "Unexpected token: Name.",
      "locations": [
        {
          "line": 2,
          "column": 13
        }
      ],
      "extensions": {
        "code": "HC0014"
      }
    }
  ]
}

I know from looking at this issue, the HC0014 error code indicates a GraphQL Document Syntax Error but I have no idea where this is coming from.

Does anybody have any advice of how to debug this further or can see the issue?

Thanks!

DaRoGa
  • 2,196
  • 8
  • 34
  • 62

1 Answers1

0

According to the graphql rules you have to place your query inside curly braces with or without keyword query, i.e.

{
  [your query body here]
}

or

query
{
  [your query body here]
}

Since you didn't do that you, obviously, got an error.

ademchenko
  • 585
  • 5
  • 18