(outdated samples?) This graph type 'OutputObjectGraphType<Book>' with name 'Book' has already been initialized.
fgroen opened this issue · 7 comments
Hey all,
I'm in the proces of upgrading to GQL v7 and I need a little bit of help.
We have a fairly big schema and it also contains some unions. So I was waiting for the issue described in #234 to be fixed. After upgrading to v7.2 our tests are failing and I'm getting other issues now.
So, after trying to fix this for an hour, I decided to build the SimpleWebApp from the samples.
When using GraphQL.NET 7.1.1 or later I'm getting this on the first execution:
"message": "Error executing document. A different instance of the GraphType 'OutputObjectGraphType<Author>' with the name 'Author' has already been registered within the schema. Please use the same instance for all references within the schema, or use GraphQLTypeReference to reference a type instantiated elsewhere.",
And this when I execute my query another time:
"message": "Error executing document. This graph type 'OutputObjectGraphType<Book>' with name 'Book' has already been initialized. Make sure that you do not use the same instance of a graph type in multiple schemas. It may be so if you registered this graph type as singleton; see https://graphql-dotnet.github.io/docs/getting-started/dependency-injection/ for more info.",
The change log for GraphQL.NET 7.1.1 is showing me this issue I'm experiencing: graphql-dotnet/graphql-dotnet@7.1.0...7.1.1
Might this be another issue in the GraphQL.NET project or am I just totally missing something in the migration documentation?
I can confirm that GraphQL.NET 7.2.0 does not work with the conventions sample project.
I can't figure out why. For some reason GraphTypeAdapter.ConstructOutputType
while creating the Author
type, calls itself to create the Author
type a second time. Somehow it ends up that the there's two distinct copies of the Author
type created in the schema and this error occurs:
A different instance of the GraphType 'OutputObjectGraphType<Author>' with the name 'Author' has already been registered within the schema. Please use the same instance for all references within the schema, or use GraphQLTypeReference to reference a type instantiated elsewhere.
It seems that while registering the Book type, the Author type gets registered as a field of the Book type, and then when it goes to add the Author type (as all types are registered via RegisterType(instance)
) then it gives an error that there are two distinct copies.
I've checked the DI implementation; it's not that. The code certainly creates two different copies during initialization and using them within the schema.
You are correct that graphql-dotnet/graphql-dotnet#3332 is the cause of the errors you are getting. The validation was added to ensure that two instances of the same type are not registered to the same schema. If that were to happen, proper initialization and validation of one of the two instances may not occur. While I'm not sure exactly how/if that were to affect this library, we want to preserve enforcement of the check in the main GraphQL.NET project. It is possible to clone an older copy of SchemaTypes
into this project and remove the check, but I would not recommend it -- SchemaTypes
is a very delicate and complex piece of code (as we see here!).
The best way to permanently solve this issue is to reduce the sample to the minimum code/schema required to reproduce the issue. You can test for the issue easier by adding schema.Initialize();
to GraphTypeAdapter.DeriveSchema
on line 52 between schema.RegisterTypes(possibleTypes);
and return schema;
. If the initialization works, you're golden; if not, there's more bug-finding to be done.
If you can identify the cause (for example perhaps it's due to a Union or Interface type in use), you may even be able to reduce the scope further into a test. The simpler the issue, the easier it will be to solve.
Sorry, I'm not sure why the other sample isn't working. Maybe @tlil can provide some insight.
I have a suspicion of what's going on here. Thanks for flagging. Out travelling at the moment, but will take a look when I'm back on Sunday.
I have a suspicion of what's going on here. Thanks for flagging. Out travelling at the moment, but will take a look when I'm back on Sunday.
Any update @tlil ?
I have a suspicion of what's going on here. Thanks for flagging. Out travelling at the moment, but will take a look when I'm back on Sunday.
Any update @tlil ?
Been out sick, sorry. Found the issue and will try and get a fix out by early next week.
@Shane32 see:
As discovered in #237, and after the addition of integrity checks in the parent library (graphql-dotnet/graphql-dotnet#3332), the Convention library has been shown to create duplicate instances of derived graph types in some cases due to insufficient caching logic.
Previously, this didn't flag since there were no integrity checks to look for this kind of behaviour. The types are derived deterministically, so although this miss caused usage with the latest version of the library to bark on query execution, there should be no logical changes in the way schemas are derived and evaluated (bar not triggering an error, obviously!) with this change.
It offers a fix to the issue outlined above. Looking at this (now old) code, there's a few things I'd like to do to tidy up the logic. However, I'll save that for subsequent PRs. :-)