无法连接到我的本地 GraphQL 服务器,使用 Apollo Studio 沙盒。

huangapple go评论56阅读模式
英文:

Not able to connect to my local graphql server with apollo studio sandbox

问题

我正在使用 serverless offline 运行 GraphQL 服务器,并尝试使用 Aollo Studio 连接到该服务器。最奇怪的问题是,一周前它可以正常连接,但现在完全相同的服务器无法连接。我将相同的内容部署到了 AWS,Aollo Studio 可以连接到部署的服务器。您认为可能是什么原因?

环境

我使用的是 Macbook m1 pro。

Node 版本是 15.6

如您在配置文件中可以看到,我在路径 http://localhost:3000/dev/playground 上启动了一个游乐场,我可以访问它,但这个游乐场也无法连接服务器。

我观察到的一件事是,当我尝试访问本地网络 IP 地址,比如 http://192.168.1.3:3000/dev/playground 时也无法工作,所以可能存在某种网络问题。但当我运行类似于 React 应用时,我可以在 http://192.168.1.3:3000 上访问它。

我的 serverless.yml 文件如下:

service: serverless-graphql-rds

frameworkVersion: "3.8.0"

provider:
  name: aws
  runtime: nodejs14.x
  stage: ${env:PROVIDER_STAGE}
  region: ${env:REGION}
  environment:
    JWT_SECRET: ${env:JWT_SECRET}
    DATABASE_URL: ${env:DATABASE_URL}
    REDIRECT_TO_DASHBOARD: ${env:REDIRECT_TO_DASHBOARD}
    HUB_SPOT_CLIENT_ID: ${env:HUB_SPOT_CLIENT_ID}
    HUB_SPOT_CLIENT_SECRET: ${env:HUB_SPOT_CLIENT_SECRET}
    HUB_SPOT_REDIRECT_URI: ${env:HUB_SPOT_REDIRECT_URI}

plugins:
  - serverless-plugin-typescript
  - serverless-offline
package:
  patterns:
    - "migrations/**"
    - "**.js"
    - "config"

custom:
  serverless-offline:
    httpPort: ${env:httpPort, 3000}
    lambdaPort: ${env:lambdaPort, 3002}
  serverless-plugin-typescript:
    tsConfigFileLocation: "./tsconfig.json"

functions:
  graphql:
    handler: server.handler
    events:
      - http:
          path: graphql
          method: post
          cors: true
  playground:
    handler: server.playgroundHandler
    events:
      - http:
          path: playground
          method: get
          cors: true
  oauth-callback:
    handler: ./rest-apis-handlers/oauth-callback.handler
    events:
      - http:
          path: oauth-callback
          method: get
          cors: true

文件 server.ts 如下,其中包含 handler 函数:

import { ApolloError, ApolloServer } from "apollo-server-lambda";
import lambdaPlayground from "graphql-playground-middleware-lambda";
import { verifyToken } from "./common/jwt";
import { useContext } from "./core/context";
import resolvers from "./graphql/resolvers";
import typeDefs from "./graphql/schema";

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ event, context, express }) => {
    const auth = express.req.headers["authorization"] as string;
    if (auth) {
      const [_, token] = auth.split("Bearer ");
      try {
        const user = verifyToken(token);
        if (user) {
          return useContext({
            type: "user",
            properties: {
              ...user,
            },
          });
        } else {
          throw new ApolloError("Not authenticated", "UNAUTHENTICATED");
        }
      } catch (ex) {}
    }
    return useContext({
      type: "public",
    });
  },
});

export const handler = server.createHandler({});

// 对于本地端点URL是 /graphql,对于生产环境是 /stage/graphql
export const playgroundHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  return lambdaPlayground({
    endpoint: process.env.REACT_APP_GRAPHQL_ENDPOINT,
  })(event, context, callback);
};
英文:

I am running a graphql server using serverless offline and trying to connect to the server with Aollo Studio. The weirdest bug is that it was connecting properly a week back and now the exact same server is not connecting. The same thing I have deployed on the AWS and Aollo Studio is able to connect to the deployed server. Any idea what could be the reason for it?

Environment

I am on Macbook m1 pro.

Node Version 15.6

As you can in the config file I have started a playground as well on this path http://localhost:3000/dev/playground which I am able to access but this playground is also not connecting the server.

One thing which I have observed is my local network IP URL like http://192.168.1.3:3000/dev/playground is also not working when I am trying to visit so maybe some kind of network issue might be there.
But when I run something like a React App I am able to access it on this http://192.168.1.3:3000

My serverless.yml looks like below

service: serverless-graphql-rds

frameworkVersion: "3.8.0"

provider:
  name: aws
  runtime: nodejs14.x
  stage: ${env:PROVIDER_STAGE}
  region: ${env:REGION}
  environment:
    JWT_SECRET: ${env:JWT_SECRET}
    DATABASE_URL: ${env:DATABASE_URL}
    REDIRECT_TO_DASHBOARD: ${env:REDIRECT_TO_DASHBOARD}
    HUB_SPOT_CLIENT_ID: ${env:HUB_SPOT_CLIENT_ID}
    HUB_SPOT_CLIENT_SECRET: ${env:HUB_SPOT_CLIENT_SECRET}
    HUB_SPOT_REDIRECT_URI: ${env:HUB_SPOT_REDIRECT_URI}

plugins:
  - serverless-plugin-typescript
  - serverless-offline
package:
  patterns:
    - "migrations/**"
    - "**.js"
    - "config"

custom:
  serverless-offline:
    httpPort: ${env:httpPort, 3000}
    lambdaPort: ${env:lambdaPort, 3002}
  serverless-plugin-typescript:
    tsConfigFileLocation: "./tsconfig.json"

functions:
  graphql:
    handler: server.handler
    events:
      - http:
          path: graphql
          method: post
          cors: true
  playground:
    handler: server.playgroundHandler
    events:
      - http:
          path: playground
          method: get
          cors: true
  oauth-callback:
    handler: ./rest-apis-handlers/oauth-callback.handler
    events:
      - http:
          path: oauth-callback
          method: get
          cors: true

And the file server.ts looks like below which contains the handler function

import { ApolloError, ApolloServer } from "apollo-server-lambda";
import lambdaPlayground from "graphql-playground-middleware-lambda";
import { verifyToken } from "./common/jwt";
import { useContext } from "./core/context";
import resolvers from "./graphql/resolvers";
import typeDefs from "./graphql/schema";

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ event, context, express }) => {
    const auth = express.req.headers["authorization"] as string;
    if (auth) {
      const [_, token] = auth.split("Bearer ");
      try {
        const user = verifyToken(token);
        if (user) {
          return useContext({
            type: "user",
            properties: {
              ...user,
            },
          });
        } else {
          throw new ApolloError("Not authenticated", "UNAUTHENTICATED");
        }
      } catch (ex) {}
    }
    return useContext({
      type: "public",
    });
  },
});
export const handler = server.createHandler({});

// for local endpointURL is /graphql and for prod it is /stage/graphql
export const playgroundHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  return lambdaPlayground({
    endpoint: process.env.REACT_APP_GRAPHQL_ENDPOINT,
  })(event, context, callback);
};

答案1

得分: 1

经过3天的努力,今天我终于弄清楚了。
由于我正在使用serverless-offline包来运行本地graphql服务器,由于它所需的node版本必须小于15。这里有详细的讨论。

所以只需将node版本降级到小于15,它就会正常工作。

英文:

After struggling for 3 days today I finally figured it out.
As I am using the serverless-offline package to run the local graphql server and due to the node version it requires less than 15. Here is the details discussion on it.

So just downgrade the node version to anything < 15 and it will work.

huangapple
  • 本文由 发表于 2023年2月10日 14:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定