I am new to GraphQL. I am following several guides on Internet in order to "create" a small app that uses Apollo Server + Express + GraphQL + MongoDB.
- I have tried to replicate this YT guide (he creates root.js file on typeDefs folder).
- This one for testing purposes.
- And this one to make sure my folder structure is correct.
I am getting from GraphQL when compiling:
Error: There can be only one type named "User".
Error: There can be only one type named "Query".
I have structured my code like this:
- config
- models
- resolvers
- index.js
- user.js
- typeDefs
- index.js
- root.js
- user.js
- index.js
Until now, my code looks like this:
typeDefs/user.js:
import { gql } from 'apollo-server-express';
const user = gql`
type User {
id: ID!
name: String
email: String
password: String
}
type Query {
getUsers: [User]
}
type Mutation {
addUser(name: String!, email: String!, password: String!): User
}
`;
export default user;
typeDefs/root.js:
import { gql } from 'apollo-server-express';
export default gql`
extend type Query {
_: String
}
type User {
_: String
}
`;
typeDefs/index.js:
import root from './root';
import user from './user';
export default [
root,
user
];
And then in my index.js:
import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
import typeDefs from './typeDefs';
import resolvers from './resolvers';
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.disable('x-powered-by');
app.listen({ port: 4000 }, () => {
console.log(`Server running at http://localhost:4000${server.graphqlPath}`)
});
What am I doing wrong?
typeDefs/root.js:
and the other withintypeDefs/user.js:
. Just remove the root one, should be enough.typeDefs
are fine as long you've got theextend
keyword on both types insideroot.js
. I ran the code locally and it runs fine. Are you still seeing an error aboutQuery
being defined more than once? If so, what version ofapollo-server-express
are you running?extend type Query
&extend type User
on typeDefs/user.js and now seems to work. Make your answer.