cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

cap cds: service to service connection access to compositions

former_member729305
Participant
0 Kudos
2,112

Dear community,

we are connecting one cap service to another to access and also maintain their entities. this works fine by

1) following the setup steps as documented in capire Consuming Services | CAPire (cloud.sap) (-> For a Remote CAP Service)

2) doing as follows in service.js file:

const externalService = await cds.connect.to('esternalService');
 srv.on('READ', 'EXTERNAL_ENTITY', async (req) => {
            const response = await externalService (req.query);
}

like this we can retrieve and access entities from another remote cap service. Still, in case we have compositions associated with our external target entities we are not allowed to access / maintain them. Does anyone know how we can achieve this?

we implemented the on READ logic as suggested here : BTP App external service (Step 2) but there we always end up with a

"Entity \"EXTERNAL_ENTITY\" is annotated with \"@cds.persistence.skip\" and cannot be served generically." error

Best Regards

Accepted Solutions (0)

Answers (1)

Answers (1)

martin-kl
Participant
0 Kudos

Hi Max,

we had the same problems and started to write our own implementation of a "generic" extend logic similar to the logic used in the tutorial, then we stumbled upon this npm package: https://www.npmjs.com/package/@sap/external-service-mashup.

Although undocumented, that seems to work really well in our scenarios (READ when expanding from local to remote entities).

Basic steps: create a MashupHandler instance (let's call it mashupHandler), call mashupHandler.init(<remote-entity>) for all your remote entities and simply register an on-READ handler like:

this.on('READ', [your, remote, entities], async (req, next) => {
return mashupHandler.handle({req, next});
})

Best Martin

t00707
Explorer
0 Kudos

Hello @martin-kl,
I am in the process of adding an external service to my CAP project to retrieve a list of our suppliers. To do this, I have added an association that refers to a supplier.
My problem now is that the association cannot be resolved by the standard implementation. Your comment seemed very interesting and also the package itself seems to be able to solve my problem with the navigation to an external association, but unfortunately the implementation is not sufficiently documented for me so that I can't get it installed.

Can you by chance share some more information on how I can implement the handler from the NPM package?

martin-kl
Participant

Hi @t00707,

you require the @sap/external-service-mashup via 

const MashupHandler = require('@sap/external-service-mashup');

at the beginning of a service. To use it, you have to instantiate it and tell it what entities are expanded and where it should run. A complete setup can look like this:

module.exports = class MyCapSrv extends cds.ApplicationService {
    async init() {
        const { ExternalEntity } = this.entities;

        // 1.: initiate mashup_handler and register external entities
        const mashup_handler = new MashupHandler(this);
        mashup_handler.init(ExternalEntity);

        // 2.: invoke mashup_handler on all local entities that you want to expand from
        this.on('READ', LocalEntityA, async (req, next) => {
            const ret = await mashup_handler.handle(req, next);
            return ret;
        });

        super.init();
    }
};​

where ExternalEntity is a projection on an entity from an external service. LocalEntityA on the other hand is one of your locally used entities, i.e. an entity that you model and use only in your CAP application.

Best, Martin

 

t00707
Explorer
0 Kudos
t00707
Explorer
0 Kudos
Hi @martin-kl, thank you, I actually tried the wrong entity. I just tested and deployed it and now the associations are resolved correctly.