Javascript 启动阿波罗服务器

Javascript 启动阿波罗服务器,javascript,graphql,apollo,apollo-server,Javascript,Graphql,Apollo,Apollo Server,嘿,我开始通过做()来学习graphQL。 我会从上下文开始,但我到处看,什么都没有,我也试图改变它,但它总是相同的输出。如果有人能帮忙,我将非常非常感激 我正在尝试用下一个代码启动服务器: import { ApolloServer } from 'apollo-server' import { loadTypeSchema } from './utils/schema' import { authenticate } from './utils/auth'

嘿,我开始通过做()来学习graphQL。 我会从上下文开始,但我到处看,什么都没有,我也试图改变它,但它总是相同的输出。如果有人能帮忙,我将非常非常感激

我正在尝试用下一个代码启动服务器:


    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
        }
        
        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: {
          _Query: {
            myCat(){
              console.log('Hello there')
              return {name:'Ivar'}
            }
          },
          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}`)
    }

数据库文件:


    import mongoose from 'mongoose'
    import options from './config'
    
    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))

}
奇怪的是,在某个时刻,它工作得很好,我做了一些改变,但并不重要

控制台输出如下:


    ...
    $ node dist/index.js
    (node:17124) UnhandledPromiseRejectionWarning: Error: "context" defined in resolvers, but not in schema (Use `node --trace-warnings ...` to show where the warning was created)
    (node:17124) 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:17124) [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.
    [nodemon] clean exit - waiting for changes before restart

我确信这是一个愚蠢的错误,但它让我很害怕。

是传递给阿波罗服务器的配置对象的一部分,而不是它的
.resolvers
对象的一部分

const server = new ApolloServer({
  typeDefs: [rootSchema, ...schemaTypes],
  resolvers: {
    _Query: {
      myCat(){
        console.log('Hello there')
        return {name:'Ivar'}
      }
    },
  },
  async context({ req }) {
    const user = await authenticate(req)
    return { user }
  },
})

这是正确的。为了进一步说明这一点,我建议您在学习时,为
typeDefs
解析器
等使用单独的变量,并在调用新的ApolloServer时将它们作为变量传递,而不是一次构建对象。更容易了解要去哪里以及为什么。很容易意外地将
上下文上移一行,但很难意外地将其嵌套到单独的解析器变量中。