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

@last-rev/graphql-algolia-integration

v0.4.1

Published

This package is a set of extensions and functions that will assist with implementing algolia indexing for a Last Rev site.

Downloads

530

Readme

Last Rev Algolia Integration

This package is a set of extensions and functions that will assist with implementing algolia indexing for a Last Rev site.

Exports

  • typeDefs - A set of type definitions for use in the graphql schema.
  • createAlgoliaSyncHandler - A function that takes in a configuration object, and a graphQl URL, and outputs a content sync service that will be triggered on every content change (through a Contentful Webhook).
  • constructObjectId - A utility function takes a contentful entry, apollo context, and an optional array of additional strings, and outputs an string that can be used for the algolia objectID.

Binaries

Installing this package exposes the lr-algolia-updater binary, which can trigger the algolia webhook handler in your app to prime the indexes.

Configuration

Configuring the Algolia integration consists of two steps:

  1. Add Algolia configuration to your site's AppConfig
    • applicationId (String) - The Algolia application ID.
    • adminApiKey (String) - The Algolia admin API key.
    • contentTypeIds (String[]) - An array of Contentful content type IDs representing the types to index.
  2. Create mappers to map your content types to Algolia fields.

Implementing the Algolia Integration

  1. Add @last-rev/graphql-algolia-integration as a dependency to the functions and graphql-extensions projects.
  2. In your project's graphql-extensions package, import the algolia integration typeDefs and merge them with your other typeDefs.
import { typeDefs } from '@last-rev/graphql-algolia-integration';

const extensions: GraphQlExtension[] = [
  //... the rest of your extensions
  Algolia : { typeDefs }
];
  1. In your project's graphql-extensions package, create and export your mappers to map specific content types to AlgoliaRecord objects.
import { ApolloContext } from '@last-rev/graphql-types';
import { getLocalizedField } from '@last-rev/graphql-contentful-core';

const mappers = {
  Blog: {
    AlgoliaRecord: async (item: any, _: never, ctx: ApolloContext) => {
      return [
        {
          index: 'blogs',
          data: {
            objectID: blog.sys.id,
            locale: ctx.locale || ctx.defaultLocale,
            site: process.env.SITE,
            preview: !!ctx.preview,
            url: getContentUrl(item, ctx); // function defined elsewhere in project
            title: getLocalizedField(blog.fields, 'title', ctx)
          }
        }
      ];
    }
  }
};
  1. Add environment variables to your .env file:
ALGOLIA_APPLICATION_ID={app_id}
ALGOLIA_ADMIN_API_KEY={admin_api_key}
ALGOLIA_INDEX_DRAFT_CONTENT={true|false}
  1. Add algolia config to your shared config.js in the functions and graphql-runner packages.
require('dotenv').config();

const LastRevAppConfig = require('@last-rev/app-config');
//... other code

const config = new LastRevAppConfig({
  //... other config
  algolia: {
    applicationId: process.env.ALGOLIA_APPLICATION_ID,
    adminApiKey: process.env.ALGOLIA_ADMIN_API_KEY,
    contentTypeIds: ['blog', 'article'],
    indexDraftContent: parseBooleanEnvVar(process.env.ALGOLIA_INDEX_DRAFT_CONTENT)
  }
});

module.exports = config;
  1. In your project's functions package, add a new function, called algolia-background and configure the contentSync service.
require('dotenv').config();

const { createAlgoliaSyncHandler } = require('@last-rev/graphql-algolia-integration');
const config = require('../../shared/config');

const URL = process.env.NETLIFY
  ? 'http://localhost:5000/graphql'
  : process.env.GRAPHQL_SERVER_URL ?? 'http://localhost:5000/graphql';

// This is helpful for testing and not going over limits
const maxRecords = process.env.ALGOLIA_MAX_RECORDS ? parseInt(process.env.ALGOLIA_MAX_RECORDS) : undefined;

module.exports.handler = createAlgoliaSyncHandler(config, URL, maxRecords);
  1. (optional) Import @last-rev/graphql-algolia-integration in your project's monorepo root and add update scripts to package.json.

package.json:

{
  "scripts": {
    "algolia:update": "lr-algolia-updater http://my-site.com/.netlify/functions/algolia",
    "algolia:update:local": "lr-algolia-updater http://localhost:8888/.netlify/functions/algolia"
  }
}