【问题标题】:How can I add a custom endpoint for Stripe or any microservice webhook?如何为 Stripe 或任何微服务 webhook 添加自定义端点?
【发布时间】:2021-06-10 21:39:24
【问题描述】:

我有一个 apollo graphql 服务器,我想为该 Stripe 或一些微服务添加一个自定义端点,如何添加其他端点?

我不想创建其他项目。

const app = express();

var corsOptions = {
  origin:["https://localhost:3000"],
  credentials: true
};

// RESOLVERS
const resolvers = require("./graphql/resolvers/index");

const resolveFunctions = {
  JSON: GraphQLJSON,
  Upload: GraphQLUpload
};

// models
const Admin = require("./models/Admin");

const pubsub = new PubSub();

// SCHEMA
const server = new ApolloServer({
  typeDefs: importSchema("./graphql/schema.graphql"),
  resolvers,
  resolveFunctions,
  context: ({ req }) => ({
    Admin,
    activeAdmin: req ? req.activeAdmin : null
  })
});

app.use(async (req, res, next) => {
  const token = req.headers["authorization"];
  if (token && token !== "null") {
    try {
      const activeAdmin = await jwt.verify(token, process.env.SECRET_KEY);
      req.activeAdmin = activeAdmin;
    } catch (e) {
      console.log("ERROR", e);
    }
  }

  next();
});

server.applyMiddleware({ app, cors: corsOptions });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(process.env.PORT || 4004, () =>
  console.log(
    `???? Server ready at http://localhost:4004${server.graphqlPath}`
  )
);

那么如何为 webhook 添加多个端点? 谢谢

【问题讨论】:

    标签: node.js graphql apollo-server


    【解决方案1】:

    是的,这是一个棘手的问题。我认为这还不被支持。但是,有一个技巧可以使用 Playground 端点进行 webhook 集成。有一个开放的GitHub issue 突出了一个类似的问题。我引用了该线程的解决方法。

    从该线程引用mondaychen。

    我的计划是在参数中添加一个新选项 playgroundPath 应用中间件。如果未指定,它将与路径相同,并且 一切都将保持不变;当它与路径不同时,它将 被用作游乐场的 URL,但端点将 仍然是路径。同时,将属性 playgroundPath 添加到 服务器实例,以便人们可以轻松地从外部读取它。

    这是来自Pocket-titan的相同想法的实现

    import express from 'express'
    import { ApolloServer } from 'apollo-server-express'
    
    const app = express()
    
    const server = new ApolloServer({
        playground: {
            endpoint: `http://${your_domain_here}:${your_port_here}/graphql-playground`,
        },
    })
    
    server.applyMiddleware({
        app,
        path: '/api/graphql',
    })
    
    app.listen(your_port_here, () => {
        console.log(`Serving graphql api on /api/graphql and playground on /graphql-playground!`)
    })
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 2020-02-23
      相关资源
      最近更新 更多