Skip to content

lawmicha/apiSearchable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to create a searchable API query to your AppSync service

Pre-req

This uses the schema from schema.graphql so you can go straight ahead into

  1. amplify init

  2. amplify add api and choose GraphQL, and use schema.graphql

  3. amplify push

  4. cd apiSearchableApp and run pod install

  5. xed to open the app

Searchable query

The schema contains a model with @searchable annotated

type Blog6 @model @searchable {
  id: ID!
  name: String!
  posts: [Post6] @connection(keyName: "byBlog", fields: ["id"])
}

Run amplify console api and navigate to the Queries tab, and determine the search query you want to perform and its parameters.

This sample provides an extension that makes it easy to map to a AppSync search query.

func searchBlogs() {
    let filter: [String: Any] = [
        "name": [
            "matchPhrase": "first"
        ]
    ]
    Amplify.API.query(request: .search(Blog6.self,
                                        filter: filter,
                                        limit: 1000,
                                        sort: QuerySortBy.ascending(Blog6.keys.id))) { event in
        switch event {
        case .success(let result):
            switch result {
            case .success(let blogs):
                print("Successfully searched blogs: \(blogs)")
            case .failure(let error):
                print("Got failed result with \(error.errorDescription)")
            }
        case .failure(let error):
            print("Got failed event with error \(error)")
        }
    }
}

Extension https://gist.github.com/lawmicha/d8a9453a3dfdc976f4cfa976a9c4eb30

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published