0.1.8 • Published 2 years ago

graphql-compose-algolia v0.1.8

Weekly downloads
58
License
MIT
Repository
github
Last release
2 years ago

graphql-compose-algolia

A plugin for graphql-compose that enriches your existing type composers with middlewares and resolvers to keep your data in sync with Algolia indicies.

This library is currently WIP. So far I've only been using it in tandem with graphql-compose-mongoose as in the examples below, so the library may be buggy, or totally broken in other environments until we can push updates.

GraphQL Compose Example:

This example uses graphql-compose-mongoose and the withMiddlewares functionality of graphql-compose resolvers.

import { Schema } from 'mongoose';
import { SchemaComposer } from 'graphql-compose';
import { composeAlgolia } from 'graphql-compose-algolia';

/** Instantiate the schema composer */
const schemaComposer = new SchemaComposer();

/** Define the Schema and create the model for our Users in MongoDB. */
const UserSchema = new Schema({
 name: {
  type: 'String',
  required: true,
 },
 email: {
  type: 'String',
  required: true,
 },
 organization: {
  type: Schema.Types.ObjectId,
  ref: 'Organization',
  required: true,
 },
}, { collection: 'users' });

const UserModel = mongoose.model('User', UserSchema);

/** Configure composeAlgolia */
const composeAlgoliaOpts = {
 indexName: 'USERS',
 fields: ['name', 'email', 'organization'],
 schemaComposer,
 appId: process.env.ALGOLIA_ID,
 apiKey: process.env.ALGOLIA_KEY,
};

/** Smash it all together */
const UserTC = composeAlgolia(
 composeMongoose(UserModel, { schemaComposer }),
 composeAlgoliaOpts,
);

/** Add some resolvers to your schema */
schemaComposer.addFields({
 Query: {
  userById: UserTC.mongooseResolvers.findById(),
  userSearch: UserTC.algoliaResolvers.search(),
 },
 Mutation: {
  userCreate: UserTC.mongooseResolvers.createOne().addMiddlewares([UserTC.algoliaMiddlewares.sync]),
  userUpdate: UserTC.mongooseResolvers.updateById().addMiddlewares([UserTC.algoliaMiddlewares.sync]),
  userRemove: UserTC.mongooseResolvers.removeById().addMiddlewares([UserTC.algoliaMiddlewares.remove]),
 }
});
0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago