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 🙏

© 2026 – Pkg Stats / Ryan Hefner

graphql-i18n

v1.1.0

Published

[![CircleCI](https://circleci.com/gh/Canner/graphql-i18n/tree/master.svg?style=shield)](https://circleci.com/gh/Canner/graphql-i18n/tree/master) [![npm version](https://badge.fury.io/js/graphql-i18n.svg)](https://badge.fury.io/js/graphql-i18n)

Downloads

14

Readme

GraphQL i18n

CircleCI npm version

Internationalization and localization solution in GraphQL.

Features

  • Flexible: Multiple storage adapters can be chosen to store i18n.
  • Easily use: Add a simple middleware, and your service is ready for i18n.

Installation

yarn add graphql-i18n

Example

Setup GraphQL Server

import { GraphQLServer } from 'graphql-yoga'
import { I18n } from 'graphql-i18n';
import { MemoryAdapter } from 'graphql-i18n/adapter';

const typeConfig = {
  Book: {
    idFromObject: (object: any) => object.id, // get id from object
    fields: ['name', 'author.name'], // i18n fields
},
};

const resolverConfig = {
  Query: {
    book: {
      dataType: 'Book' // target type for i18n
    },
    books: {
      dataType: 'Book'
    }
  }
};

const adapter = MemoryAdapter(typeConfig);

const i18n = new I18n({ adapter, typeConfig, resolverConfig, defaultLang: 'en' });

const where = { type: 'Book', id: '1' };
const dataEn = { name: 'test', author: { name: 'testAuthor' } };
const dataZh = { name: '測試', author: { name: '測試作者' } };
i18n.sync(where, dataEn, 'en');
i18n.sync(where, dataZh, 'zh');

const typeDefs = `
  directive @locale (
    lang: String!
  ) on FIELD

  type Query {
    book: Book!
    books: [Book!]!
  }

  type Book {
    id: ID!
    name: String!
    author: BookAuthor!
  }

  type BookAuthor {
    name: String!
  }
`;

const resolvers = {
    Query: {
        book: () => ({ id: '1', ...dataEn }),
        books: () => ([{ id: '1', ...dataEn }]),
    }
};

const server = new GraphQLServer({
    typeDefs,
    resolvers,
    middlewares: [i18n.middleware()]
})

server.start(() => console.log('Server is running on http://localhost:4000'))

Now, you can get i18n by add directive locale in your Query.

query {
  book @locale(lang: "en") {
    name
    author {
      name
    }
  }
  books @locale(lang: "zh") {
    name
    author {
      name
    }
  }
}

Edit graphql-i18n demo

API

Types

export interface IWhereUnique {
  id: string;
  type: string;
}

export interface IWhere {
  ids: string[];
  type: string;
}

export interface II18n {
  sync(where: IWhereUnique, data: Record<string, string>, language: string): Promise<void>;
  find(where: IWhere, language: string): Promise<Record<string, any>>;
  findOne(where: IWhereUnique, language: string): Promise<any>;
  destroy(where: IWhereUnique, language: string): Promise<void>;
  middleware(): IMiddleware;
}

export interface ITypeConfig {
  [key: string]: {
    idFromObject: (object: any) => string;
    fields: string[];
  };
}

export interface IResolverConfig {
  Query: {
    [key: string]: {
      dataType: string;
      dataPath?: string;
    },
  };
}

export interface IAdapter {
  create(where: IWhereUnique, data: Record<string, any>, language: string): Promise<any>;
  update(where: IWhereUnique, data: Record<string, any>, language: string): Promise<any>;
  find(where: IWhere, language: string): Promise<Record<string, any>>;
  findOne(where: IWhereUnique, language: string): Promise<null | Record<string, any>>;
  destroy(where: IWhereUnique, language: string): Promise<void>;
}

export interface II18nConstructorOptions {
  adapter: IAdapter;
  typeConfig: ITypeConfig;
  resolverConfig: IResolverConfig;
  defaultLang: string;
}

i18n.sync(where, data, language)

Create or update i18n data.

i18n.find(where, language)

Find list of i18n data. If language is not existent, graphql-i18n will find default language.

i18n.findOne(where, language)

Find one of i18n data. If language is not existent, graphql-i18n will find default language.

i18n.destroy(where, language)

Destroy one of i18n data.

Adapters

Memory Adapter

Store i18n data in memory. Data will be lost after closing server.

import { MemoryAdapter } from 'graphql-i18n/adapter';
const memoryAdapter = new MemoryAdapter(
  // type config
);

MongoDB Adapter

Store i18n data in MongoDB.

import { MongodbAdapter } from 'graphql-i18n/adapter';
const mongodbAdapter = new MongodbAdapter(
  // mongodb uri
);

Firebase Adapter (coming soon)

Store i18n data in Firebase.

License

Apache-2.0

footer banner