尝试使用 apollo-server 的 playground 会导致 JSON 输入意外结束
Attempting to use apollo-server's playground results in Unexpected end of JSON input
我在 apolly-server-lambda 应用程序中启动 playground 时遇到此错误:
Playground.tsx:289 SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at index.js:40
该应用只有一个测试解析器。我使用 type-graphql 来定义模式:
@Resolver()
export class InviteResolver {
@Query(() => String)
hello() {
return 'test';
}
}
然后我用生成的模式配置了 apollo 服务器:
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));
};
最后,我用无服务器完成了所有设置:
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 start
而且好像启动成功了:
┌───────────────────────────────────────────────────────────────────────────┐
│ │
│ 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,但我立即开始在控制台中一遍又一遍地看到上面的错误。我看不到架构或文档选项卡(基本上所有轮询都失败),并且没有查询到达该解析器。
你们看到我的接线方式有什么问题吗?
问题是我导出的是异步函数而不是常规函数。
对我有用的是:
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))
}
我在 apolly-server-lambda 应用程序中启动 playground 时遇到此错误:
Playground.tsx:289 SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at index.js:40
该应用只有一个测试解析器。我使用 type-graphql 来定义模式:
@Resolver()
export class InviteResolver {
@Query(() => String)
hello() {
return 'test';
}
}
然后我用生成的模式配置了 apollo 服务器:
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));
};
最后,我用无服务器完成了所有设置:
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 start
而且好像启动成功了:
┌───────────────────────────────────────────────────────────────────────────┐
│ │
│ 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,但我立即开始在控制台中一遍又一遍地看到上面的错误。我看不到架构或文档选项卡(基本上所有轮询都失败),并且没有查询到达该解析器。
你们看到我的接线方式有什么问题吗?
问题是我导出的是异步函数而不是常规函数。
对我有用的是:
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))
}