Apollo GraphQL游乐场:未经授权的401

时间:2019-09-05 18:26:52

标签: express graphql apollo-server

我能够使用Express设置Apollo GraphQL Server。

但是,游乐场无法正常工作。该页面已加载,但我尝试从Playground进行的所有查询均失败,并显示HTTP 401 Unauthorized

我的代码:

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' }));

该Playground运行在localhost:4000 / graphql上,但是我进行的所有查询均失败,并显示HTTP 401 Unauthorized

1 个答案:

答案 0 :(得分:1)

您需要启用同源政策。参见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',
    },
  },
});

制作Playground(Prisma)的公司页面上存在的适当文档有些令人困惑。 docs on the Apollo website不要提及这个。