{"id":13566924,"url":"https://github.com/graphql-compose/graphql-compose-elasticsearch","last_synced_at":"2025-05-07T04:27:44.719Z","repository":{"id":18406548,"uuid":"83980444","full_name":"graphql-compose/graphql-compose-elasticsearch","owner":"graphql-compose","description":"Hide Elastic Search REST API behind GraphQL.","archived":false,"fork":false,"pushed_at":"2024-01-31T17:46:58.000Z","size":2420,"stargazers_count":584,"open_issues_count":46,"forks_count":77,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-05T12:18:45.452Z","etag":null,"topics":["elastic","elasticsearch","graphql","graphql-compose"],"latest_commit_sha":null,"homepage":"https://graphql-compose.herokuapp.com/elasticsearch/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphql-compose.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["nodkz"],"patreon":null,"open_collective":"graphql-compose","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-03-05T15:26:11.000Z","updated_at":"2025-02-13T19:38:43.000Z","dependencies_parsed_at":"2023-02-18T03:15:27.157Z","dependency_job_id":"973a64df-069e-4430-8811-5f756a34e8b8","html_url":"https://github.com/graphql-compose/graphql-compose-elasticsearch","commit_stats":null,"previous_names":["nodkz/graphql-compose-elasticsearch"],"tags_count":70,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-elasticsearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-elasticsearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-elasticsearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-elasticsearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-compose","download_url":"https://codeload.github.com/graphql-compose/graphql-compose-elasticsearch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252495277,"owners_count":21757229,"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":["elastic","elasticsearch","graphql","graphql-compose"],"created_at":"2024-08-01T13:02:19.696Z","updated_at":"2025-05-07T04:27:44.699Z","avatar_url":"https://github.com/graphql-compose.png","language":"TypeScript","funding_links":["https://github.com/sponsors/nodkz","https://opencollective.com/graphql-compose"],"categories":["TypeScript"],"sub_categories":[],"readme":"# graphql-compose-elasticsearch\n\n[![npm](https://img.shields.io/npm/v/graphql-compose-elasticsearch.svg)](https://www.npmjs.com/package/graphql-compose-elasticsearch)\n[![trends](https://img.shields.io/npm/dt/graphql-compose-elasticsearch.svg)](http://www.npmtrends.com/graphql-compose-elasticsearch)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nThis module expose Elastic Search REST API via GraphQL.\n\n## Elastic Search REST API proxy\n\nSupported all elastic versions that support official [elasticsearch-js](https://github.com/elastic/elasticsearch-js) client. Internally it parses its source code annotations and generates all available methods with params and descriptions to GraphQL Field Config Map. You may put this config map to any GraphQL Schema.\n\n```js\nimport { GraphQLSchema, GraphQLObjectType } from 'graphql';\nimport elasticsearch from 'elasticsearch';\nimport { elasticApiFieldConfig } from 'graphql-compose-elasticsearch';\n\nconst schema = new GraphQLSchema({\n  query: new GraphQLObjectType({\n    name: 'Query',\n    fields: {\n      elastic50: elasticApiFieldConfig(\n        // you may provide existed Elastic Client instance\n        new elasticsearch.Client({\n          host: 'http://localhost:9200',\n          apiVersion: '5.0',\n        })\n      ),\n\n      // or may provide just config\n      elastic24: elasticApiFieldConfig({\n        host: 'http://user:pass@localhost:9200',\n        apiVersion: '2.4',\n      }),\n\n      elastic17: elasticApiFieldConfig({\n        host: 'http://user:pass@localhost:9200',\n        apiVersion: '1.7',\n      }),\n    },\n  }),\n});\n```\n\nFull [code example](https://github.com/graphql-compose/graphql-compose-elasticsearch/tree/master/examples/differentVersions)\n\nLive demo of [Introspection of Elasticsearch API via Graphiql](https://graphql-compose.herokuapp.com/elasticsearch/)\n\n---\n\n## ObjectTypeComposer from Elastic mapping\n\nIn other side this module is a plugin for [graphql-compose](https://github.com/graphql-compose/graphql-compose), which derives GraphQLType from your [elastic mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) generates tons of types, provides all available methods in QueryDSL, Aggregations, Sorting with field autocompletion according to types in your mapping (like Dev Tools Console in Kibana).\n\nGenerated ObjectTypeComposer model has several awesome resolvers:\n\n- `search` - greatly simplified elastic `search` method. According to GraphQL adaptation and its projection bunch of params setup automatically due your graphql query (eg `_source`, `explain`, `version`, `trackScores`), other rare fine tuning params moved to `opts` input field.  \n- `searchConnection` - elastic `search` method that implements Relay Cursor Connection [spec](https://facebook.github.io/relay/graphql/connections.htm) for infinite lists. Internally it uses cheap [search_after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html) API. One downside, Elastic does not support backward scrolling, so `before` argument will not work.\n- `searchPagination` - elastic `search` method that has `page` and `perPage` arguments\n- `findById` - get elastic record by id\n- `updateById` - update elastic record by id\n- feel free to add your resolver or ask for a new one\n\n```js\nimport { GraphQLSchema, GraphQLObjectType } from 'graphql';\nimport elasticsearch from 'elasticsearch';\nimport { composeWithElastic } from 'graphql-compose-elasticsearch';\n\nconst mapping = {\n  properties: {\n    name: {\n      type: 'text',\n      fields: {\n        keyword: {\n          type: 'keyword',\n        },\n      },\n    },\n    gender: {\n      type: 'keyword',\n    },\n    skills: {\n      type: 'text',\n    },\n    languages: {\n      type: 'keyword',\n    },\n    location: {\n      properties: {\n        name: {\n          type: 'text',\n        },\n        point: {\n          type: 'geo_point',\n        },\n      },\n    },\n    createdAt: {\n      type: 'date',\n    },\n  },\n};\n\nconst UserTC = composeWithElastic({\n  graphqlTypeName: 'UserES',\n  elasticIndex: 'user',\n  elasticType: 'user',\n  elasticMapping: mapping,\n  elasticClient: new elasticsearch.Client({\n    host: 'http://localhost:9200',\n    apiVersion: '5.0',\n    log: 'trace',\n  }),\n  // elastic mapping does not contain information about is fields are arrays or not\n  // so provide this information explicitly for obtaining correct types in GraphQL\n  pluralFields: ['skills', 'languages'],\n});\n\nconst Schema = new GraphQLSchema({\n  query: new GraphQLObjectType({\n    name: 'Query',\n    fields: {\n      user: UserTC.getResolver('search').getFieldConfig(),\n      userPagination: UserTC.getResolver('searchPagination').getFieldConfig(),\n      userConnection: UserTC.getResolver('searchConnection').getFieldConfig(),\n    },\n  }),\n});\n```\n\nFull [code example](https://github.com/graphql-compose/graphql-compose-elasticsearch/blob/master/examples/elastic50/index.js)\n\n## Installation\n\n```bash\nyarn add graphql graphql-compose elasticsearch graphql-compose-elasticsearch\n// or\nnpm install graphql graphql-compose elasticsearch graphql-compose-elasticsearch --save\n```\n\nModules `graphql`, `graphql-compose`, `elasticsearch` are in `peerDependencies`, so should be installed explicitly in your app.\n\n## Screenshots\n\n### API proxy: Raw search method\n\n\u003cimg width=\"1316\" alt=\"screen shot 2017-03-07 at 22 26 17\" src=\"https://cloud.githubusercontent.com/assets/1946920/23859886/61066f40-082f-11e7-89d0-8443aa2ae930.png\"\u003e\n\n### API proxy: Getting several raw elastic metric in one request\n\n\u003cimg width=\"1314\" alt=\"screen shot 2017-03-07 at 22 34 01\" src=\"https://cloud.githubusercontent.com/assets/1946920/23859892/65e71744-082f-11e7-8c1a-cafeb87e08e6.png\"\u003e\n\n### Mapping: Relay Cursor Connection\n\n\u003cimg width=\"1411\" alt=\"screen shot 2017-03-22 at 19 34 09\" src=\"https://cloud.githubusercontent.com/assets/1946920/24200219/a058c220-0f36-11e7-9cf1-38394052f922.png\"\u003e\n\n### Mapping: Generated GraphQL Types and Documentation\n\n\u003cimg width=\"1703\" alt=\"screen shot 2017-03-22 at 19 33 24\" src=\"https://cloud.githubusercontent.com/assets/1946920/24200220/a05944b6-0f36-11e7-9919-39b7001af203.png\"\u003e\n\n## FAQ\n\n### Creating custom Resolvers\n\nIf you need create something special, you may create a custom Resolver. For example, if you need to add a new tag for existing record, do it in the following manner ([see full test-case](https://github.com/graphql-compose/graphql-compose-elasticsearch/blob/master/src/__tests__/github_issues/37-test.js)):\n\n```js\nActivitiesEsTC.addResolver({\n  name: 'addTag',\n  kind: 'mutation',\n  type: 'JSON',\n  args: {\n    id: 'String!',\n    tag: 'String!',\n  },\n  resolve: ({ args }) =\u003e {\n    return elasticClient.update({\n      index: elasticIndex,\n      type: elasticType,\n      id: args.id,\n      body: {\n        script: {\n          inline: 'ctx._source.tags.add(params.tag)',\n          params: { tag: args.tag },\n        },\n      },\n    });\n  },\n});\n```\n\n## License\n\n[MIT](https://github.com/graphql-compose/graphql-compose-elasticsearch/blob/master/LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-elasticsearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-elasticsearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-elasticsearch/lists"}