graphql-for-vscode icon indicating copy to clipboard operation
graphql-for-vscode copied to clipboard

Handling duplicate "Query" and "Mutation" types

Open deklanw opened this issue 7 years ago • 6 comments

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?

deklanw avatar May 04 '18 14:05 deklanw

This would be addressed in the GQL plugin.

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

kumarharsh avatar May 04 '18 14:05 kumarharsh

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

deklanw avatar May 04 '18 15:05 deklanw

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!
}

Mayank1791989 avatar May 04 '18 18:05 Mayank1791989

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!

deklanw avatar May 05 '18 16:05 deklanw

@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.

nfantone avatar Jun 06 '18 13:06 nfantone

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?

Renaud009 avatar Apr 25 '19 19:04 Renaud009