0
votes

I was able to set up Apollo GraphQL Server with Express.

However, the Playground is not working. The page loads but all queries I try to make from the Playground fail with HTTP 401 Unauthorized.

My code:

const server = new ApolloServer({
  schema: MySchema,
  context: ({ req }) => ({
    connection: req.context.db,
    auth: req.context.auth,
  }),
  playground: true,
});

// Add to Express
app.use(server.getMiddleware({ path: '/graphql' }));

The Playground is running at localhost:4000/graphql, however all queries I make fail with HTTP 401 Unauthorized.

1

1 Answers

1
votes

You need to enable the same-origin policy. See the GraphQL Playground docs.

const server = new ApolloServer({
  schema: MySchema,
  context: ({ req }) => ({
    connection: req.context.db,
    auth: req.context.auth,
  }),
  playground: {
    settings: {
      // Needed for auth
      // Docs: https://github.com/prisma/graphql-playground
      ['request.credentials']: 'same-origin',
    },
  },
});

It's a bit confusing the proper docs live on the page of the company that made the Playground (Prisma). The docs on the Apollo website don't mention this.