【问题标题】:Apollo GraphQL Playground: 401 UnauthorizedApollo GraphQL 游乐场:401 未经授权
【发布时间】:2020-01-08 16:33:57
【问题描述】:

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

但是,Playground 无法正常工作。页面加载,但我尝试从 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 失败。

【问题讨论】:

    标签: express graphql apollo-server


    【解决方案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 不提这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2011-03-06
      • 2018-05-19
      • 2020-06-11
      • 2017-02-22
      相关资源
      最近更新 更多