Hi,
I am trying to add a route into NodeBB to serve GraphQL API.
After doing some research on Google, I find a popular package named Apollo GraphQL, which has a document to show how to integrate Apollo GraphQL server with existing Express server, can help me to do my job.
Then, I tried to create a plugin and put the example code of graphql with a route path:/mygraphql
into my library.js
. But, the nodebb keep returning 404 Not found
. Did I do anything wrong?
Here is what my library.js
looks like:
const { ApolloServer, gql } = require('apollo-server-express');
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
const resolvers = {
Query: {
books: () => books,
},
};
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
});
const plugin = {};
plugin.init = function (params, callback) {
const app = params.app;
apolloServer.applyMiddleware({
app,
path: '/api/mygraphql',
});
callback();
};
module.exports = plugin;
Thanks!