npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-algolia-transformer

v2.1.0

Published

Amplify GraphQL @algolia transformer

Downloads

27

Readme

graphql-algolia-transformer

Pull requests are welcome! npm GitHub license Code Coverage

Description

Add Serverless search to your Amplify API with Aloglia using this transformer.

@algolia vs @searchable

I made this transformer because I didn't want the monthly costs for the Elasticsearch instances used by @searchable. Algolia is effortless to get started and is free for up to 10k records and searches per month which makes it perfect for MVPs. As your app grows, you should probably re-evaluate the pricing difference between Elasticsearch and Algolia but this point is probably when you reach ~500k records/searches. Also, Algolia comes with nice client-side search UIs that you can just drop into your app. An obvious downside to using Algolia instead of Elasticsearch is that it takes you outside of the AWS world but I think that it's worth the tradeoff.

Use

Install Transform

npm install graphql-algolia-transformer

Import Transform

/amplify/backend/api/<API_NAME>/transform.conf.json

{
    . . .
    "transformers": [
        . . .,
        "graphql-algolia-transformer"
    ]
}

Use @algolia directive

Append to target models

@algolia(
  fields?: {
    include?: [string],
    exclude?: [string]
  }, 
  settings: {
    forwardToReplicas?: Boolean, 
    requestOptions?: AWSJSON, 
    settings: AWSJSON,
  }
)
type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @hasMany
}

type Post @model @algolia(fields:{include:["title"]}) {
  id: ID!
  title: String!
  blog: Blog @belongsTo
  content: String
  comments: [Comment] @hasMany
}

type Comment @model @algolia{
  id: ID!
  post: Post @belongsTo
  content: String!
}
  • You cannot specify include and exclude in the same fields parameter.
  • Declare Algolia settings https://www.algolia.com/doc/api-reference/api-methods/set-settings/#parameters
  • Double check your Algolia settings (if specified) because they arent's validated until they are used in the Lambda function and can lead to a tricky problem to track down. If you create an entry on a model with the @algolia directive and no index is made in Algolia, check the Lambda function logs.
  • The Algolia ObjectID is a concatenation of the DynamoDB keys for the object; PrimaryKey(:SortKey).
  • Automatically creates an index. The index name will match your DynamoDB table name. (e.g. post-ahdoegz2xvaibaim2lopa7jv4a-dev)

Configure Project ID & API Keys

/amplify/backend/api/<API_NAME>/parameters.json

{
  . . .
  "AlgoliaAppId": "APPID",
  "AlgoliaApiKey": "APIKEY",
}

This is your the Algolia App ID and API Key that will be used by the transformer. You can find these in your Algolia dashboard.

Push Changes

amplify push

Query

For querying the search indexes, use an Algolia search client.

Or you can use graqhQL to query the indexed items. For example, if you have a Post model with an @algolia directive, you can query the index like this:

query MyQuery {
  searchPosts(query: "abc")
}

This searchPost query is generated by the transformer. It will return a JSON object with the Algolia search results.

Example

Check out the schema for the searchable blog example.

How it works

This directive creates an individual Lambda function for each GraphQL Api in your Amplify project and attaches DynamoDB streams from the respective tables to the function. On receiving a stream, the function filters the fields as specified, formats the record into an Algolia payload and updates the Algolia index with the model name (if it doesn't exist, it creates it).

Legacy Amplify Transformer V1

You can find legacy documentation for the Amplify Transformer V1 in the README. To use this package with the v1 transformer, you must install version 1.6.0. Read more about the difference between v1 and v2 transformers here

Contribute

Contributions are more than welcome!

Please feel free to create, comment and of course solve some of the issues. To get started you can also go for the easier issues marked with the good first issue label if you like.

Development

Algolia Lambda

  • npm run lambda uses SAM to invoke the Lambda function. You need to supply an App ID and API Key in template.yaml.
  • Modify the event you are sending in events/event.json.

Transformer

  • Initialize an amplify project and add an API
  • Import the transformer with the absolute path
// amplify/backend/api/<API_NAME>/transform.conf.json
{
    ...
    "transformers": [
        "file:///absolute/path/to/graphql-algolia-transform/"
    ]
}
  • Rebuild the transformer with npm run build.
  • amplify api gql-compile lets you check the stack outputs without having to go through the lengthy push process.
  • Check amplify/backend/api/<API>/build/stacks/AlgoliaStack for expected outputs.

License

The MIT License

Credits

The graphql-algolia-transformer library is maintained by

Based on amplify-graphql-searchable-transformer