4

I have defined a query in the query.qry file in my hyperledger composer app:

query selectPersonsByHobby {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE (passTime CONTAINS (name == _$targetHobby ))
}

Here's the relevant part of the model file:

participant Person identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
  o Interest[] passTime
}

concept Interest {
  o String name
  o String description
}

How can I access the query from my angular application and pass parameters;?

Is the following correct?:

return this.httpClient.get(`http://localhost:3000/api/queries/selectPersonsByHobby`, {_$targetHobby: "Football"});
steady_progress
  • 1,561
  • 6
  • 23
  • 52
  • You will have the sawgger generated with the API documentation, using that make a req with the required params in the req body. Try using [postman](https://www.getpostman.com/) and then you can make the get call in Angular accordingly. – Avinash Jun 23 '18 at 21:59

2 Answers2

3

I think this will work for you.

this.http.get("http://localhost:3000/api/queries/selectPersonsByHobby?targetHobby=Football")
Mohammed Ashfaq
  • 2,808
  • 2
  • 11
  • 18
  • thank you for your answer .... I need to keep the code flexible though ... with your solution I cannot pass a variable to the request .... I need sth like: targetHobby=Variable – steady_progress Jul 02 '18 at 01:14
  • We can make this request dynamic by appending the targetHobby value to Url. let serverUrl = "http://localhost:3000/api/queries/selectPersonsByHobby?targetHobby="; and valueOfTagetHobby = "Football" ; serverUrl = serverUrl + valueOfTargetHobby ; this.http.get(serverUrl); – Mohammed Ashfaq Jul 02 '18 at 05:44
1

from an angular app, this can be done in this way:

In app.service.ts file, we defined the getPersonByTargetHobby function where we can pass the url and target hobby value. By combining url + targethobby we will get query url.

/*app.service.ts File*/

import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable()
export class AppService {

   constructor(private http:HttpClient) {}

   getPersonByTargetHobby(url, tagetHobby) {
    let queryUrl = url + tagetHobbys;
    return this.http.get(queryUrl);
  }
}

The Above function {getPersonByTargetHobby} we can access in our component in this way

 /* First we need to import the app service in our component file {app.component.ts}*/

import { AppService } from '../app.service'
export class ComponentName implements OnInit {
 constructor(private _appService: AppService){}     
 ngOnInit() {

   url = "http://localhost:3000/api/queries/selectPersonsByHobby?targetHobby="

   tagetHobby = "Football"

   this._appService.getPersonByTargetHobby(url, tagetHobby).subscribe(
     data => {
       console.log("query response", data);
     },
     err => console.error(err),
    () => {}
  );
}

I have tested it, hopefully, it will work for u too.

Making API calls with the HttpClient service: https://www.metaltoad.com/blog/angular-5-making-api-calls-httpclient-service

Mohammed Ashfaq
  • 2,808
  • 2
  • 11
  • 18