Merge data from an external REST service with Hasura schema #9173
-
Hi Husara team. Thank you so much for building such a fantastic tool. It helped our team immensely! We have a question about how to merge data from an external REST service into our Hasura GraphQL schema. Let me explain our case in more detail. We are building a social platform with a concept of Posts with some CRUD operations around them. Hasura covers that part perfectly. Now we need to add search to our platform, and for that, we will use the Algolia service. Upon search request from the front end, we want to seamlessly integrate data returned from Algolia search results into our schema and merge it with our Posts objects. The first step we took was to build a Hasura Action around the Algolia REST API search endpoint. Here are the action types and an example response. type Query {
search_posts(
query: String!
request_options: jsonb
): SearchPostsOutput
}
type SearchPostsOutput {
hits: [PostHit]
page: Int!
nbHits: Int!
nbPages: Int!
hitsPerPage: Int!
}
type PostHit {
post_id: uuid!
title: String
content: String!
all_tags: [String]
objectID: String!
_highlightResult: PostHitHighlight
}
type PostHitHighlight {
title: HighlightResult
content: HighlightResult
all_tags: [HighlightResult]
}
type HighlightResult {
value: String!
matchLevel: String!
matchedWords: [String]
fullyHighlighted: Boolean
} An example response: {
"hits": [{
"title": "sssfffs",
"post_id": "c2fba996-2a11-42ad-a099-8d92a4d5ead0",
"content": "fff",
"all_tags": ["footag", "bartag"],
"community_tags": ["footag"],
"tooling_tags": ["bartag"],
"objectID": "posts-c2fba996-2a11-42ad-a099-8d92a4d5ead0",
"_highlightResult": {
"title": {
"value": "sssfffs",
"matchLevel": "none",
"matchedWords": []
},
"content": {
"value": "fff",
"matchLevel": "none",
"matchedWords": []
},
"all_tags": [{
"value": "<em>footag</em>",
"matchLevel": "full",
"fullyHighlighted": true,
"matchedWords": ["footag"]
}, {
"value": "bartag",
"matchLevel": "none",
"matchedWords": []
}]
}
}],
"nbHits": 1,
"page": 0,
"nbPages": 1,
"hitsPerPage": 20,
"exhaustiveNbHits": true,
"exhaustiveTypo": true,
"exhaustive": {
"nbHits": true,
"typo": true
},
"query": "footag",
"params": "query=footag",
"renderingContent": {},
"processingTimeMS": 1,
"processingTimingsMS": {
"total": 1
}
} The As an alternative, we considered merging Algolia results with Posts from our schema during the action execution in the code. But since Hasura doesn't allow to reuse the auto-generated types (i.e. Posts) in a custom action, we can't simply nest the What can you recommend in our case? How to best achieve what we want? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You should explore "configuring REST Connectors with Transform" (transform "response body" in your case). Here is the Hasura Document that you may find useful. |
Beta Was this translation helpful? Give feedback.
-
@georgy-scifind Let us know if you need any additional info from us. |
Beta Was this translation helpful? Give feedback.
@georgy-scifind
Thanks for your update. Looks like you have a working solution that meets your needs. Meanwhile, we also have developed a POC with Algolia API, that you may use as a reference.
Let us know if you need any additional info from us.