{"id":20218263,"url":"https://github.com/cannercms/graphql-i18n","last_synced_at":"2025-04-10T15:46:06.403Z","repository":{"id":144737502,"uuid":"159144258","full_name":"CannerCMS/graphql-i18n","owner":"CannerCMS","description":"An i18n middleware for GraphQL","archived":false,"fork":false,"pushed_at":"2018-11-28T11:11:08.000Z","size":89,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T13:36:27.055Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CannerCMS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-11-26T09:33:40.000Z","updated_at":"2023-07-08T14:40:18.000Z","dependencies_parsed_at":"2023-05-04T22:27:24.513Z","dependency_job_id":null,"html_url":"https://github.com/CannerCMS/graphql-i18n","commit_stats":null,"previous_names":["cannercms/graphql-i18n"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-i18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-i18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-i18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-i18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CannerCMS","download_url":"https://codeload.github.com/CannerCMS/graphql-i18n/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248243512,"owners_count":21071055,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T06:37:47.668Z","updated_at":"2025-04-10T15:46:06.376Z","avatar_url":"https://github.com/CannerCMS.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL i18n\n\n[![CircleCI](https://circleci.com/gh/Canner/graphql-i18n/tree/master.svg?style=shield)](https://circleci.com/gh/Canner/graphql-i18n/tree/master)\n[![npm version](https://badge.fury.io/js/graphql-i18n.svg)](https://badge.fury.io/js/graphql-i18n)\n\n\u003e Internationalization and localization solution in GraphQL.\n\n## Features\n\n* Flexible: Multiple storage adapters can be chosen to store i18n.\n* Easily use: Add a simple middleware, and your service is ready for i18n.\n\n## Installation\n\n```sh\nyarn add graphql-i18n\n```\n\n## Example\n\nSetup GraphQL Server\n\n```typescript\nimport { GraphQLServer } from 'graphql-yoga'\nimport { I18n } from 'graphql-i18n';\nimport { MemoryAdapter } from 'graphql-i18n/adapter';\n\nconst typeConfig = {\n  Book: {\n    idFromObject: (object: any) =\u003e object.id, // get id from object\n    fields: ['name', 'author.name'], // i18n fields\n},\n};\n\nconst resolverConfig = {\n  Query: {\n    book: {\n      dataType: 'Book' // target type for i18n\n    },\n    books: {\n      dataType: 'Book'\n    }\n  }\n};\n\nconst adapter = MemoryAdapter(typeConfig);\n\nconst i18n = new I18n({ adapter, typeConfig, resolverConfig, defaultLang: 'en' });\n\nconst where = { type: 'Book', id: '1' };\nconst dataEn = { name: 'test', author: { name: 'testAuthor' } };\nconst dataZh = { name: '測試', author: { name: '測試作者' } };\ni18n.sync(where, dataEn, 'en');\ni18n.sync(where, dataZh, 'zh');\n\nconst typeDefs = `\n  directive @locale (\n    lang: String!\n  ) on FIELD\n\n  type Query {\n    book: Book!\n    books: [Book!]!\n  }\n\n  type Book {\n    id: ID!\n    name: String!\n    author: BookAuthor!\n  }\n\n  type BookAuthor {\n    name: String!\n  }\n`;\n\nconst resolvers = {\n    Query: {\n        book: () =\u003e ({ id: '1', ...dataEn }),\n        books: () =\u003e ([{ id: '1', ...dataEn }]),\n    }\n};\n\nconst server = new GraphQLServer({\n    typeDefs,\n    resolvers,\n    middlewares: [i18n.middleware()]\n})\n\nserver.start(() =\u003e console.log('Server is running on http://localhost:4000'))\n```\n\nNow, you can get i18n by add directive `locale` in your Query.\n\n```GraphQL\nquery {\n  book @locale(lang: \"en\") {\n    name\n    author {\n      name\n    }\n  }\n  books @locale(lang: \"zh\") {\n    name\n    author {\n      name\n    }\n  }\n}\n```\n\n[![Edit graphql-i18n demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/pkw76vxm6x)\n\n## API\n\n### Types\n\n```typescript\nexport interface IWhereUnique {\n  id: string;\n  type: string;\n}\n\nexport interface IWhere {\n  ids: string[];\n  type: string;\n}\n\nexport interface II18n {\n  sync(where: IWhereUnique, data: Record\u003cstring, string\u003e, language: string): Promise\u003cvoid\u003e;\n  find(where: IWhere, language: string): Promise\u003cRecord\u003cstring, any\u003e\u003e;\n  findOne(where: IWhereUnique, language: string): Promise\u003cany\u003e;\n  destroy(where: IWhereUnique, language: string): Promise\u003cvoid\u003e;\n  middleware(): IMiddleware;\n}\n\nexport interface ITypeConfig {\n  [key: string]: {\n    idFromObject: (object: any) =\u003e string;\n    fields: string[];\n  };\n}\n\nexport interface IResolverConfig {\n  Query: {\n    [key: string]: {\n      dataType: string;\n      dataPath?: string;\n    },\n  };\n}\n\nexport interface IAdapter {\n  create(where: IWhereUnique, data: Record\u003cstring, any\u003e, language: string): Promise\u003cany\u003e;\n  update(where: IWhereUnique, data: Record\u003cstring, any\u003e, language: string): Promise\u003cany\u003e;\n  find(where: IWhere, language: string): Promise\u003cRecord\u003cstring, any\u003e\u003e;\n  findOne(where: IWhereUnique, language: string): Promise\u003cnull | Record\u003cstring, any\u003e\u003e;\n  destroy(where: IWhereUnique, language: string): Promise\u003cvoid\u003e;\n}\n\nexport interface II18nConstructorOptions {\n  adapter: IAdapter;\n  typeConfig: ITypeConfig;\n  resolverConfig: IResolverConfig;\n  defaultLang: string;\n}\n```\n\n### `i18n.sync(where, data, language)`\n\nCreate or update i18n data.\n\n### `i18n.find(where, language)`\n\nFind list of i18n data. If `language` is not existent, graphql-i18n will find default language.\n\n### `i18n.findOne(where, language)`\n\nFind one of i18n data. If `language` is not existent, graphql-i18n will find default language.\n\n### `i18n.destroy(where, language)`\n\nDestroy one of i18n data.\n\n## Adapters\n\n### Memory Adapter\n\nStore i18n data in memory. Data will be lost after closing server.\n\n```typescript\nimport { MemoryAdapter } from 'graphql-i18n/adapter';\nconst memoryAdapter = new MemoryAdapter(\n  // type config\n);\n```\n\n### MongoDB Adapter\n\nStore i18n data in MongoDB.\n\n```typescript\nimport { MongodbAdapter } from 'graphql-i18n/adapter';\nconst mongodbAdapter = new MongodbAdapter(\n  // mongodb uri\n);\n```\n\n### Firebase Adapter (coming soon)\n\nStore i18n data in Firebase.\n\n## License\n\nApache-2.0\n\n![footer banner](https://user-images.githubusercontent.com/26116324/37811196-a437d930-2e93-11e8-97d8-0653ace2a46d.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcannercms%2Fgraphql-i18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcannercms%2Fgraphql-i18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcannercms%2Fgraphql-i18n/lists"}