【问题标题】:Apollo Server - GET query missingApollo 服务器 - 缺少 GET 查询
【发布时间】:2022-01-27 04:42:21
【问题描述】:

我正在尝试使用这个 template 来学习如何使用 graphql/apollo/prisma 等。

当我尝试启动服务器时,我在控制台中得到确认,但浏览器中出现错误提示:缺少 GET 查询。

import "reflect-metadata"
import "dotenv/config"

import { ApolloServerPluginCacheControl, ApolloServerPluginLandingPageDisabled } from "apollo-server-core"
import { ApolloServer } from "apollo-server-express"
import jwt from "express-jwt"
import { buildSchema } from "type-graphql"
import { Container } from "typedi"

import { JWT_AUTH } from "./lib/config"
import { ExpressContext } from "./lib/express"
import { formatResponse } from "./lib/formatResponse"
import { ErrorInterceptor } from "./lib/globalMiddleware"
import { loadPrismaHooks } from "./lib/hooks"
import { loadCurrentUser } from "./lib/loadCurrentUser"
import { loadResolvers } from "./lib/loadResolvers"
import { prisma } from "./lib/prisma"
import { Server } from "./lib/server"

class App extends Server {
  constructor() {
    super()
    this.init().catch((error) => {
      this.logger.error(error)
      process.exit(1)
    })
  }

  async init() {
    await this.setUpDb()
    await this.setUpAuth()
    await this.setupApollo()
    this.start()
  }
  async setUpDb() {
    await prisma.$connect()
    loadPrismaHooks()
    this.logger.info("DB ready")
  }
  async setUpAuth() {
    this.app
      .use(jwt(JWT_AUTH))
      .use((err: any, _: any, __: any, next: any) => {
        if (err.name === "UnauthorizedError") next()
      })
      .use(loadCurrentUser)
    this.logger.info("Auth ready")
  }

  async setupApollo() {
    const schema = await buildSchema({
      container: Container,
      resolvers: loadResolvers(),
      globalMiddlewares: [ErrorInterceptor],
    })
    const apolloServer = new ApolloServer({
      context: ({ req, res }: ExpressContext) => ({ req, res, prisma }),
      formatResponse,
      plugins: [ApolloServerPluginCacheControl(), ApolloServerPluginLandingPageDisabled()],
      schema,
// playground: true,
// introspection: true
      
    })
    await apolloServer.start()
    apolloServer.applyMiddleware({ app: this.app })
    // I deleted  cors: true from the above line
    this.logger.info("Apollo setup hello")
  }
}

new App()

我看到其他帖子描述不再需要 cors(不确定其基础是什么),并且还建议我将 Playground 和 introspection 参数添加到新的 ApolloServer。我试过这个(如注释行所示),但游乐场未被识别为有效参数。

服务器在 lib 文件夹中定义为:

import "reflect-metadata"
import "dotenv/config"

import * as Sentry from "@sentry/node"
import * as Tracing from "@sentry/tracing"
import chalk from "chalk"
import express from "express"
import morgan from "morgan"

import { IS_PRODUCTION, PORT, SENTRY_DSN } from "./config"

export class Server {
  private readonly _app: express.Application

  readonly logger: {
    info: (message: string) => void
    error: (message: string) => void
  }

  constructor() {
    this._app = express()
      .use(Sentry.Handlers.requestHandler())
      .use(Sentry.Handlers.tracingHandler())
      .enable("trust proxy")
      .use(
        morgan("dev", {
          skip: (req) => req.method === "OPTIONS",
          stream: { write: (message) => console.log(message + "\n\n") },
        }),
      )

    if (IS_PRODUCTION) {
      Sentry.init({
        dsn: SENTRY_DSN,
        integrations: [
          new Sentry.Integrations.Http({ tracing: true }),
          new Tracing.Integrations.Express({ app: this._app }),
        ],
        enabled: IS_PRODUCTION,
        tracesSampleRate: 1.0,
      })
    }
    this.logger = {
      info: this.info,
      error: this.error,
    }
  }

  protected error(message: string) {
    console.log(`[${chalk.red("ERROR")}] `, message)
  }
  protected info(message: string) {
    console.log(`[${chalk.blue("INFO")}] `, message)
  }

  protected get app(): express.Application {
    return this._app
  }

  start(): void {
    this._app
      .use(Sentry.Handlers.errorHandler())
      .listen(PORT, () => this.logger.info(`Server started at http://localhost:${PORT}/graphql ????` + "\n"))
  }
}

控制台登录终端打印服务器启动确认,但浏览器只是生成无法获取消息。我不知道这条消息意味着什么,我可以开始尝试弄清楚如何去操场。

谁能推荐有关如何配置服务器的当前说明?

【问题讨论】:

    标签: reactjs express graphql apollo prisma


    【解决方案1】:

    这个问题的原因似乎与迁移到 graphql 沙箱环境有关。如果将 localhost555 添加到沙盒地址栏,则页面加载时不会出错。

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2019-09-26
      • 2020-05-05
      • 2019-04-18
      • 2017-12-09
      • 2016-08-31
      • 2019-02-22
      • 2023-04-02
      • 2017-08-14
      相关资源
      最近更新 更多