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

apollo-couch

v0.0.15

Published

A framework for building an Apollo GraphQL API backed by Couchbase.

Downloads

6

Readme

Apollo On The Couch

A framework for building GraphQL APIs with the Apollo GraphQL server backed by Couchbase. The main purpose of this framework is to make it simple to build super scalable and reliable APIs quickly and cost-effectively.

Getting Started

Prerequisites

  • A Couchbase Capella account or your own Couchbase server.
  • Node.js
  • npm or similar

Create Your Apollo On The Couch Server Project

Create a Typescript project with the appropriate dependencies.

mkdir my-apollo-couch-server
cd my-apollo-couch-server
npm init -y
tsc --init
npm install apollo-couch couchbase graphql
npm install --save-dev @types/node @graphql-codegen/cli @graphql-codegen/typescript-resolvers eslint nodemon typescript

Create the required files

mkdir src
touch src/index.ts codegen.ts .env

Open your editor and update the src/index.ts file

import { startApolloCouchServer } from "apollo-couch";

startApolloCouchServer();

Update the tsconfig.json file

{
  "compilerOptions": {
      "preserveConstEnums": true,
      "strictNullChecks": true,
      "sourceMap": true,
      "allowJs": true,
      "target": "es5",
      "module": "es6",
      "outDir": ".build",
      "moduleResolution": "node",
      "lib": ["es2015"],
      "rootDir": "./",
      "baseUrl": ".",
      "paths": {
          "*": ["*", "src/*"]
      },
      "allowSyntheticDefaultImports": true,
      "skipLibCheck": true
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "include": ["src/**/*", "scripts/generate-schema.ts", "apollo-couch/src/data", "apollo-couch/src/graphql/lib", "apollo-couch/src/couchbase"]
}

Update the codegen.ts file

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  overwrite: true,
  schema: [
    "src/graphql/**/*.graphql", 
    "node_modules/apollo-couch/src/graphql/**/*.graphql"
  ],
  generates: {
    "src/graphql/generated-types.ts": {
      plugins: ["typescript", "typescript-resolvers"],
      config: {
        useIndexSignature: true
      },
    }, 
  }
};

export default config;

Update the package.json file

{  
  ...
  "type": "module",
  "scripts": {
    "init": "npm run generate-graphql-types",
    "dev": "nodemon -r dotenv/config src/index.ts",
    "generate-graphql-types": "graphql-codegen --config codegen.ts",
    "generate-resource": "generate-resource",
    "postgenerate-resource": "npm run generate-graphql-types"
  },
}

Configure the Couchbase Environment Variables in the .env file

Remember to add the .env file to your .gitignore file to avoid accidentally committing sensitive information.

Capella database
PORT=4000
LOCAL=true

COUCHBASE_USER=username
COUCHBASE_PASSWORD=password
COUCHBASE_ENDPOINT=couchbases://cb.yourendpoint.cloud.couchbase.com
COUCHBASE_DEFAULT_BUCKET=_default
COUCHBASE_DEFAULT_SCOPE=_default
COUCHBASE_IS_CLOUD_INSTANCE=true
Localhost database
PORT=4000
LOCAL=true

COUCHBASE_USER=username
COUCHBASE_PASSWORD=password
COUCHBASE_ENDPOINT=couchbase://localhost
COUCHBASE_DEFAULT_BUCKET=_default
COUCHBASE_DEFAULT_SCOPE=_default
COUCHBASE_IS_CLOUD_INSTANCE=false

Generate an apollo-couch resource

In apollo-couch, the GraphQL schema and resolvers are structured in what's called resources. These resources will typically be very similar to REST resources, with CrUD operations.

You can use a scaffolding script, generate-resource, to generate a new resource. This script will generate resources with scaffold resolvers and schema files that you can edit to fit your purposes.

You also have the flexibility to create your own resources that can contain any type of API resolvers and schema.

To create a resource using the scaffolding script, follow these steps:

Run the generate-resource script:

npm run generate-resource <resourceNameInPlural>

Edit the ./src/graphql/resources/<resourceNameInPlural>/schema.graphql file. Fill in the properties you want to expose on the resource.

E.g.

type AccountContent {
    name: String!
    phone: String
}

input AccountContentInput {
  name: String!
  phone: String
}

input AccountContentPatchInput {
  name: String
  phone: String
}

input AccountsListFiltersInput {
  name: String
}

Notice that there is no exclamation mark in the AccountContentPatchInput input, since you probably don't want to require any field to be included when patching records.

Run the generate-graphql-types script:

npm run generate-graphql-types

Start the server

npm run dev

License

This project is licensed under the ISC License. See the LICENSE file for details.