graphql-import icon indicating copy to clipboard operation
graphql-import copied to clipboard

Importing named type conflicts with import * from same file, depending on order of imports

Open jessicaxp opened this issue 7 years ago • 6 comments

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.

jessicaxp avatar May 24 '18 20:05 jessicaxp

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.

jessicaxp avatar May 27 '18 00:05 jessicaxp

I have the same problem and found out what's going on.

the probleme is here

    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 avatar Jul 06 '18 10:07 EmrysMyrddin

@EmrysMyrddin That's definitely the sort of piece of code I assumed existed somewhere!

jessicaxp avatar Jul 06 '18 22:07 jessicaxp

@schickling thoughts on this? @EmrysMyrddin has a proposal PR open that would fix this issue

mikefowler avatar Jul 12 '18 21:07 mikefowler

I'm only seeing this issue now but PR #213 will fix this too.

SpaceK33z avatar Aug 15 '18 16:08 SpaceK33z

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.

ardatan avatar Jan 01 '20 18:01 ardatan