graphql-compose-elasticsearch icon indicating copy to clipboard operation
graphql-compose-elasticsearch copied to clipboard

ES results return a existing graphQL Object Type

Open KidA001 opened this issue 5 years ago • 1 comments

I'm successfully getting responses via GraphQL from elastic search (great, thank you!). The results that I get back from ES via graphQL contain data from our user model that we've uploaded into ES.

Ideally I would love to have a query endpoint on graphQL that returns our userObjectType, instead of the elastic data I've pasted below/

Is it possible to map the results I'm getting back from ES via graphql and instead lookup and return a userObjectType?

{
  "data": {
    "elastic": {
      "search": {
        "took": 18,
        "timed_out": false,
        "_shards": {
          "total": 10,
          "successful": 10,
          "skipped": 0,
          "failed": 0
        },
        "hits": {
          "total": 1,
          "max_score": 0.2876821,
          "hits": [
            {
              "_index": "users",
              "_type": "user",
              "_id": "XXnLi2UBTapVJORUwH3i",
              "_score": 0.2876821,
              "_source": {
                "id": "42c54556-45c2-4deb-9b87-868ea41cc5ef",
                "preferredUsername": "omg",
                "nickname": "OmarG",
                "firstName": "Omar",
                "lastName": "Goodfrey"
              }
            }
          ]
        }
      }
    }
  }
}

I'm new to GraphQL, so apologies if this is a bad question.

"elasticsearch": "^15.1.1",
"graphql": "^0.13.2",
"graphql-compose": "^4.8.2",
"graphql-compose-elasticsearch": "^1.10.1",
"graphql-relay": "0.5.5",
"graphql-sequelize": "8.3.1"

KidA001 avatar Aug 30 '18 18:08 KidA001

Sorry for late response. Crazy days till the end of the year.

I'm doing it via adding a new field to hits with name fromMongo:

// CvEsTC - it's TypeComposer generated from Elastic mapping
// CvTC - it's TypeComposer generated from mongoose model

CvEsTC.get('$search.hits').addRelation('fromMongo', {
  resolver: () => CvTC.getResolver('findById'),
  prepareArgs: {
    _id: source => source._id,
  },
  projection: { _id: true },
});

nodkz avatar Nov 12 '18 15:11 nodkz