-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
This error exists in both 14.x and 15.x branches, and has a one-line tested fix, so presenting that here rather than in multiple pull requests.
Issue:
In either branch latest, graphql fails immediately for certain schemas, reporting:
Error: Schema must contain uniquely named types but contains multiple types named "Name"
where for my schema Name may be PageInfo or Mutation.
Others have reported similar problems, also for graphql as used in Gridsome.
Response:
Tracking this down via the stack trace and logging, the problem is that function typeMapReducer()
in type/schema.js is doing a compare of objects for the Type name, to determine failure.
if (seenType) {
if (seenType !== namedType) {
throw new Error("Schema must contain uniquely named types but contains multiple types named \"".concat(namedType.name, "\"."));
}
...
Adding a cast to String before comparing solves this. The code becomes:
if (seenType) {
if (String(seenType) !== String(namedType)) {
throw new Error("Schema must contain uniquely named types but contains multiple types named \"".concat(namedType.name, "\"."));
}
...
This code appears at apparent lines 338 in type/schema.js source for 14.x, and 319 in 15.x source, as these appear on this github repo.
It seems both branches should be repaired with fresh release, especially14.x, as many are using that in the field. Thanks.
Rationale:
The commonality for the PageInfo and Mutation terms that fail with present code is that they appear in multiple graphql Connections.
It's possible that the uniqueness test really does intend to compare objects, but that the situation is complicated by the fact that in Gridsome (also potentially in Gatsby, perhaps others), there are data-source-resolving prefixes added to the actual declaration of some terms, which arrive early in the map
array for typeMapReducer()
.
I'm attaching a log of that map
array, as in use at the point where the failure occurs. so you can see the use of prefixes.
If the prefixes are the problem, then this 'fix' becomes a workaround, until a proper design can accomodate.