问题描述
大家好,我正在尝试设置一个 graphql 无服务器项目,但是我在运行 graphql 服务器或至少运行 graphql 游乐场时遇到了问题。
当我通过 sls offline start
运行服务器时,操场午餐它无法以某种方式到达我的架构。
我的 graphql 服务器 graphql.ts
看起来像这样:
import { ApolloServer,gql,IResolvers,makeExecutableSchema } from 'apollo-server-lambda';
// // Construct a schema,using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
// // Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello World!',},};
const schema = makeExecutableSchema({
typeDefs,resolvers: resolvers as IResolvers,});
const server = new ApolloServer({ schema });
export const graphqlHandler = server.createHandler({
cors: {
methods: ["GET","POST"],origin: 'http://localhost:3000',});
我的 serverless.yml
文件是这样的:
service: test
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: 20201221
plugins:
- serverless-webpack
- serverless-offline
custom:
webpack:
webpackConfig: 'webpack.config.ts'
includeModules: true
functions:
graphql:
handler: src/graphql.graphqlHandler
events:
- http:
path: graphql
method: get
cors: true
- http:
path: graphql
method: post
cors: true
但我想 serverless.yml
应该不是问题,因为它可以正确运行和编译。而且,正如您所看到的,网址与 Playground 中的网址相同:
Serverless: Bundling with Webpack...
asset src/graphql.js 4.82 KiB [emitted] (name: src/graphql)
./src/graphql.ts 670 bytes [built] [code generated]
external "apollo-server-lambda" 42 bytes [built] [code generated]
webpack compiled successfully in 217 ms
Serverless: Watching for changes...
offline: Starting Offline: dev/us-east-1.
offline: Offline [http for lambda] listening on http://localhost:3002
offline: Function names exposed for local invocation by aws-sdk:
┌───────────────────────────────────────────────────────────────────────────┐
│ │
│ GET | http://localhost:3000/dev/graphql │
│ POST | http://localhost:3000/2015-03-31/functions/graphql/invocations │
│ POST | http://localhost:3000/dev/graphql │
│ POST | http://localhost:3000/2015-03-31/functions/graphql/invocations │
│ │
└───────────────────────────────────────────────────────────────────────────┘
如果有帮助,我的 webpack 配置看起来像这样:
module.exports = {
context: __dirname,mode: slsw.lib.webpack.isLocal ? 'development' : 'production',entry: slsw.lib.entries,devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',resolve: {
extensions: ['.mjs','.json','.ts'],symlinks: false,cacheWithContext: false,// plugins: [
// new TsconfigPathsPlugin({
// configFile: './tsconfig.paths.json',// }),// ],output: {
libraryTarget: 'commonjs',path: path.join(__dirname,'.webpack'),filename: '[name].js',optimization: {
concatenateModules: false,target: 'node',externals: [nodeExternals()],module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,loader: 'ts-loader',exclude: [
[
path.resolve(__dirname,'node_modules'),path.resolve(__dirname,'.serverless'),],options: {
transpileOnly: true,experimentalWatchApi: true,{
test: /\.graphql?$/,use: [
{
loader: 'webpack-graphql-loader',options: {
validate: true,schema: "./src/schema.graphql"
}
}
]
}
],plugins: [],};
有人能看到我遗漏了什么吗?谢谢!
UPDATE 似乎在本地运行时不起作用。当我部署 lambda 时,它工作得很好。我不知道它是否与 serverless offline
或我的配置有关。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)