简体   繁体   中英

How to use feathers with graphql?

I want to build graphql in feathers. And try below method。

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

This code refer "apollo-server-express" example and take it to feathers. but get info: Page not found {"className":"not-found","code":404,"data":{"url":"/graphql"},"errors":{},"name":"NotFound","type":"FeathersError"} i think this error is not match feather route,and also create a service to try solve it.

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

But it also fail. How can i build a base feathers and graphql project. This is my full code https://github.com/CoreyHuang/baseFeathers

I bumped into the same problem when upgrading my existing setup with FeathersJS and Apollo to Apollo 3. For me I was able to fix it, when I finally figured that for some reason the FeathersJS not found middleware was being triggered instead of the Apollo one. My quick fix was to modify app.js as such:

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

There's probably some better way to do it and it's probably somehow related to some async code running at the wrong time, but this at least fixed it for me.

Update:

The problem was caused by the Apollo handler registering after the notFound handler. I fixed this by turning my whole Feathers app initialization into an async function. In 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;
}

Then in 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();

You can take a look at my code here: https://github.com/Purrrrrr/Tanssiaistietokanta/tree/main/backend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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