Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lambda.md #6876

Closed
wants to merge 1 commit into from
Closed

Conversation

sergiycheck
Copy link

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.

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.
@apollo-cla
Copy link

@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/

@netlify
Copy link

netlify bot commented Sep 2, 2022

Deploy Preview for apollo-server-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1e73732
🔍 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.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 2, 2022

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 1e73732:

Sandbox Source
Apollo Server Typescript Configuration
Apollo Server Configuration

@glasser
Copy link
Member

glasser commented Oct 3, 2022

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 glasser closed this Oct 3, 2022
@sergiycheck
Copy link
Author

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.

@glasser
Copy link
Member

glasser commented Oct 4, 2022

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 #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?

@sergiycheck
Copy link
Author

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.

@glasser
Copy link
Member

glasser commented Oct 7, 2022

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:

// By default, serverless integrations serve on root rather than
// /graphql, since serverless handlers tend to just do one thing and
// paths are generally configured as part of deploying the app.
{
path: '/',

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?

@sergiycheck
Copy link
Author

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants