Home

Awesome

oc-graphql-client Build Status

NOTICE!


A OpenComponents plugin that expose the a graphql client for interacting with a GraphQL based server.

Requirements:

Install

yarn add oc-graphql-client

Registry setup

More info about integrating OC plugins: here

...
var registry = new oc.registry(configuration);

registry.register({
  name: 'graphqlClient',
  register: require('oc-graphql-client'),
  options: {
    serverUrl: 'http://graphql-server.hosts.com'
  }
}, function(err){
  if(err){
    console.log('plugin initialisation failed:', err);
  } else {
    console.log('graphql client now available');
  }
});

...

registry.start(callback);

Register API

parametertypemandatorydescription
serverUrlstringyesThe Url for the GraphQL server

Usage

Example for a components' server.js:


module.exports.data = function(context, callback){
  const query = `
  query restaurantInfo($id: Int!) {
      restaurant(id: $id) {
        name
    }
  }`;

  const headers = {
    'accept-language': 'en-US, en'
  };

  context.plugins.graphql.query({ query, variables: { id: 4 } }, headers, timeout)
    .then(res => { ... })
    .catch(err => { ... })

API

parametertypemandatorydescription
optionsobjectyesA composite of the query & variables to pass to GraphQL server
headersobjectnoThe headers to pass down to unerlying services
timeoutintnoThe timeout in ms. It defaults to OS default

Contributing

PR's are welcome!

License

MIT