0

大家好,我正在尝试设置 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不应该是问题,因为它可以正确运行和编译。而且,您可以看到网址与操场上的相同:

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'),
            path.resolve(__dirname, '.webpack'),
          ],
        ],
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
        },
      },
      {
        test: /\.graphql?$/,
        use: [
          {
            loader: 'webpack-graphql-loader',
            options: {
              validate: true,
              schema: "./src/schema.graphql"
            }
          }
        ]
      }
    ],
  },
  plugins: [],
};

有人能看到我错过了什么吗?谢谢你!

更新它似乎在本地运行时不起作用。当我部署 lambda 时,它工作得非常好。我不知道它是否与serverless offline我的配置有关。

4

0 回答 0