mmacneil / ASPNetCoreGraphQL

Sample project demonstrating a GraphQL service built with ASP.NET Core 2.2, Entity Framework Core and graphql-dotnet.
https://fullstackmark.com/post/17/building-a-graphql-api-with-aspnet-core-2-and-entity-framework-core
MIT License
265 stars 123 forks source link

Error deleting an entity after adding it #2

Open jcsilvers opened 5 years ago

jcsilvers commented 5 years ago

Current build does not allow you to create an entity and then with another mutation delete that entity.

Steps to reproduce: 1) Create Add Mutation 2) Create Delete Mutation 3) Add item

mutation ($day: DayInput!)  {
  createDay(day:$day){
    id
    agendaDay
  }
}

mutation ($day: DayInput!)  {
  createDay(day:$day){
    id
    agendaDay
  }
}

4) Immediately try and delete

mutation ($dayId: Int!)  {
  deleteDay(dayId:$dayId){
    id
    agendaDay
  }
}

{

    "dayId" : "7004"

}

I get the following error

{
  "data": {
    "deleteDay": null
  },
  "errors": [
    {
      "message": "There was an error deleting 7004: The instance of entity type 'Day' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "deleteDay"
      ]
    }
  ]
}

I believe this line is the reason

var sp = services.BuildServiceProvider();
services.AddSingleton<ISchema>(new MITSSchema(new FuncDependencyResolver(type =>sp.GetService(type))));

I replaced it with below and it works as expected.

services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
omar84 commented 5 years ago

services.AddScoped(s => new FuncDependencyResolver(s.GetRequiredService));

this generated errors on graphiql for me

mmacneil commented 5 years ago

Hi @omar84 - do you see the same error @jcsilvers reported? I fixed an issue a couple of weeks ago with the scoping of the repository dependencies that I believe were the root cause of @jcsilvers issue but if you're still seeing it perhaps there is still something lingering.

omar84 commented 5 years ago

@mmacneil I didn't try his scenario to be honest, I just thought, if there is an issue, I would want to fix it :)

if you believe its already fixed, then this should probably be closed

mmacneil commented 5 years ago

ok thanks..so do you get an error (in any scenario) in graphiql?

On Mon, Apr 1, 2019, 5:37 PM Omar M., notifications@github.com wrote:

@mmacneil https://github.com/mmacneil I didn't try his scenario to be honest, I just thought, if there is an issue, I would want to fix it :)

if you believe its already fixed, then this should probably be closed

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mmacneil/ASPNetCoreGraphQL/issues/2#issuecomment-478737890, or mute the thread https://github.com/notifications/unsubscribe-auth/AAXRu2bgFRD1AnM4wNTyXnT_D4XnzYxiks5vcm4BgaJpZM4ZLtkR .

omar84 commented 5 years ago

not with your code, but with what @jcsilvers suggested