繁体   English   中英

graphql如何使用羽毛?

[英]How to use feathers with graphql?

我想在羽毛中构建 graphql。 并尝试以下方法。

from index.js

/* eslint-disable no-console */
const logger = require('./logger');
const app = require('./app');
const port = app.get('port');


const { ApolloServer } = require('apollo-server-express');
const { typeDefs } = require('../schema')
const { resolvers } = require('../resolvers')

let apolloServer = null;
async function startServer() {
  apolloServer = new ApolloServer({
    typeDefs,
    resolvers
  });
  await apolloServer.start();
  apolloServer.applyMiddleware({ app });
}
startServer();


const server = app.listen(port);
process.on('unhandledRejection', (reason, p) =>
  logger.error('Unhandled Rejection at: Promise ', p, reason)
);

server.on('listening', () =>
  logger.info('Feathers application started on http://%s:%d', app.get('host'), port)
);

此代码参考“apollo-server-express”示例并将其用于羽毛。 但获取info: Page not found {"className":"not-found","code":404,"data":{"url":"/graphql"},"errors":{},"name":"NotFound","type":"FeathersError"}我认为这个错误与羽毛路由不匹配,并创建一个服务来尝试解决它。

from service-graphql

const { ApolloServer } = require('apollo-server-express');
const { typeDefs } = require('../../../schema')
const { resolvers } = require('../../../resolvers')

module.exports = async function (app) {
  // const options = {
  //   paginate: app.get('paginate')
  // };

  let apolloServer = null;
  async function startServer() {
    apolloServer = new ApolloServer({
      typeDefs,
      resolvers
    });
    await apolloServer.start();
    apolloServer.applyMiddleware({ app });
  }
  // startServer();

  // Initialize our service with any options it requires
  app.use('/graphql', await startServer);

  // Get our initialized service so that we can register hooks
  // const service = app.service('graphql');

  // service.hooks(hooks);
};

但它也失败了。 我如何构建基础羽毛和 graphql 项目。 这是我的完整代码https://github.com/CoreyHuang/baseFeathers

在使用 FeathersJS 和 Apollo 将现有设置升级到 Apollo 3 时,我遇到了同样的问题。对我来说,我能够修复它,当我最终发现由于某种原因触发了 FeathersJS not found 中间件而不是 Apollo 时。 我的快速解决方法是这样修改 app.js:

const notFound = express.notFound();
app.use(function (req, res, next) {
  const isGraphQl = req.url.startsWith('/graphql');
  return isGraphQl ? next() : notFound(req, res, next);
});

可能有一些更好的方法可以做到这一点,它可能与一些在错误时间运行的异步代码有关,但这至少为我修复了它。

更新:

该问题是由在 notFound 处理程序之后注册的 Apollo 处理程序引起的。 我通过将整个 Feathers 应用程序初始化转换为异步 function 来解决此问题。 在 app.js 中:

async function getApp() {
  const app = express(feathers());
  app.configureAsync = async function configureAsync(fn) {
    await fn.call(this, this);

    return this;
  };
  ...
  await app.configureAsync(graphQLService);
  ...
  return app;
}

然后在 index.js 中:

async function start() {
  const app = await getApp();
  const port = app.get('port');
  const server = app.listen(port);

  server.on('listening', () =>
    logger.info('Feathers application started on http://%s:%d', app.get('host'), port)
  );
}

start();

您可以在这里查看我的代码: https://github.com/Purrrrrr/Tanssiaistietokanta/tree/main/backend

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM