GraphQL无法读取未定义的属性“查询”

时间:2019-06-23 20:21:27

标签: javascript graphql apollo postgraphile

当我知道定义正确时,我无法弄清楚为什么我会得到未定义的查询。 graphiQL正在顺利解决我的问题:

enter image description here

自动完成效果很好

Before hitting alt+enter

enter image description here

点击ctrl+enter后,所有字段均输入,请参见上文。

然后我执行查询,我将得到:

{
  "errors": [
    {
      "message": "Cannot read property 'query' of undefined",
      "locations": [
        {
          "line": 1,
          "column": 3
        }
      ],
      "path": [
        "allAwards"
      ]
    }
  ],
  "data": {
    "allAwards": null
  }
}

npm运行graphql

"graphql": "nodemon -r dotenv/config --experimental-modules --inspect=[9222] graphql_server.js",

graphql_server.js

import express from 'express'
import pg from 'pg'
import graphqlHTTP from 'express-graphql'
import PAS from 'postgraphile-apollo-server'
import AP from 'apollo-server-express'

const { makeSchemaAndPlugin } = PAS
const { ApolloServer } = AP

const env = process.env.NODE_ENV || 'development'
const dbHost = process.env.DB_HOST
const dbPort = process.env.DB_PORT
const dbName = process.env.DB_NAME
const dbUser = process.env.DB_USER
const dbPwd = process.env.DB_PWD
const dbUrl = dbPwd
  ? `postgres://${dbUser}:${dbPwd}@${dbHost}:${dbPort}/${dbName}`
  : `postgres://${dbHost}:${dbPort}/${dbName}`

const pgPool = new pg.Pool({
  connectionString: dbUrl,
})

async function main() {
  const { schema, plugin } = await makeSchemaAndPlugin(
    pgPool,
    'public', // PostgreSQL schema to use
    {
      // PostGraphile options, see:
      // https://www.graphile.org/postgraphile/usage-library/
    }
  )

  const server = new ApolloServer({
    schema,
    plugins: [plugin],
  })
  const app = express()

  app.use(
    '/graphql',
    graphqlHTTP({
      schema: schema,
      graphiql: true,
    })
  )

  server.applyMiddleware({ app })

  app.listen({ port: 4000 }, () => console.log(`? Server ready at http://localhost:4000${server.graphqlPath}`))
}

main().catch(e => {
  console.error(e)
  process.exit(1)
})

psql数据库中目前也有2行用于奖励

enter image description here

1 个答案:

答案 0 :(得分:1)

您不应在快速应用程序中同时使用express-graphqlapollo-server的中间件。由于postgraphile-apollo-server与ApolloServer明确兼容,因此将express-graphql完全删除。拥有这两种中间件可能会导致意外问题,因为它们在相同的路径上进行监听。

Apollo放弃了GraphiQL,转而使用GraphQL Playground。如果要将GraphiQL与Apollo一起使用,则可以使用express-graphiql-middleware之类的软件包。