apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Update lambda.md

Open sergiycheck opened this issue 3 years ago • 3 comments
trafficstars

Currently we are getting an error Can not post null, when we make a POST request with the body

query Query { hello }

We need to change the default path from / to graphql in the serverless.yml in order to get response from the resolver.

sergiycheck avatar Sep 02 '22 08:09 sergiycheck

@sergiycheck: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

apollo-cla avatar Sep 02 '22 08:09 apollo-cla

Deploy Preview for apollo-server-docs ready!

Built without sensitive environment variables

Name Link
Latest commit 1e737322c122cf47d03473d5454e9861065250e0
Latest deploy log https://app.netlify.com/sites/apollo-server-docs/deploys/6311baec04295900088955ed
Deploy Preview https://deploy-preview-6876--apollo-server-docs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Sep 02 '22 08:09 netlify[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1e737322c122cf47d03473d5454e9861065250e0:

Sandbox Source
Apollo Server Typescript Configuration
Apollo Server Configuration

codesandbox-ci[bot] avatar Sep 02 '22 08:09 codesandbox-ci[bot]

Hi @sergiycheck . We're about to release Apollo Server 4, which moves the responsibility for maintaining the Lambda integration away from the Apollo Server core team and into the hands of Lambda experts (with a new integration package), so this doc page is going to be removed soon. Apollo Server 4 also removes path parsing and the default check for /graphql which should address the same issue in a different way.

glasser avatar Oct 03 '22 21:10 glasser

Hi @glasser, I am proposing to change serverless.yml configuration file to make things work for now. This file is not related with apollo server source code, it's related with lambda handler and serverless framework.

sergiycheck avatar Oct 04 '22 07:10 sergiycheck

I understand, but the file is in a docs page that's going to be deleted in a few days, and if it eventually gets re-created the path thing won't be relevant because Apollo Server will have stopped unnecessarily defaulting to scanning the URL path for /graphql.

I suppose we do keep the ASv3 docs around so I guess I will take a moment to see what's going on here. The line you want to change was updated in https://github.com/apollographql/apollo-server/pull/5497 which was released in apollo-server v3.0.1. That release last year removed the "look for /graphql" behavior from serverless integrations (which never worked very well, and as I mentioned has now been removed in AS4 from all integrations).

Is it possible you're using Apollo Server 2 for some reason?

glasser avatar Oct 04 '22 18:10 glasser

I am using Apollo Server 3 for learning purposes. It would not be great for me if the serverless.yml was deleted from docs, because I would have to investigate serverless docs and figure out how to configure that file properly. If Apollo Server v4 is backward compatible and seamless to use and update from v3 It will be easy solution for that small issue with path.

sergiycheck avatar Oct 06 '22 09:10 sergiycheck

So again — this is reverting a change that we made a year ago, and my understanding is that the change you're suggesting should not be correct since we actually removed the requirement of /graphql in v3.0.1: https://github.com/apollographql/apollo-server/blob/c3672608df6b691967703bf0c29753cac8e94ea6/packages/apollo-server-lambda/src/ApolloServer.ts#L46-L50

It's hard for me to prioritize evaluating the correctness of your change when it's to a package that is about to be unsupported and where the kind of issue (Apollo Server caring about the URL path) is also about to be fully solved.

Perhaps you are passing a path option to something when you configure your app?

glasser avatar Oct 07 '22 20:10 glasser

Found that app and tested again. I could not get response without path: graphql in serverless.yml config file. I provide the files that I used to get response with serverless offline with the link to the project aws serverless graphql apollo server

package.json

{
  "name": "aws-node-sls-apollo",
  "scripts": {
    "prebuild": "rimraf lib",
    "build": "tsc -p ./tsconfig.build.json",
    "start": "rimraf .build && sls offline --config sls.dev.yml",
    "deploy:dev": "sls -c sls.dev.yml deploy",
    "deploy:dev:func": "sls -c sls.dev.yml deploy function --function graphql",
    "remove:dev": "sls remove -c sls.dev.yml"
  },
  "devDependencies": {
    "serverless-offline": "^9.2.6",
    "serverless-plugin-typescript": "^2.1.2",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "apollo-server-lambda": "^3.10.1",
    "graphql": "^16.6.0",
    "rimraf": "^3.0.2",
    "serverless": "^3.22.0"
  }
}

graphql.ts

import { ApolloServer, gql } from 'apollo-server-lambda';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import { DocumentNode, GraphQLResolveInfo } from 'graphql';
import { APIGatewayEvent, Context, Callback } from 'aws-lambda';

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: (_parent, _args, _context, _info: GraphQLResolveInfo) => {
      return 'Hello world!';
    },
  },
};

function getServer(typeDefs: DocumentNode, resolvers) {
  const isDev = process.env.NODE_ENV === 'dev';
  let server: ApolloServer;
  if (!server) {
    server = new ApolloServer({
      typeDefs,
      resolvers,
      context: ({ event, context, express }) => {
        return {
          headers: event.headers,
          functionName: context.functionName,
          event,
          context,
          expressRequest: express.req,
        };
      },
      csrfPrevention: true,
      cache: 'bounded',
      introspection: isDev,
      plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
    });
  }

  return server;
}

exports.graphqlHandler = async function (
  event: APIGatewayEvent,
  context: Context,
  callback: Callback
) {
  const server = getServer(typeDefs, resolvers);
  const handler = server.createHandler();
  const result = await handler(event, context, callback);

  return result;
};

sls.dev.yml

service: aws-node-project

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs16.x
  region: eu-central-1
  deploymentMethod: direct
  environment:
    NODE_ENV: dev

plugins:
  - serverless-plugin-typescript
  - serverless-offline

functions:
  graphql:
    handler: src/graphql.graphqlHandler
    events:
      - http:
          path: graphql
          method: post
          cors: true
      - http:
          path: graphql
          method: get
          cors: true

custom:
  serverless-offline:
    httpPort: 3029
  serverlessPluginTypescript:
    tsConfigFileLocation: './tsconfig.build.json'

I start the project with the command

yarn run start

I query the server and get response in the playground

image

with the default configuration for serverless I can't get the response and get an Error.

default sls.dev.yml with path: /

service: aws-node-project

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs16.x
  region: eu-central-1
  deploymentMethod: direct
  environment:
    NODE_ENV: dev

plugins:
  - serverless-plugin-typescript
  - serverless-offline

functions:
  graphql:
    handler: src/graphql.graphqlHandler
    events:
      - http:
          path: /
          method: post
          cors: true
      - http:
          path: /
          method: get
          cors: true

custom:
  serverless-offline:
    httpPort: 3029
  serverlessPluginTypescript:
    tsConfigFileLocation: './tsconfig.build.json'

The response that I get with the config above

image

sergiycheck avatar Oct 08 '22 13:10 sergiycheck