apimeteorgraphqlnimblerestivus

nimble-restivus package does not work in Meteor GraphQL


I setup GraphQL in Meteor server, and I created a simple API using nimble-restivus package. But I can not call to this API. Please help.

This is API setup:

  var Api = new Restivus({
    useDefaultAuth: true,
    prettyJson: true,
  });
  Api.addRoute('status', {
    post: function() {
      return { status: 'OK' };
    },
  });

This is command line to test api:

curl -X POST http://localhost:3000/api/status

This is config GraphQL on Meteor server:

import { Meteor } from 'meteor/meteor';
import express from 'express';
import {
  graphqlExpress,
  graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { schema } from './schema';

const server = express();

const whitelist = [ 'http://localhost:3000' ];
const corsOptions = {
  origin(origin, callback){
    const originIsWhitelisted = whitelist.indexOf(origin) !== -1;
    callback(null, originIsWhitelisted);
  },
  credentials: true,
};
server.use(cors(corsOptions));
// server.use('*', cors({ origin: Meteor.settings.GraphQL.clientURL }));
server.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
  subscriptionsEndpoint: 'ws://localhost:3000/subscriptions',
}));

// We wrap the express server so that we can attach the WebSocket for subscriptions
const ws = createServer(server);
ws.listen(3000, () => {
  console.log('GraphQL Server is running');
  // Set up the WebSocket for handling GraphQL subscriptions
  new SubscriptionServer({
    execute,
    subscribe,
    schema
  }, {
    server: ws,
    path: '/subscriptions',
  });
});

This is the result after run command:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/status</pre>
</body>
</html>

Solution

  • I fixed it. The reason is Meteor server and GraphQL are running in the same port (port 3000). We need to change port of GraphQL to other port (for example: 3002, etc)