graphql-for-vscode
graphql-for-vscode copied to clipboard
Handling duplicate "Query" and "Mutation" types
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?
This would be addressed in the GQL plugin.
@Mayank1791989 this usecase is what we were discussing about today, no?
I thought that might be the case. Should I open this same issue over there?
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
- 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!
}
- 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!
}
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!
@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.
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?