apollo-datasource-soap

A simple implementaion of apollo datasource for soap requests

View the Project on GitHub RishikeshDarandale/apollo-datasource-soap

apollo-datasource-soap

CircleCI CodeQL Analysis Known Vulnerabilities npm npm NpmLicense

A simple implementation of apollo datasource for soap requests

Connect the SOAP based services to your Apollo server using Data Sources

apollo-datasource-soap versions:

Soap Data Source

Install

npm install --save apollo-dataSource-soap

or

yarn add apollo-dataSource-soap

Usage

Define a data source by extending the SOAPDataSource class. You can invoke the soap method with param by invoking invoke method.

The cache can be initialized as specified in [apollo migration 4 guide]

class TestSoapDataSource extends SOAPDataSource {
  constructor() {
    // pass the cache as per your requirement
    super('http://www.thomas-bayer.com/axis2/services/BLZService?wsdl', {cache: new InMemoryLRUCache<string, any>()});
  }

  async willSendRequest(options) {
    // override the soap endpoint for all requests
    options.endpoint = 'http://www.thomas-bayer.com/axis2/services/BLZService';
    // these will be used for all soap calls
    options.wsdl_headers = {
      Authorization: token,
    }
  }

  async getBank() {
    return await this.invoke('getBank', {blz: 37050198});
  }
}

SOAP Cache utility

SOAP is sent as HTTP POST and its a non-idempotent. Thus it can not be cached at HTTP level.

This is draft verison of document for response caching for SOAP, but did not found any implementaion of it.

Thus it make sense to client decide to cache it or not and for how much duration.

Specify the ttl to cache the SOAP response.

class TestSoapDataSource extends SOAPDataSource {
  constructor() {
    // pass the cache as per your requirement
    super('http://www.thomas-bayer.com/axis2/services/BLZService?wsdl', {cache: new InMemoryLRUCache<string, any>()});
  }

  async willSendRequest(options) {
    // override the soap endpoint for all requests
    options.endpoint = 'http://www.thomas-bayer.com/axis2/services/BLZService';
    // these will be used for all soap calls
    options.wsdl_headers = {
      Authorization: token,
    }
  }

  async getBank() {
    // cache the response for 1 hour
    return await this.invoke('getBank', {blz: 37050198}, {ttl: 3600});
  }
}

Decide when to cache

There might be a situation where client needs to decide the response should be cached based on response code. This can be achieved overriding the method shouldCache method. shouldCache method returns a boolean flag to indicate if response can be cached or not. Please take a look at below example:

class TestSoapDataSource extends SOAPDataSource {
  constructor() {
    // pass the cache as per your requirement
    super('http://www.thomas-bayer.com/axis2/services/BLZService?wsdl', {cache: new InMemoryLRUCache<string, any>()});
  }

  async willSendRequest(options) {
    // override the soap endpoint for all requests
    options.endpoint = 'http://www.thomas-bayer.com/axis2/services/BLZService';
    // these will be used for all soap calls
    options.wsdl_headers = {
      Authorization: token,
    }
  }

  async getBank() {
    // cache the response for 1 hour
    return await this.invoke('getBank', {blz: 37050198}, {ttl: 3600});
  }

  shouldCache(response) {
    return response.code === 0 ? true : false;
  }
}

Issue or need a new feature?

If you are experiencing a issue or wanted to add a new feature, please create a github issue here.

Contributing

:star: Star me on GitHub — it helps!

:heart: contribution: Here is contributing guide in deatil.

For impatient here are quick steps: