首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用apollo-server的游乐场会导致JSON输入意外结束

尝试使用apollo-server的游乐场会导致JSON输入意外结束
EN

Stack Overflow用户
提问于 2020-12-28 12:31:30
回答 1查看 225关注 0票数 0

当我在apolly-server-lambda应用程序中启动游乐场时,我得到了这个错误:

代码语言:javascript
复制
Playground.tsx:289 SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at index.js:40

这个应用程序只有一个测试解析器。我使用type-graphql来定义模式:

代码语言:javascript
复制
@Resolver()
export class InviteResolver {

    @Query(() => String)
    hello() {
        return 'test';
    }

}

然后我用生成的模式配置了apollo服务器:

代码语言:javascript
复制
async function createHandler() {
    const schema = await buildSchema({
        resolvers: [InviteResolver]
    });

    const server = new ApolloServer({
        schema,
        playground: {
            endpoint: "/dev/graphql"
        }
    })

    return server.createHandler({
        cors: {
            origin: '*',
            methods: '*',
            allowedHeaders: '*',
            credentials: true,
        },
    });
}

export const handler = async function(event: APIGatewayProxyEvent, context: LambdaContext, callback: APIGatewayProxyCallback) {
    context.callbackWaitsForEmptyEventLoop = false;
    return await createHandler()
        .then(handler => handler(event, context, callback));
};

最后,我用serverless完成了所有的设置:

代码语言:javascript
复制
service: app
provider:
  name: aws
  runtime: nodejs12.x
  region: us-east-1
  apiGateway:
    shouldStartNameWithService: true
functions:
  graphql:
    handler: src/app.handler
    events:
      - http:
          path: /graphql
          method: post
          cors: true
      - http:
          path: /graphql
          method: get
          cors: true
plugins:
  - serverless-dotenv-plugin
  - serverless-plugin-typescript
  - serverless-offline

正如您在配置中所看到的,我使用serverless-offline进行开发,并且我从以下内容开始:

serverless offline start

而且它似乎开始得很成功:

代码语言:javascript
复制
   ┌───────────────────────────────────────────────────────────────────────────┐
   │                                                                           │
   │   POST | http://localhost:3000/dev/graphql                                │
   │   POST | http://localhost:3000/2015-03-31/functions/graphql/invocations   │
   │   GET  | http://localhost:3000/dev/graphql                                │
   │   POST | http://localhost:3000/2015-03-31/functions/graphql/invocations   │
   │                                                                           │
   └───────────────────────────────────────────────────────────────────────────┘

我可以在http://localhost:3000/dev/graphql上访问操场UI,但很快我就开始在控制台中一遍又一遍地看到上面的错误。我看不到schema或doc选项卡(基本上所有轮询都失败了),也没有查询到达该解析器。

你们觉得我的布线方式有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-29 00:29:27

问题是我导出的是异步函数,而不是常规函数。

对我起作用的是:

代码语言:javascript
复制
const globalSchema = buildSchema({
    resolvers: [InviteResolver]
});

async function getServer() {
    const schema = await globalSchema;
    return new ApolloServer({
        schema,
        playground: {
            endpoint: "/dev/graphql"
        }
    });
}

export function handler(event: any, ctx: any, callback: any) {
    getServer()
        .then(server => server.createHandler({
            cors: {
                origin: '*',
                methods: '*',
                allowedHeaders: '*',
                credentials: true,
            },
        }))
        .then(handler => handler(event, ctx, callback))
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65473030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档