Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 220 Vote(s) - 3.36 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Apollo Server - GraphQL Error: There can be only one type named "Query"

#1
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](

[To see links please register here]

) for testing purposes.
* And [this one](

[To see links please register here]

) 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

[To see links please register here]

}`)
});

What am I doing wrong?
Reply

#2
When following the pattern of deep modularization, where you want to have each type definition in its own file, and each set of resolvers in their own file, you want to use the `extend` keyword and create "empty" definitions.


Supposing that you have `root` and `user` type definitions in separate files, your index file that puts them together should look like this:


```
const user = require('./user');
const root= require('./root');
const typeDefs = gql`
type Query{
_empty: String
}
type Mutation {
_empty: String
}
${user}
${root}
`;

module.exports = typeDefs;
```
You're using
```
type Query{
_empty: String
}
```
to make an empty `Query`. Then you're adding your user and root at the end.

Within your user file, you'd want this:

```
extend type Query {
getUsers: [User]
}
```

So the `extend` keyword is you extending the empty query you created in your index file.

You can read more on modularization here

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through