Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jessicaxp opened this issue May 24, 2018 · 6 comments

Comments

@jessicaxp
Copy link

jessicaxp commented May 24, 2018

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 jessicaxp changed the title import SomeType conflicts with import * from same file, depending on order of imports importing named type conflicts with import * from same file, depending on order of imports May 24, 2018
@jessicaxp jessicaxp changed the title importing named type conflicts with import * from same file, depending on order of imports Importing named type conflicts with import * from same file, depending on order of imports May 24, 2018
@jessicaxp
Copy link
Author

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.

@EmrysMyrddin
Copy link

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 added a commit to Zenika/graphql-import that referenced this issue Jul 6, 2018
EmrysMyrddin added a commit to Zenika/graphql-import that referenced this issue Jul 6, 2018
EmrysMyrddin added a commit to Zenika/graphql-import that referenced this issue Jul 6, 2018
EmrysMyrddin added a commit to Zenika/graphql-import that referenced this issue Jul 6, 2018
@jessicaxp
Copy link
Author

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

@mikefowler
Copy link

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

@SpaceK33z
Copy link
Collaborator

SpaceK33z commented Aug 15, 2018

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

@ardatan
Copy link
Owner

ardatan commented Jan 1, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants