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

Handling duplicate "Query" and "Mutation" types #92

Open
deklanw opened this issue May 4, 2018 · 6 comments
Open

Handling duplicate "Query" and "Mutation" types #92

deklanw opened this issue May 4, 2018 · 6 comments

Comments

@deklanw
Copy link

deklanw commented May 4, 2018

I'm using a file structure which I believe is common. I have multiple files like "Post.graphql" and "User.graphql", each with their own distinct types. But, each also has a "Query" and "Mutation" type. I use merge-graphql-schemas to merge them together, which works fine functionally.

But, with this plugin I'm getting this linting error:

[graphql] Schema must contain unique named types but contains multiple types named "Query"

Similarly for "Mutation".

Aside from changing my structure such that all the queries and mutations are in the same file, how can I work around this error?

@kumarharsh
Copy link
Owner

kumarharsh commented May 4, 2018

This would be addressed in the GQL plugin.

@Mayank1791989 this usecase is what we were discussing about today, no?

@deklanw
Copy link
Author

deklanw commented May 4, 2018

I thought that might be the case. Should I open this same issue over there?

@Mayank1791989
Copy link
Collaborator

We only support the official graphql spec and according to spec you can't have two type definition with same name. If you can change how you write graphql files then there are two options

  1. Use graphql schema extension (I dont know whether merge-graphql-schema supports it)
# post.graphql
type Post {
  id: String!
  name: String!
}

extend type Query {
   posts: [Post!]
}

extend type Mutation {
  addPost(name: String!): Post!
}
# user.graphql
type User {
  id: String!
  name: String!
}

extend type Query {
   users: [User!]
}

extend type Mutation {
  addUser(name: String!): User!
}
  1. Write all your types in separate files but merge all query and mutation.
# post.graphql
type Post {
  id: String!
  name: String!
}
# user.graphql
type User {
  id: String!
  name: String!
}
# query.graphql
type Query {
   posts: [Posts!]
   users: [User!]
}
# mutation.graphql
type Mutation {
   addPost(name: String!): Post!
   addUser(name: String!): User!
}

@deklanw
Copy link
Author

deklanw commented May 5, 2018

It appears the first option doesn't work with merge-graphql-schema (or I'm doing something wrong). I opened an issue there.

The second option will do for now.

Thanks for the quick replies!

@nfantone
Copy link

nfantone commented Jun 6, 2018

@kumarharsh Has this been "addressed" by the GQL plugin as mentioned some months back now?

This is still a detrimental issue that doesn't play well with a common pattern.

@Renaud009
Copy link

Both options doesn't work for me since I'm using merge-graphql-schema too and extending types just fail and my main Mutation object is generated by Prisma :-(

I actually want to extend Prisma entities and mutations with authorization directives.

There's no way currently to just deactivate "unique named types" check?

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

No branches or pull requests

5 participants