<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tingting Jiang</title>
    <description>The latest articles on DEV Community by Tingting Jiang (@tingtingjh).</description>
    <link>https://dev.to/tingtingjh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F386943%2Fda865318-4c88-4f4f-acb4-d388310ec236.jpeg</url>
      <title>DEV Community: Tingting Jiang</title>
      <link>https://dev.to/tingtingjh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tingtingjh"/>
    <language>en</language>
    <item>
      <title>AWS Amplify ElasticSearch Query for Interface/Union type with AND/OR operations</title>
      <dc:creator>Tingting Jiang</dc:creator>
      <pubDate>Sat, 23 May 2020 18:30:38 +0000</pubDate>
      <link>https://dev.to/tingtingjh/aws-amplify-elasticsearch-query-for-interface-union-type-with-and-or-operations-1ogp</link>
      <guid>https://dev.to/tingtingjh/aws-amplify-elasticsearch-query-for-interface-union-type-with-and-or-operations-1ogp</guid>
      <description>&lt;p&gt;AWS Amplify GraphQL Transform can automatically create the backend API according to your data model schema. With built-in Directives, such as @model, @connection, &lt;a class="mentioned-user" href="https://dev.to/key"&gt;@key&lt;/a&gt;, and @searchable, Amplify further facilitates the connection between your data models with other Amazon Web services including DynamoDB and ElasticSearch.&lt;/p&gt;

&lt;p&gt;For example, with the data models, Collection and Archive, our initial &lt;em&gt;amplify/backend/api/api_name/schema.graphql&lt;/em&gt; might look like:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;type Collection
  @model
  @searchable
  @key(
    fields: ["identifier"]
    name: "Identifier"
    queryField: "collectionByIdentifier"
  ) {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  collection_category: String!
  visibility: Boolean!
  parent_collection: [String!]
  archives: [Archive] @connection(name: "CollectionArchives")
}
type Archive
  @model
  @searchable
  @key(
    fields: ["identifier"]
    name: "Identifier"
    queryField: "archiveByIdentifier"
  ) {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  parent_collection: [String!]
  item_category: String!
  visibility: Boolean!
  collection: Collection @connection(name: "CollectionArchives")
}&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;By running “amplify push”, a full CloudFormation template will be set up for you with your new API. In the &lt;em&gt;graphql/queries.js&lt;/em&gt;, you can see the automatically generated queries. In particular, @searchable directive streams your data into the ElasticSearch engine with &lt;em&gt;searchArchives&lt;/em&gt; and &lt;em&gt;searchCollections&lt;/em&gt; queries that are ready at your disposal.&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;/* eslint-disable */
// this is an auto generated file. This will be overwritten

export const getCollection = /* GraphQL */ `
  query GetCollection($id: ID!) {
    getCollection(id: $id) {
      id
      title
      identifier
      description
      creator
      source
      circa
      date
      collection_category
      visibility
      parent_collection
      archives {
        items {
          id
          title
          identifier
          description
          date
          parent_collection
          item_category
          visibility
        }
        nextToken
      }
    }
  }
`;
export const listCollections = /* GraphQL */ `
  query ListCollections(
    $filter: ModelCollectionFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listCollections(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        title
        identifier
        description
        date
        collection_category
        visibility
        parent_collection
        archives {
          nextToken
        }
      }
      nextToken
    }
  }
`;
export const getArchive = /* GraphQL */ `
  query GetArchive($id: ID!) {
    getArchive(id: $id) {
      id
      title
      identifier
      description
      date
      parent_collection
      item_category
      visibility
      collection {
        id
        title
        identifier
        description
        date
        collection_category
        visibility
        parent_collection
        archives {
          nextToken
        }
      }
    }
  }
`;
export const listArchives = /* GraphQL */ `
  query ListArchives(
    $filter: ModelArchiveFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listArchives(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        title
        identifier
        description
        date
        parent_collection
        item_category
        visibility
        collection {
          id
          title
          identifier
          description
          date
          collection_category
          visibility
          parent_collection
        }
      }
      nextToken
    }
  }
`;
export const collectionByIdentifier = /* GraphQL */ `
  query CollectionByIdentifier(
    $identifier: String
    $sortDirection: ModelSortDirection
    $filter: ModelCollectionFilterInput
    $limit: Int
    $nextToken: String
  ) {
    collectionByIdentifier(
      identifier: $identifier
      sortDirection: $sortDirection
      filter: $filter
      limit: $limit
      nextToken: $nextToken
    ) {
      items {
        id
        title
        identifier
        description
        date
        collection_category
        visibility
        parent_collection
        archives {
          nextToken
        }
      }
      nextToken
    }
  }
`;
export const archiveByIdentifier = /* GraphQL */ `
  query ArchiveByIdentifier(
    $identifier: String
    $sortDirection: ModelSortDirection
    $filter: ModelArchiveFilterInput
    $limit: Int
    $nextToken: String
  ) {
    archiveByIdentifier(
      identifier: $identifier
      sortDirection: $sortDirection
      filter: $filter
      limit: $limit
      nextToken: $nextToken
    ) {
      items {
        id
        title
        identifier
        description
        date
        parent_collection
        item_category
        visibility
        collection {
          id
          title
          identifier
          description
          date
          collection_category
          visibility
          parent_collection
        }
      }
      nextToken
    }
  }
`;
export const searchCollections = /* GraphQL */ `
  query SearchCollections(
    $filter: SearchableCollectionFilterInput
    $sort: SearchableCollectionSortInput
    $limit: Int
    $nextToken: String
  ) {
    searchCollections(
      filter: $filter
      sort: $sort
      limit: $limit
      nextToken: $nextToken
    ) {
      items {
        id
        title
        identifier
        description
        date
        collection_category
        visibility
        parent_collection
        archives {
          nextToken
        }
      }
      nextToken
      total
    }
  }
`;
export const searchArchives = /* GraphQL */ `
  query SearchArchives(
    $filter: SearchableArchiveFilterInput
    $sort: SearchableArchiveSortInput
    $limit: Int
    $nextToken: String
  ) {
    searchArchives(
      filter: $filter
      sort: $sort
      limit: $limit
      nextToken: $nextToken
    ) {
      items {
        id
        title
        identifier
        description
        date
        parent_collection
        item_category
        visibility
        collection {
          id
          title
          identifier
          description
          date
          collection_category
          visibility
          parent_collection
        }
      }
      nextToken
      total
    }
  }
`;



```

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now what if the application requires to search for both Collection and Archive records with sorting capability by some common field such as “title”? This can be achieved by using GraphQL’s &lt;em&gt;interface&lt;/em&gt; or &lt;em&gt;union&lt;/em&gt; type as it represents the generalization of multiple types. Unfortunately, Amplify GraphQL Transform does not have @searchable directive support for either of them. This comes with our problem to solve:&lt;/p&gt;

&lt;h2&gt;Create custom resolvers for Interface/Union type to search across multiple indices&lt;/h2&gt;

&lt;p&gt;To provide this customized search query, we need to add corresponding types into the schema and then attach request/response resolvers to the query. It is much more intuitive to iterate the process of modifying the schema, implementing the resolvers, and testing the query by working directly in the AWS AppSync console.&lt;/p&gt;

&lt;ul&gt;
&lt;ol&gt;Step 1. Modify your schema in AppSync console and click "Save Schema"
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Freknvfb4kqt13fz8uu85.png" alt="modify schema"&gt;

Based on your modeling logic, if you chose &lt;em&gt;interface&lt;/em&gt; type, you might modify your schema like this:
&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;interface Object {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  visibility: Boolean!
  parent_collection: [String!]
}

type Collection implements Object
  # the rest is unchanged
  
type Archive implements Object
  # the rest is unchanged
  
type Query {
  searchObjects(
    sort: SearchableCollectionSortInput
    filter: SearchableCollectionFilterInput
    limit: Int
    nextToken: String
    category: String
  ): SearchableObjectConnection
  # the rest is unchanged
}

type SearchableObjectConnection {
  items: [Object]
  nextToken: String
  total: Int
}&lt;/code&gt;
&lt;/pre&gt; 
 

&lt;p&gt;The &lt;em&gt;union&lt;/em&gt; type follows a similar schema change with the absence of implementing the &lt;em&gt;union&lt;/em&gt; type. Simply create the &lt;em&gt;union&lt;/em&gt; type like this:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;union Object = Collection | Archive&lt;/code&gt;
&lt;/pre&gt;


&lt;/ol&gt;

&lt;ol&gt;Step 2. Attach resolvers to the custom search Query
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frtqxh6nljqv8ghrf30dy.png" alt="attach resolver"&gt;
&lt;/ol&gt;

&lt;ol&gt;Step 3. Choose "ElasticSearchDomain" as data source and write request/response resolvers:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyfuuu7rtcdg3owno43p0.png" alt="data source"&gt;

&lt;p&gt;To provide a starting point, GraphQL Transform auto-generated resolvers (via @searchable in the original schema) can be served as a template for your custom resolvers. They are &lt;em&gt;Query.searchModels.req.vtl&lt;/em&gt; and &lt;em&gt;Query.searchModels.res.vtl&lt;/em&gt;, which can be found at
&lt;em&gt;amplify/backend/api/your_api_name/build/resolvers&lt;/em&gt;.&lt;/p&gt;

When implementing the request resolver, there are certain points worth noting:
&lt;ul&gt;
&lt;li&gt;
Update &lt;em&gt;$indexPath&lt;/em&gt; to perform multi-document search or wildcard search&lt;/li&gt;
&lt;li&gt;The Utility helper &lt;em&gt;$util.transform.toElasticSearchQueryDSL&lt;/em&gt; defaults by &lt;em&gt;AND&lt;/em&gt; operation. If your Query logic involves other complex operations, write your own ElasticSearch Query DSL. For example, the OR operation can be achieved by using the “should” clause and “minimum_should_match” parameter.&lt;/li&gt;
&lt;li&gt;Make sure your query accepts external filter inputs, e.g., by using the "must" clause&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of a request resolver:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;#set( $indexPath = "/*/doc/_search" )
#set( $nonKeywordFields = ["visibility"] )
#if( $util.isNullOrEmpty($context.args.sort) )
  #set( $sortDirection = "desc" )
  #set( $sortField = "id" )
#else
  #set( $sortDirection = $util.defaultIfNull($context.args.sort.direction, "desc") )
  #set( $sortField = $util.defaultIfNull($context.args.sort.field, "id") )
#end
#if( $nonKeywordFields.contains($sortField) )
  #set( $sortField0 = $util.toJson($sortField) )
#else
  #set( $sortField0 = $util.toJson("${sortField}.keyword") )
#end
{
  "version": "2018-05-29",
  "operation":"GET",
  "path":"$indexPath",
  "params":{
    "body":{
      #if( $context.args.nextToken ) "search_after": [$util.toJson($context.args.nextToken)], #end
      "size": #if( $context.args.limit ) $context.args.limit #else 10 #end,
      "sort": [{$sortField0: { "order" : $util.toJson($sortDirection) }}],
      "query": {
        "bool": {
          #if( $context.args.filter ) "must": $util.transform.toElasticsearchQueryDSL($ctx.args.filter), #end
          "filter": {
            "term": {
              "visibility": "true"
            }
          },
          "should": [
            {
              "bool": {
                "must": {
                  "match": {
                    "collection_category": "$context.arguments.category"
                  }
                },
                "must_not": {
                  "exists": {
                    "field": "parent_collection"
                  }
                }
              }
            },
            {
              "bool": {
                "must": {
                  "match": {
                    "item_category": "$context.arguments.category"
                  }
                }
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
  }
}&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;For a response resolver, most GraphQL query results are extracted from the &lt;em&gt;_source&lt;/em&gt; field in ElasticSearch response. However, to be able to identify different model types, &lt;em&gt;__typename&lt;/em&gt; meta field is required for &lt;em&gt;interface/union&lt;/em&gt; type. Thus, make sure to add &lt;em&gt;__typename&lt;/em&gt; into &lt;em&gt;_source&lt;/em&gt; for returned &lt;em&gt;interface/union&lt;/em&gt; type.&lt;/p&gt;

&lt;p&gt;This is an example of a response resolver:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;#set( $es_items = [] )
#foreach( $entry in $context.result.hits.hits )
  #if( !$foreach.hasNext )
    #set( $nextToken = $entry.sort.get(0) )
  #end
  #foreach ( $mapEntry in $entry.entrySet() )
    #if( $mapEntry.key == "_source" )
      #if( $mapEntry.value.get("collection_category") )
        $util.qr( $mapEntry.value.put("__typename", "Collection") )
      #else
        $util.qr( $mapEntry.value.put("__typename", "Archive") )
      #end
    #end
  #end
  $util.qr( $es_items.add($entry.get("_source")) )
#end

$util.toJson({
  "items": $es_items,
  "total": $ctx.result.hits.total,
  "nextToken": $nextToken
})&lt;/code&gt;
&lt;/pre&gt;
 


&lt;/ol&gt;

&lt;ol&gt;Step 4. Test out custom resolvers in AppSync queries
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkwbg3p64lw8o19h1j0zz.png" alt="test query"&gt;
&lt;/ol&gt;

&lt;ol&gt;Step 5. Add resolvers to your API and attach custom resources

&lt;p&gt;After the search query in AppSync works as expected, we need to update the API in Amplify. Running "amplify pull" will not update the API for you. Everything needs to be changed in &lt;em&gt;amplify/backend/api/api_name&lt;/em&gt;; be careful not to touch anything in the &lt;em&gt;amplify/backend/api/api_name/build&lt;/em&gt; directory.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Update &lt;em&gt;schemal.graphql&lt;/em&gt;. Because GraphQL Transform does not support @searchable for &lt;em&gt;interface&lt;/em&gt;, you will need to add some extra &lt;em&gt;input&lt;/em&gt;s in order to make the schema compile successfully.
&lt;pre class="highlight plaintext"&gt;

```



interface Object {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  visibility: Boolean!
  parent_collection: [String!]
}

type Collection implements Object
  @model
  @searchable
  &lt;a class="mentioned-user" href="https://dev.to/key"&gt;@key&lt;/a&gt;(
    fields: ["identifier"]
    name: "Identifier"
    queryField: "collectionByIdentifier"
  ) {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  collection_category: String!
  visibility: Boolean!
  parent_collection: [String!]
  archives: [Archive] @connection(name: "CollectionArchives")
}
type Archive implements Object
  @model
  @searchable
  &lt;a class="mentioned-user" href="https://dev.to/key"&gt;@key&lt;/a&gt;(
    fields: ["identifier"]
    name: "Identifier"
    queryField: "archiveByIdentifier"
  ) {
  id: ID!
  title: String!
  identifier: String!
  description: String
  date: String
  parent_collection: [String!]
  item_category: String!
  visibility: Boolean!
  collection: Collection @connection(name: "CollectionArchives")
}
type Query {
  searchObjects(
    sort: SearchableObjectSortInput
    filter: SearchableObjectFilterInput
    limit: Int
    nextToken: String
    category: String
  ): SearchableObjectConnection
}

type SearchableObjectConnection {
  items: [Object]
  nextToken: String
  total: Int
}

input SearchableObjectFilterInput {
  id: SearchableIDFilterInput
  title: SearchableStringFilterInput
  identifier: SearchableStringFilterInput
  description: SearchableStringFilterInput
  date: SearchableStringFilterInput
  visibility: SearchableBooleanFilterInput
  parent_collection: SearchableStringFilterInput
  and: [SearchableObjectFilterInput]
  or: [SearchableObjectFilterInput]
  not: SearchableObjectFilterInput
}

input SearchableBooleanFilterInput {
  eq: Boolean
  ne: Boolean
}

input SearchableObjectSortInput {
  field: SearchableObjectSortableFields
  direction: SearchableSortDirection
}

enum SearchableObjectSortableFields {
  id
  title
  identifier
  description
  date
}

input SearchableIDFilterInput {
  ne: ID
  gt: ID
  lt: ID
  gte: ID
  lte: ID
  eq: ID
  match: ID
  matchPhrase: ID
  matchPhrasePrefix: ID
  multiMatch: ID
  exists: Boolean
  wildcard: ID
  regexp: ID
}

enum SearchableSortDirection {
  asc
  desc
}

input SearchableStringFilterInput {
  ne: String
  gt: String
  lt: String
  gte: String
  lte: String
  eq: String
  match: String
  matchPhrase: String
  matchPhrasePrefix: String
  multiMatch: String
  exists: Boolean
  wildcard: String
  regexp: String
}



```

&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Copy and paste the resolvers into resolvers directory with file names: &lt;em&gt;Query.searchObjects.req.vtl&lt;/em&gt; and &lt;em&gt;Query.searchObjects.res.vtl&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;Update &lt;em&gt;amplify/backend/api/api_name/stacks/CustomResouces.json&lt;/em&gt; as:
&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;{//Anything before "Resouces" remains the same
  "Resources": {
    "QuerySearchObjectResolver": {
      "Type": "AWS::AppSync::Resolver",
      "Properties": {
        "ApiId": {
          "Ref": "AppSyncApiId"
        },
        "DataSourceName": "ElasticSearchDomain",
        "TypeName": "Query",
        "FieldName": "searchObjects",
        "RequestMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Query.searchObjects.req.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        },
        "ResponseMappingTemplateS3Location": {
          "Fn::Sub": [
            "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Query.searchObjects.res.vtl",
            {
              "S3DeploymentBucket": {
                "Ref": "S3DeploymentBucket"
              },
              "S3DeploymentRootKey": {
                "Ref": "S3DeploymentRootKey"
              }
            }
          ]
        }
      }
    }
  },
//Anything after "Resources" remains the same

```



&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;



&lt;ol&gt;Step 6. Push API to AWS Cloud

&lt;p&gt;To avoid the "Only one resolver is allowed per field" error, make sure to delete the resolvers attached to the custom search query (in AWS AppSync console) before running "amplify push."&lt;/p&gt;

&lt;p&gt;With that, your custom search query is ready to use!&lt;br&gt;
&lt;/p&gt;
&lt;/ol&gt;


&lt;/ul&gt;

</description>
      <category>amplify</category>
      <category>graphql</category>
      <category>elasticsearch</category>
      <category>interface</category>
    </item>
  </channel>
</rss>
