Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL.ExecutionError #1063

Closed
RaySheikh opened this issue Mar 15, 2019 · 1 comment
Closed

GraphQL.ExecutionError #1063

RaySheikh opened this issue Mar 15, 2019 · 1 comment

Comments

@RaySheikh
Copy link

RaySheikh commented Mar 15, 2019

Hi, I recently started getting the error below after publishing my project specially when more users started using the app. I am using asp.net core 2.1 with EF core repository pattern. This completely locks up the server. Any help would be greatly appreciated. This will randomly happen with different queries..

GraphQL.Controllers.GraphQLController:Error: GraphQL errors: GraphQL.ExecutionError: Error trying to resolve currentUser. ---> System.InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSectionAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
   at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken)

Startup.cs

services.AddDbContext<AppContext>(options => options.UseSqlServer(appconn, opt => opt.EnableRetryOnFailure()));

            //DI
            services.AddScoped<IAppRepository, AppRepository>();
            services.AddScoped<AppSchema>();
            services.AddScoped<AppQuery>();
            services.AddScoped<AppMutation>();
            services.AddScoped<UserType>();
            services.AddScoped<IDocumentExecuter, DocumentExecuter>();
            var sp = services.BuildServiceProvider();
            services.AddSingleton<ISchema>(new AppSchema(new FuncDependencyResolver(type => sp.GetService(type))));

            services.AddGraphQL(options =>
            {
                options.EnableMetrics = true;
                options.ExposeExceptions = this.Environment.IsDevelopment();
            });

controller.cs

[Route("[controller]")]
    public class GraphQLController : Controller
    {
        private IDocumentExecuter _documentExecuter { get; set; }
        private ISchema _schema { get; set; }
        private readonly ILogger _logger;

        public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter, ILogger<GraphQLController> logger)
        {
            _schema = schema;
            _documentExecuter = documentExecuter;
            _logger = logger;
        }

        [HttpGet]
        public IActionResult Index()
        {
            _logger.LogInformation("Got request for GraphiQL. Sending GUI back");
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
        {
            if (query == null) { throw new ArgumentNullException(nameof(query)); }

            var inputs = query.Variables.ToInputs();
            var executionOptions = new ExecutionOptions()
            {
                Schema = _schema,
                Query = query.Query,
                Inputs = inputs
            };
            var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);

            if (result.Errors?.Count > 0)
            {
                _logger.LogError("GraphQL errors: {0}", result.Errors);
                return BadRequest(result);
            }
            _logger.LogDebug("GraphQL execution result: {result}", JsonConvert.SerializeObject(result.Data));
            return Ok(result);
        }
    }

Query.cs

Field<UserType>(
                "currentUser",
                description: "Get current user",
                arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
                resolve: context => repo.GetUserByIdAsync(context.GetArgument<int>("id"))
                );
@joemcbride
Copy link
Member

This is related to #648 . Please read through that thread.

Question
#648 (comment)

Answer
#648 (comment)

A fix:
#648 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants