apollo-server-integrations / apollo-server-integration-aws-lambda

An integration to use AWS Lambda as a hosting service with Apollo Server
MIT License
46 stars 9 forks source link

Apollo Server supports only GET/POST requests. #103

Closed RoyLuoNanjing closed 1 year ago

RoyLuoNanjing commented 1 year ago

Hi, I am trying to deploy apollo server on aws lambda as a part of my msc project. I got so confused with how it works because there are not many up-to-date videos. Now I managed to come this far but coming with a issue "Apollo Server supports only GET/POST requests."

my front end is : const client = new ApolloClient({ link: new HttpLink({ uri: '...' }), fetchOptions: { mode: 'no-cors', }, cache: new InMemoryCache(), }) const GET_Hello = gql query GetHello { hello }

const { loading, error, data } = useQuery(GET_Hello) console.log(data) if (loading) return

Loading...

if (error) return

Error : {error.message}

my backend on aws is : import { ApolloServer } from '@apollo/server' import { startServerAndCreateLambdaHandler, handlers } from '@as-integrations/aws-lambda'

const typeDefs = #graphql type Query { hello: String }

const resolvers = { Query: { hello: () => 'world', }, }

const server = new ApolloServer({ typeDefs, resolvers, csrfPrevention: false })

//v2 export const graphqlHandler = startServerAndCreateLambdaHandler( server, // We will be using the Proxy V2 handler handlers.createAPIGatewayProxyEventV2RequestHandler(), )

the error is : allow": "GET, POST", "content-type": "application/json; charset=utf-8", "content-length": "881" }, "body": "{\"errors\":[{\"message\":\"Apollo Server supports only GET/POST requests.\",\"extensions\":{\"code\":\"BAD_REQUEST\",\"stacktrace\":[\"BadRequestError: Apollo Server supports only GET/POST requests.\",\" at new GraphQLErrorWithCode (file:///var/task/node_modules/@apollo/server/dist/esm/internalErrorClasses.js:7:9)\",\" at new BadRequestError (file:///var/task/node_modules/@apollo/server/dist/esm/internalErrorClasses.js:75:9)\",\" at runHttpQuery (file:///var/task/node_modules/@apollo/server/dist/esm/runHttpQuery.js:120:19)\",\" at runPotentiallyBatchedHttpQuery (file:///var/task/node_modules/@apollo/server/dist/esm/httpBatching.js:34:22)\",\" at ApolloServer.executeHTTPGraphQLRequest (file:///var/task/node_modules/@apollo/server/dist/esm/ApolloServer.js:499:26)\",\" at async Runtime.handler (/var/task/node_modules/@as-integrations/aws-lambda/dist/lambdaHandler.js:18:30)\"]}}]}\n" }

can anyone please help me check what the issue is ?

BlenderDude commented 1 year ago

This is because of the OPTIONS preflight request that is being sent before the GET request. You can setup API Gateway, Function URLs, and the like to capture this request instead of having the Lambda handle it. CORS is a common request to my plan is to add it to this library as a built-in helper. Please see the discussion over at #85