LahiruE
LahiruE

Reputation: 65

Hyperledger Composer query not working

I'm developing an application using Hyperledger composer and its related part of the model file as bellows.

abstract participant Stakeholder {
 o String name
 o Address address
 o String email optional 
 o String telephone optional
 o Certification certification optional 
 o String[] images optional
 o Company company
 o String username
 o String password      
}

participant Farmer identified by stakeholderId extends Stakeholder {
 o String stakeholderId
 o String description optional  
 --> Farm[] farms
}

I'm trying to retrieve specific farmers by their username using below query.

query getUserFromUsername{
description:"get user from username"
statement:
        SELECT org.ucsc.agriblockchain.Stakeholder
          WHERE  (username == _$username) 
}

But it does not work as expected. Here, since Farmer is not the only stakeholder in the system, the Stakeholder abstract participant is used.

Any suggestions?

Upvotes: 0

Views: 184

Answers (2)

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

You can move the identified by stakeholderId to the abstract participation as mentioned below.

abstract participant Stakeholder identified by stakeholderId{
 o String stakeholderId
 o String name
 o Address address
 o String email optional 
 o String telephone optional
 o Certification certification optional 
 o String[] images optional
 o Company company
 o String username
 o String password      
}

participant Farmer  extends Stakeholder {
 o String description optional  
 --> Farm[] farms
}

To query for getUserFromUsername

query getUserFromUsername{
  description:"get user from username"
  statement:
    SELECT org.ucsc.agriblockchain.Farmer
    WHERE  (username == _$username) 
}

Upvotes: 1

R Thatcher
R Thatcher

Reputation: 5570

Because it is an abstract type, there is no data for the "Stakeholder" registry. You need to query the Farmer registry ... SELECT org.ucsc.agriblockchain.Farmer ...

Changing Stakeholkder from abstract to concrete won't help either if Farmer extends it because the Farmer will still be part of the Farmer registry.

I'm not sure exactly what you are wanting to achieve from the model, but you could revert to a single Participant type with optional fields for the different Stakeholder types, otherwise just write separate queries for the different Participant Types, and collate the results yourself in your code.

Update Following Comment

That Query against the Farmer registry should work.

Couple of hints ...

  • If you have changed the model or the Query, remember to stop and restart the REST server so that it can Discover the changes.
  • If you have changed the model, create some new test data.
  • If you are testing with the REST server, when you enter the parameter for the username, do not use quotes round the value.

(If you still have problems, please post the whole model, the latest query, and some example data that you are using in JSON format.)

Upvotes: 1

Related Questions