Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 1 Error: Type "Query" was defined more than once. #27

Open
LeeMellon opened this issue Nov 4, 2020 · 7 comments
Open

Lesson 1 Error: Type "Query" was defined more than once. #27

LeeMellon opened this issue Nov 4, 2020 · 7 comments

Comments

@LeeMellon
Copy link

LeeMellon commented Nov 4, 2020

Following along with the first lesson and getting the following error.

Successfully compiled 16 files with Babel.
$ node dist/index.js
(node:87765) UnhandledPromiseRejectionWarning: Error: Type "Query" was defined more than once.
    at Object.buildASTSchema (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/graphql/utilities/buildASTSchema.js:84:15)
    at Object.buildSchemaFromTypeDefinitions (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/graphql-tools/dist/generate/buildSchemaFromTypeDefinitions.js:23:28)
    at Object.makeExecutableSchema (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/graphql-tools/dist/makeExecutableSchema.js:26:29)
    at new ApolloServerBase (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/apollo-server-core/dist/ApolloServer.js:150:43)
    at new ApolloServer (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/apollo-server-express/dist/ApolloServer.js:44:1)
    at new ApolloServer (/Users/iangoodrich/dev/experimental/intro-to-graphql/node_modules/apollo-server/dist/index.js:23:9)
    at start (/Users/iangoodrich/dev/experimental/intro-to-graphql/dist/server.js:47:18)
(node:87765) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:87765) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@LeeMellon
Copy link
Author

LeeMellon commented Nov 4, 2020

src/server.js

import { ApolloServer } from 'apollo-server'
import { loadTypeSchema } from './utils/schema'
import { authenticate } from './utils/auth'
import { merge } from 'lodash'
import config from './config'
import { connect } from './db'
import product from './types/product/product.resolvers'
import coupon from './types/coupon/coupon.resolvers'
import user from './types/user/user.resolvers'

const types = ['product', 'coupon', 'user']

export const start = async () => {
  const rootSchema = 
    type Cat {
    name: String
    age: Int
    }
    
    type Query {
      myCat: Cat
    }
    
    schema {
      query: Query
      mutation: Mutation
    }
  
  const schemaTypes = await Promise.all(types.map(loadTypeSchema))

  const server = new ApolloServer({
    typeDefs: [rootSchema, ...schemaTypes],
    resolvers: merge({
      Query: {
        myCat() {
          return {name: 'Zangief'}
        }
      }
    }, product, coupon, user),
    async context({ req }) {
      const user = await authenticate(req)
      return { user }
    }
  })

  await connect(config.dbUrl)
  const { url } = await server.listen({ port: config.port })

  console.log(`GQL server ready at ${url}`)
}

@laniehei
Copy link
Contributor

laniehei commented Nov 6, 2020

Hi @LeeMellon, does the solution code to this exercise help at all? https://github.com/FrontendMasters/intro-to-graphql/blob/lesson-1-solution/src/server.js

@bilalds
Copy link

bilalds commented Nov 11, 2020

Going through the same lesson and also seeing this issue. There are slight differences in the code the instructor has in the first lesson vs the repo when you clone it.

@bilalds
Copy link

bilalds commented Nov 11, 2020

(node:14474) UnhandledPromiseRejectionWarning: Error: There can be only one type named "Query".

@bilalds
Copy link

bilalds commented Nov 11, 2020

Okay, my issue was resolved with installing bcrypt and running the mongodb server.

@enigmatikme
Copy link

same issue

@leventefrks
Copy link

Yes, there are slight differences between the code in the video and the repo. I spent some time to figure it out but I finally managed to solve it by modifying the db.js file and putting an _ before the word Query in the server.js

db.js:

export const connect = (url = options.dbUrl, opts = {}) => {
return mongoose
.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log('Database Connected'))
.catch(err => console.log(err))
}

server.js:

export const start = async () => {
const rootSchema = `
type Cat {
name: String
}

type _Query {
  myCat: Cat
}

schema {
  query: _Query
  mutation: Mutation
}

`
const schemaTypes = await Promise.all(types.map(loadTypeSchema))

const server = new ApolloServer({
typeDefs: [rootSchema, ...schemaTypes],
resolvers: merge(
{
_Query: {
myCat() {
return { name: 'Garfield' }
}
}
},
product,
coupon,
user
),
async context({ req }) {
const user = await authenticate(req)
return { user }
}
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants