<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aashish Kumar on Medium]]></title>
        <description><![CDATA[Stories by Aashish Kumar on Medium]]></description>
        <link>https://medium.com/@asharies97?source=rss-da5ae6276ecd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kNVn78_3qlQT9lw44Vslqg.png</url>
            <title>Stories by Aashish Kumar on Medium</title>
            <link>https://medium.com/@asharies97?source=rss-da5ae6276ecd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 20:47:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asharies97/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nested GraphQL API queries on knowledge graphs in AWS Neptune]]></title>
            <link>https://medium.com/@asharies97/nested-graphql-api-queries-on-knowledge-graphs-in-aws-neptune-a3366593e73f?source=rss-da5ae6276ecd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3366593e73f</guid>
            <category><![CDATA[graphql-nested-queries]]></category>
            <category><![CDATA[aws-appsync]]></category>
            <category><![CDATA[aws-neptune]]></category>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Aashish Kumar]]></dc:creator>
            <pubDate>Tue, 04 Oct 2022 06:41:38 GMT</pubDate>
            <atom:updated>2022-10-04T09:01:32.816Z</atom:updated>
            <content:encoded><![CDATA[<h3>Nested GraphQL queries on knowledge graphs in AWS Neptune</h3><h3>Introduction</h3><p>AWS AppSync is a great service for serverless GraphQL API implementation and supports direct connectivity with native AWS services like DynamoDB and RDS, and indirect connectivity via lambda functions to other data sources i.e. AWS Neptune, OpenSearch, etc.</p><p>In this blog, we will see how nested GraphQL queries can be performed on knowledge graphs in AWS Neptune graph databases using AWS AppSync and Lambda.</p><h3>Architecture</h3><p>For this implementation, we will set up a Neptune graph DB, a Sagemaker notebook a GraphQL API on AppSync, and a lambda function for querying the KG in Neptune. Below is a high-level architecture diagram for our implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ayIgEc4d8DHUUC2xGBDpug.png" /><figcaption>Graph Database &amp; GraphQL API Architecture</figcaption></figure><h3><strong>Dataset &amp; The Data Model</strong></h3><p>We will be using AirRoutes dataset available in RDF format for this implementation. This data set has 4 classes Version, Continent, and Country, Airport and each class have a set of data properties and object properties. In our dataset, Airports are linked to their respective Continent and country via an object property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*5UCL-03JevP2YkQ1AK9luA.jpeg" /><figcaption>AirRoutes Data Classes</figcaption></figure><h3>The Problem</h3><p>The information is stored in multiple distributed classes and each Airport class is connected to the Continent and Country classes. However, the Country and Continent classes are not linked to each other.</p><p>Users want to identify all of the airports in a continent along with their respective country information. Creating a generic API that fetches this information at once can be an easy solution, but this won’t be an optimal one. As all of the users might not require the all information, so we want our API to be capable of resolving the fields dynamically and providing only the requested information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6TGWt6l4qdAuiJmkUnt6nQ.jpeg" /><figcaption>User Requests</figcaption></figure><h3><strong>Yes, We Have A Solution</strong></h3><blockquote>The optimal solution to above problem is the Nested GraphQL queries. GraphQL allows each field to be processed in its own specific way and query resolver to call other intermediate resolvers to process the nested fields. One class can be created as a nested field in other class and a separate resolver can be attached to this nested field.</blockquote><p>Whenever this nested field will be queried via the parent class, its dedicated resolver will come into action and will process it independently and return the results back to the parent class. The below diagrams represent the solution flow for User-1 and User-2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlTuTsZBFs5I5Gru3oGQAA.jpeg" /><figcaption>User-1 Solution Flow</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w--dSaZOo0KdPMiCm9tbiw.jpeg" /><figcaption>User-2 Solution Flow</figcaption></figure><h3><strong>Let’s Setup The Infrastructure</strong></h3><p>First, we will set up our Neptune graph database and the Sagemaker notebook. Neptune database will house our knowledge graph and Sagemaker notebooks will be used to load the AirRoutes data into the Neptune db.</p><p><strong>Configuring the Graph Infrastructure</strong></p><p>Let’s setup the AWS Neptune and Sagemaker notebook first</p><ol><li>Go to the Neptune console and click on “Launch Amazon Neptune”</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*nJwYAdjXymnTq7pZGsdJSw.png" /><figcaption>Neptune console</figcaption></figure><p>2. Provide the DB Cluster Identifier, Template, DB Instance class, and Availability, and connectivity configurations</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/1*N5SbtKQzKGCrz_DsSPiZ8Q.png" /><figcaption>Neptune db configuration</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*AVJrYj9ZWugrVjaFzlcgWQ.png" /><figcaption>Neptune db configuration-2</figcaption></figure><p>3. Provide the Sagemaker notebook configurations: Notebook instance type, Notebook name, and IAM role name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*NhSgwa5Yi7_rJgYLlLZ6TQ.png" /><figcaption>Sagemaker notebook configuration</figcaption></figure><p>4. Keep the remaining default configuration as it is and click on “create database”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*nY_LjVmljdDcoDKY5PohUg.png" /><figcaption>Create db</figcaption></figure><p>Once the DB creation is completed, your database cluster and Notebook should appear in the respective sections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*l86T8tjuZ5LdZuQJ9_QrSA.png" /><figcaption>Neptune database available</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*VJ_LtHgA67M-HlhpkjPQ0w.png" /><figcaption>Sagemaker notebook available</figcaption></figure><p><strong>Configure the API Service</strong></p><p>Our graph infrastructure is ready, Now we will set up our GraphQL API service. For this, We will configure our GraphQL API in AppSync and we will also set up a Lambda function acting as a bridge between the AppSync and Neptune.</p><ol><li>Go to the AWS Console and search for Lambda service.</li><li>Click on Create a function, provide the Lambda name, and select “Python 3.9” as the Runtime. Keep the other default configurations as it is and click on create function:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlA82MNKV0xNthyIhQ0ilA.jpeg" /><figcaption>Lambda Function Configuration</figcaption></figure><p>Post successful creation, the Lambda function will be available in the Lambda home.</p><p>3. Once the Lambda function is available, open the function and update it from the “Airports-Neptune-DataPull-LambdaFunction.zip” file available in the <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>git</em></a> repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*Ms8FzsWUJKVZHKJ_q7wO8Q.jpeg" /><figcaption>Update Lambda Function From .Zip</figcaption></figure><p>4. Update your Neptune Endpoint in the lambda_handeler function and lambda is ready.</p><p>We will now configure the GraphQL API in AppSync.</p><ol><li>Go to the AppSync Console and click on Create API</li><li>Select “Build from scratch” click on start and provide the API name</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oaLKANy6YdJy5t8LQj_BVA.jpeg" /><figcaption>Build API From Scratch</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*j1oJ-9n-jYQdppPDUoi3yQ.png" /><figcaption>Provide The API Name</figcaption></figure><p>3. Go to the schema and update the schema from the “AirRoutes-API-Schema.gql” file available in the <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>git</em></a> repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oiQyZb_09-P1gijinrLazA.jpeg" /><figcaption>Updating the GraphQL Schema</figcaption></figure><p>4. On the left pane click on the “Data Sources” and then click on “Create Data Source”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*nByX3PeVAJzkoJxUHiCQSg.png" /><figcaption>Adding data sources</figcaption></figure><p>5. Enter the data source name, select “AWS Lambda Function” as the data source type and select the region, then select the lambda function and click on create.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*lXnrqr7CekDSrLaB5cWEIw.png" /><figcaption>Lambda Data Source Configuration</figcaption></figure><p><strong>Identifying The Function and Field Resolvers</strong></p><p>So far, We have set up our Lambda function, AppSync API and Configured Lambda as a data source for API. But our API is not yet ready for usage, as we have not yet specified how to resolve the functions and different fields available in the schema.</p><p>Before configuring our resolvers, let’s first evaluate what all fields and functions are needed to be resolved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*2LMbiVcYg4ISs1t1Jn-kFQ.png" /><figcaption>API Requestmechanisms</figcaption></figure><p>In Schema, we have declared one API request mechanism “Query” of type <a href="https://graphql.org/learn/queries/"><em>query</em></a>, and there are multiple functions declared for type “Query”. Each of these functions requires a resolver to be attached for resolving the calls made to the function. Below is a compact definition of each function:</p><ol><li>getairports() : Takes Airport code as an argument, and returns the details of airports.</li><li>get_airports_bycountry() : Takes Country code as an argument, and returns the country information along with information of all Airports in that country (if requested).</li><li>get_airports_bycontinent() : Takes Continent code as an argument, and returns the continent information along with information of all Airports in that continent (if requested).</li></ol><p>Let’s Identify the required fields to be resolved independently. Looking at the Schema you can see that only <em>airport.country</em>, <em>country.airports,</em> and <em>continent.aiports</em> fields are non-string fields and these fields are referencing to other type objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fS11HQbuGxXCOpWA42FyeA.png" /><figcaption>Fields to be resolved</figcaption></figure><p>For each of these fields, we will have to add the field resolvers. While adding field resolvers, we have to secure a link between the parent class and the child class.</p><p>i.e. User requesting the Aiport information with airport code “OSL” should only get the details of its respective country. Similarly, user requesting all airports in the country “India” should only get airports from India only.</p><p><strong>Configuring The Function Resolvers</strong></p><p>A resolver has two components; a Request template and a Response template and these are written in the <a href="https://velocity.apache.org/engine/1.7/vtl-reference.html"><em>Apache VTL</em></a> . The request template transforms the user input received in GraphQL format to the target data source format and the Response template captures the result set received from the data source.</p><p>Let’s configure function resolvers to our three functions getairports(), get_airports_bycountry() and get_airports_bycontinent(). For attaching the resolvers, Go to the API &gt; Schema and click on the “Attach” button next to the function name and select the Lambda source created previously.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FidG0KvznETdEYAPn_rDuA.png" /><figcaption>Attaching the function resolvers</figcaption></figure><p>The <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>Git</em></a> repository contains the Request and Response templates of function, Configure the respective templates and save the resolvers. Once all function resolvers are configured properly, the API Schema page should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j02hnuL8fxc65y2FHZOfVg.png" /><figcaption>Function resolvers configured</figcaption></figure><p>Since our function resolvers are configured now, we can now try querying the API. On the left pane click on queries to navigate to the query editor.</p><p>Let’s call two functions: <em>getairport(code: “IXC”)</em> for retrieving the details of the airport with code “IXC” and <em>get_airports_bycontinent(code: “AS”) </em>for retrieving the information of the continent with code “AS<em>”. (</em>Refer to the<em> ‘Query-1.gql’ </em>file in the <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>git</em></a> repo)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8tycR3X9BRuy2c1OFHHbmQ.png" /><figcaption>Query results</figcaption></figure><p>If all configurations were made correctly, you should also receive the result in the right tab.</p><p>Let&#39;s try retrieving the nested information from both of these functions. Add Country fields to the <em>getairports()</em> function call and Airports fields to the <em>get_airports_bycontinent() </em>function call and run the query. <em>(</em>Refer to the<em> ‘Query-2.gql’ </em>file in the <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>git</em></a> repo)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4hTmB0Wfu93eJ9bfLTLzVw.png" /><figcaption>Nested query result</figcaption></figure><p>We are getting <em>null</em> in both of the nested fields. This is happening because these fields belong to other classes and our resolver does not know how to resolve these fields. This problem can be solved by attaching the filed resolvers to the nested fields</p><p><strong>Configuring The Field Resolvers</strong></p><p>The “Field Resolvers” folder in <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>Git</em></a><em> </em>repository contains all of the field resolver Request and Response templates. For this implementation, we will add resolvers to the <em>Airport.Country</em> and <em>Continent.Airport</em> fields.</p><p>Go to the schema and look for the nested fields in the Resolver tab. Click on “Attach” select the Lambda function data source and enter the respective Request and Response templates from the <a href="https://github.com/asharies97/Airports-GraphQL-API"><em>Git</em></a><em> </em>repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v-IXD6p9sSCQxvHbGltbLQ.png" /><figcaption>Field Resolvers</figcaption></figure><p>Once field resolvers are configured, go to the Queries tab and run the query again. This time we should get the information in both of the nested fields.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TnuzpjIoFTTWwddK93OTSQ.png" /><figcaption>Nested query result-1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-92virnpA_49iefNHxZRNA.png" /><figcaption>Nested query result-2</figcaption></figure><p>Now we have successfully configured our GraphQL API to resolve the nested fields dynamically. The result of parent class resolver results is passed to the child class resolvers via <em>$context.source </em>variable<em>. </em>Take a look at the Sparql query provided in the Request templates of the field resolvers.</p><p><em>References:</em></p><ul><li><a href="https://docs.aws.amazon.com/appsync/latest/devguide/configuring-resolvers.html">Configuring Resolvers</a></li><li><a href="https://graphql.org/learn/queries/">Queries | GraphQL</a></li></ul><blockquote>Hope this was helpful!!<br>See you in the next session!<br>Aashish Kumar</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3366593e73f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>