question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using GraphQLServerLambda, playground is not working. Getting a 404.

See original GitHub issue

As the title says, I have a GraphQLServerLambda implementation that works wonderful with serverless, getting responses from the graphqlHandler, but when I try to use the playgroundHandler endpoint the playground client loads but it seems to not be able to load info. Let alone reach the server.

This is my config:

import { makeExecutableSchema } from "graphql-tools"
import { GraphQLServer, GraphQLServerLambda } from "graphql-yoga"
import mongoose from "mongoose"
import dotenv from "dotenv"

dotenv.config()

const typeDefs = `
  type Query {
    hello(name: String): String
  }
`

const resolvers = {
  Query: {
    hello: (_,{ name }) => `Hello ${name || 'world'}`
  }
}

// Executable Schema
const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
})

const port = process.env.PORT || 4000
// Express init

// Mongoose setup
mongoose.Promise = global.Promise // Mongoose promises deprecated, needed to reassign
mongoose.connect(process.env.MONGO_URL, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
})

const context = async (context) => {
  let headers, request

  if (context.event?.headers) {
    // for serverless context
    headers = context.event.headers
    request = context.requestContext
  } else {
    // for local context
    headers = context.request.headers
    request = context.request
  }

  return {
  	headers
    currentUser,
    request,
  }
}

const server = new GraphQLServerLambda({
  schema,
  context,
  tracing: true,
  cacheControl: true,
})

exports.graphqlHandler = server.graphqlHandler
exports.graphqlPlayground = server.playgroundHandler

// This is how I've been running it on local
if (process.env.NODE_ENV === "dev") {
  const localServer = new GraphQLServer({
    schema,
    context,
    tracing: true,
    cacheControl: true,
  })

  const options = {
    port,
  }

  localServer.start(options, ({ port }) =>
    console.log(
      `Server started, listening on port ${port} for incoming requests.`,
    ),
  )
}

This is my serverless file:

# serverless.yml

service: apollo-lambda

# Use serverless-webpack plugin to transpile ES6/ES7
plugins:
  - serverless-webpack
  - serverless-dotenv-plugin
  - serverless-offline

# Configuration for serverless-webpack
# Enable auto-packing of external modules
custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

provider:
  name: aws
  runtime: nodejs12.x
functions:
  graphql:
    # this is formatted as <FILENAME>.<HANDLER>
    handler: src/server.graphqlHandler
    events:
      - http:
          path: /graphql
          method: post
          cors: true
      - http:
          path: /graphql
          method: get
          cors: true
  playground:
    handler: src/server.graphqlPlayground
    events:
    - http:
        path: /playground
        method: get
        cors: true

This is what I get on the playground:

Screen Shot 2021-02-10 at 11 36 32 PM

Let me know if you need some clarification! Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mishchiefcommented, Mar 30, 2021

You can check their docs, should be very straight forward https://www.apollographql.com/docs/apollo-server/deployment/lambda/

1reaction
mishchiefcommented, Mar 30, 2021

No dice @pratiksyngenta I ended up ripping out graphql yoga and going with Apollo Graphql and their Lambda solution which works wonderfully!

It seems like the prisma lab folks have abandoned the project so it was easier for me to just switch to Apollo. Hope this helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Playground not loading with Express Middleware throwing 404
This issue pertains to the following package(s): GraphQL Playground - Electron App GraphQL Playground HTML GraphQL Playground GraphQL ...
Read more >
Error handling - Apollo GraphQL Docs
If a request uses an invalid HTTP method ( GET with a mutation, or any HTTP method other than GET or POST ),...
Read more >
Build fails with 404 Not Found at Netlify function path with ...
Hi I am implementing an Apollo graphql server using a Netlify lambda. The graphql client is configured to use process.env.
Read more >
Get 404 error when connecting to apollo graphQL server from ...
It is a port issue, port 6000 is one of the ports not consider safe for most browsers, even though you can curl...
Read more >
GraphQL By Example: With Graphene, Flask, and Fauna
Let's talk about GraphQL shall we?. In this article, I'll be going over how to create simple GraphQL servers the conventional way, with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found