我正在尝试使用无服务器 lambda 设置测试联合 GraphQL 实例。现在,我将产品子图部署在一个 lambda 上,网关部署在另一个 lambda 上。对产品 lambda 的查询工作正常;但是,对网关进行相同的查询会返回以下错误。如果我部署一个本地网关,我可以对产品子图进行查询——它仅不能通过部署在 Lambda 中的网关工作。虽然我在网上看到有人说他们已经在 AWS lambda 中成功实现了 GraphQL 联合网关,但我一直无法找到有效的代码库或遇到同样问题的任何人。任何见解将不胜感激,谢谢!
错误状态:400
{
"errors": [
{
"message": "Cannot read property 'QUERY' of undefined",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: Cannot read property 'QUERY' of undefined",
" at QueryPlanningContext.getVariableUsages (/var/task/node_modules/@apollo/query-planner/dist/QueryPlanningContext.js:46:115)",
" at executionNodeForGroup (/var/task/node_modules/@apollo/query-planner/dist/buildQueryPlan.js:162:36)",
" at /var/task/node_modules/@apollo/query-planner/dist/buildQueryPlan.js:58:39",
" at Array.map (<anonymous>)",
" at buildQueryPlan (/var/task/node_modules/@apollo/query-planner/dist/buildQueryPlan.js:58:26)",
" at QueryPlanner.buildQueryPlan (/var/task/node_modules/@apollo/query-planner/dist/index.js:28:52)",
" at /var/task/node_modules/@apollo/gateway/dist/index.js:95:58",
" at NoopContextManager.with (/var/task/node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:36:24)",
" at ContextAPI.with (/var/task/node_modules/@opentelemetry/api/build/src/api/context.js:71:54)",
" at NoopTracer.startActiveSpan (/var/task/node_modules/@opentelemetry/api/build/src/trace/NoopTracer.js:67:28)"
]
}
}
}
]
}
下面的代码是子图 lambda
产品.js
const { ApolloServer, gql } = require ('apollo-server-lambda');
const { buildSubgraphSchema } = require('@apollo/subgraph');
const products = [
{
upc: 'a1',
name: 'latte',
price: 3
},
{
upc: 'a2',
name: 'macchiato',
price: 4
},
{
upc: 'a3',
name: 'mocha',
price: 5
}
];
const typeDefs = gql`
type Product @key(fields: "upc"){
upc: String!
name: String!
price: Int
}
type Query {
product(upc: String!): Product
}
`;
const resolvers = {
Query: {
product(parent, args, context, info){
return products.find(product => product.upc === args.upc)
}
},
Product: {
__resolveReference(reference){
return products.find(product => product.upc === reference.upc)
}
}
};
const server = new ApolloServer({
schema: buildSubgraphSchema({typeDefs, resolvers})
});
exports.productsHandler = server.createHandler();
无服务器.yml
service: graphqlproducts
provider:
name: aws
runtime: nodejs14.x
functions:
products:
handler: products.productsHandler
events:
- http:
path: /
method: post
cors: true
- http:
path: /
method: get
cors: true
包.json
{
"name": "graphql-poc",
"description": "GraphQL POC",
"version": "0.0.1",
"private": true,
"dependencies": {
"@apollo/federation": "^0.35.1",
"@apollo/subgraph": "^0.2.0",
"apollo-server": "^3.6.3",
"apollo-server-lambda": "^3.6.2",
"graphql": "^16.3.0"
},
"devDependencies": {},
"scripts": {}
}
下面是网关 lambda 的代码
index.js
const { ApolloServer } = require('apollo-server-lambda');
const { ApolloGateway, IntrospectAndCompose } = require('@apollo/gateway');
const gateway = new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: 'products', url: 'redacted' }
],
}),
});
// Pass the ApolloGateway to the ApolloServer constructor
const server = new ApolloServer({
gateway
});
exports.gatewayHandler = server.createHandler();
无服务器.yml
service: graphqlgateway
provider:
name: aws
runtime: nodejs14.x
functions:
gateway:
handler: index.gatewayHandler
timeout: 20
events:
- http:
path: /
method: post
cors: true
- http:
path: /
method: get
cors: true
包.json
{
"name": "graphqlpocgateway",
"version": "0.0.1",
"description": "Gateway for graphqlpoc",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@apollo/gateway": "^0.47.0",
"apollo-server": "^3.6.2",
"apollo-server-lambda": "^3.6.3",
"graphql": "^15.8.0"
},
"devDependencies": {
"@apollo/rover": "^0.4.1"
}
}