1

I'm new to Rxjs (and Angular2 in general) and I'm having trouble understanding the subtleties of RxJS.

I want to make two REST calls to the GitLab API:

  1. Receive all the groups of a certain user (via gitlab.com/api/v4/groups). this will give me back a JSON like this one:

    [
    {
        "id": 1511397,
        "name": "some name",
        "parent_id": 1505403,
    ...
    },
    {
        "id": 1511403,
        "name": "some other name",
        "parent_id": 1505403,
    ...
    }
    ]
  2. Receive all the projects of per group (via gitlab.com/api/v4/groups/:id) which give you a detailed version of 1 group:

    {
    "id": 1511397,
    "name": "group name",
    "parent_id": 1505403,
    "projects": [
        {
            "id": 3099499,
            "description": "project 1"
        },
        {
            "id": 3099489,
            "description": "Project 2"
        }
    ]
    }

So basically I need to loop over all ID's given by the first request and deliver an array of group-details:

[
 {
 "id": 1511397,
 "name": "group name",
 "parent_id": 1505403,
 "projects": [
     {
         "id": 3099499,
         "description": "project 1"
     },
     {
         "id": 3099489,
         "description": "Project 2"
     }
 ]
 },
 {
 "id": 1194997,
 "name": "a second group name",
 "parent_id": 152393,
 "projects": [
     {
         "id": 9423423,
         "description": "project 3"
     },
     {
         "id": 2394238,
         "description": "Project 4"
     }
 ]
 }
]

How can this be done? I already tried something along the lines of switchMap, concatMap and MergeMap, but I never can get it to work...

2 Answers 2

1

you can do something like this

getGroups() {
  return this.http.get('gitlab.com/api/v4/groups')
    .map(res => res.json()) // convert to object[]
    .map(res => res.map(g => g.id)) // get all id
    .mergeMap(gid => {
      let gs = [];
      gid.forEach(id => {
        let req = this.http.get(`gitlab.com/api/v4/groups/${id}`);
        gs.push(req);
      });
      return Observable.forkJoin(gs);
    })
    .map(res => { // we have array of Response Object
        return res.map(x => x.json()); // Array.map not Observable map operator.
    });
}

getGroups().subscribe(res => console.log(res))
Sign up to request clarification or add additional context in comments.

Comments

0

Something like that. You need to replace static Observable.of to $http observables.

Rx.Observable.of([{userId:1}, {userId:2}])
.switchMap(ids => Rx.Observable.of(...ids))
.mergeMap(userId => {
 return Rx.Observable.of([
   {userId:userId, name:'project1'},
   {userId:userId, name:'project2'}
 ]);
})
.subscribe(x=>console.log(x));
  1. Rx.Observable.of([{userId:1}, {userId:2}]) - is like http call that gives u array of users 2 .switchMap(ids => Rx.Observable.of(...ids)) - for each user you create a separate observable
  2. .mergeMap for each user observable you call $http to get projects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.