Importing named type conflicts with import * from same file, depending on order of imports
I'm using type extension in my project and just ran into an issue that took me a while to figure out. I was following along with the comments in Type extension does not work and found an issue where importing named vs. wildcard types from the same file can result in the wildcard import being clobbered, based on the order that the referencing items are imported.
Consider the following type definitions: SomeType.graphql
type SomeType {
name: String!
}
type Mutation {
changeSomeType(name: String!): SomeType
}
Mutation.graphql
# import Mutation.* from './SomeType.graphql'
Query.graphql
# import SomeType from './SomeType.graphql
type Query {
getMyType: SomeType
}
If the definition using the named import is imported first, as in this example: Schema.graphql
# import Query from './Query.graphql'
# import Mutation from './Mutation.graphql'
type Schema {
query: Query,
mutation: Mutation
}
Running this code will return an error "Mutation" defined in resolvers, but not in schema
However, if the definition using the wildcard import is imported first, as in this example: Schema.graphql
# import Mutation from './Mutation.graphql'
# import Query from './Query.graphql'
type Schema {
query: Query,
mutation: Mutation
}
The code will run fine.
I imagine there's some filename-based caching or de-duping going on. Not sure whether this is intentional behavior, but if it is I'd recommend clarifying in the import documentation.
Another bit of info - This fix only works in version 0.5.2
If I upgrade to 0.6.0, the "Mutation" defined in resolvers, but not in schema error persists regardless of the order of those statements, and I cannot seem to get it to run.
I have the same problem and found out what's going on.
if (!processedFiles.has(moduleFilePath)) {
collectDefinitions(
m.imports,
read(moduleFilePath, schemas),
moduleFilePath,
schemas,
processedFiles,
typeDefinitions,
allDefinitions,
)
}
Here, we check if the file has already been imported, which is good.
But we should also take in account that m.imports is possibly not the same.
To fix that, we should check for the file and the named imports given. This will probably be a bit tricky with * and Query.* or Mutation.*.
But I can try to open a PR for this.
@EmrysMyrddin That's definitely the sort of piece of code I assumed existed somewhere!
@schickling thoughts on this? @EmrysMyrddin has a proposal PR open that would fix this issue
I'm only seeing this issue now but PR #213 will fix this too.
Hi @jessicaxp ! In 1.0.0 beta release, we introduced a lot of changes; Could you install graphql-import@beta to try new changes? Don't forget to modify your code regarding to the migration notes in README. https://github.com/ardatan/graphql-import#updating-from-07x
Let us know if the problem persists.