How to create dynamic schema, query & resolvers in apollo server on the fly which is deployed on cloud?

OP @ SO : http://stackoverflow.com/questions/40757283/how-to-create-dynamic-schema-query-resolvers-in-apollo-server-on-the-fly-whic

Posting the question here for quick reference:


Please note I’m not developing an end-user app but a developer framework around apollo server (or graphql server, although preferably apollo server). Hence, I have this specific requirement.

Let’s, say I have deployed a simple apollo server with schema & query as below:

const typeDefinitions = `

type Query {
  testString : String
}

schema {
  query: Query
}
`;

export default [typeDefinitions];

Now, there’s a third-party service whose REST API I want to consume through the apollo server as a Graphql endpoint. Hence, I need to add schema, query & resolver for this Rest API so that I can consume it through my apollo server as a graphql endpoint.

How do I modify/update the above running (or deployed) graphql server to then look like below :

const typeDefinitions = `

type couchName {
  fname : String,
  lname : String
}

type Query {
  testString : String
  couchNames : couchName
}

schema {
  query: Query
}
`;

export default [typeDefinitions];

Post that I also need to add a resolver in let’s say resolvers.js file which would look like below:

import { CouchNames } from './connectors';

const resolvers = {
  Query: {
    testString(){
      return "This is hard-coded in resolvers";
    },
    couchNames() {
      return CouchNames.getOne();
    }
  },
};

export default resolvers;

Finally I need to add a connector in connectors.js as below:

import rp from 'request-promise-native'

const CouchNames = {
  getOne() {

    return rp('http://127.0.0.1:5984/testa/5884adf5879173b2fd58e5fae5000323')
      .then((res) => JSON.parse(res))
      .then((res) => {
          console.log(res);
        return res;
      });

  }
};

export { CouchNames };

How can I achieve the above on a running apollo server deployed on a cloud machine? As mentioned earlier, since I’m developing this as a developer framework; summing up my exact requirement: I need to have an ability to add schemas, queries, resolvers & connectors on the fly inside a running apollo server (possibly hosted on any cloud like Digital Ocean) since the REST APIs would be unknown when the apollo server is deployed on the cloud machine for the first time.

Please let me know if this question is confusing or if further details are needed.

P.S.: I am new to graphql and apollo server.

Where you able to come up with any sort of solution this type of problem?