Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo Server - GraphQL Error: There can be only one type named "Query"

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?

like image 484
Maramal Avatar asked Apr 09 '19 20:04

Maramal


People also ask

What is type query in GraphQL?

The Query type is a special object type that defines all of the top-level entry points for queries that clients execute against your server. Each field of the Query type defines the name and return type of a different entry point. The Query type for our example schema might resemble the following: GraphQL. 1.

When using GraphQL which defines a set of types?

GraphQL comes with a set of default scalar types out of the box: Int : A signed 32‐bit integer. Float : A signed double-precision floating-point value. String : A UTF‐8 character sequence.

What is Gql in Apollo?

gql. The gql template literal tag can be used to concisely write a GraphQL query that is parsed into a standard GraphQL AST. It is the recommended method for passing queries to Apollo Client. While it is primarily built for Apollo Client, it generates a generic GraphQL AST which can be used by any GraphQL client.

What other types can be used in a GraphQL schema?

The GraphQL schema language supports the scalar types of String , Int , Float , Boolean , and ID , so you can use these directly in the schema you pass to buildSchema . By default, every type is nullable - it's legitimate to return null as any of the scalar types.


1 Answers

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 https://blog.apollographql.com/modularizing-your-graphql-schema-code-d7f71d5ed5f2

like image 177
paceaux Avatar answered Oct 18 '22 10:10

paceaux