@last-rev/graphql-algolia-integration
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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"
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @last-rev/graphql-algolia-integration

Weekly Downloads

206

Version

0.2.0

License

ISC

Unpacked Size

63.2 kB

Total Files

62

Last publish

Collaborators

  • maxtechera.lastrev
  • tharris
  • justinlastrev
  • jaimelastrev
  • bradtaylorsf