Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork77
Hide Elastic Search REST API behind GraphQL.
License
graphql-compose/graphql-compose-elasticsearch
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This module expose Elastic Search REST API via GraphQL.
Supported all elastic versions that support officialelasticsearch-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.
import{GraphQLSchema,GraphQLObjectType}from'graphql';importelasticsearchfrom'elasticsearch';import{elasticApiFieldConfig}from'graphql-compose-elasticsearch';constschema=newGraphQLSchema({query:newGraphQLObjectType({name:'Query',fields:{elastic50:elasticApiFieldConfig(// you may provide existed Elastic Client instancenewelasticsearch.Client({host:'http://localhost:9200',apiVersion:'5.0',})),// or may provide just configelastic24:elasticApiFieldConfig({host:'http://user:pass@localhost:9200',apiVersion:'2.4',}),elastic17:elasticApiFieldConfig({host:'http://user:pass@localhost:9200',apiVersion:'1.7',}),},}),});
Fullcode example
Live demo ofIntrospection of Elasticsearch API via Graphiql
In other side this module is a plugin forgraphql-compose, which derives GraphQLType from yourelastic mapping 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).
Generated ObjectTypeComposer model has several awesome resolvers:
search- greatly simplified elasticsearchmethod. 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 tooptsinput field.searchConnection- elasticsearchmethod that implements Relay Cursor Connectionspec for infinite lists. Internally it uses cheapsearch_after API. One downside, Elastic does not support backward scrolling, sobeforeargument will not work.searchPagination- elasticsearchmethod that haspageandperPageargumentsfindById- get elastic record by idupdateById- update elastic record by id- feel free to add your resolver or ask for a new one
import{GraphQLSchema,GraphQLObjectType}from'graphql';importelasticsearchfrom'elasticsearch';import{composeWithElastic}from'graphql-compose-elasticsearch';constmapping={properties:{name:{type:'text',fields:{keyword:{type:'keyword',},},},gender:{type:'keyword',},skills:{type:'text',},languages:{type:'keyword',},location:{properties:{name:{type:'text',},point:{type:'geo_point',},},},createdAt:{type:'date',},},};constUserTC=composeWithElastic({graphqlTypeName:'UserES',elasticIndex:'user',elasticType:'user',elasticMapping:mapping,elasticClient:newelasticsearch.Client({host:'http://localhost:9200',apiVersion:'5.0',log:'trace',}),// elastic mapping does not contain information about is fields are arrays or not// so provide this information explicitly for obtaining correct types in GraphQLpluralFields:['skills','languages'],});constSchema=newGraphQLSchema({query:newGraphQLObjectType({name:'Query',fields:{user:UserTC.getResolver('search').getFieldConfig(),userPagination:UserTC.getResolver('searchPagination').getFieldConfig(),userConnection:UserTC.getResolver('searchConnection').getFieldConfig(),},}),});
Fullcode example
yarn add graphql graphql-compose elasticsearch graphql-compose-elasticsearch// ornpm install graphql graphql-compose elasticsearch graphql-compose-elasticsearch --save
Modulesgraphql,graphql-compose,elasticsearch are inpeerDependencies, so should be installed explicitly in your app.
If 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):
ActivitiesEsTC.addResolver({name:'addTag',kind:'mutation',type:'JSON',args:{id:'String!',tag:'String!',},resolve:({ args})=>{returnelasticClient.update({index:elasticIndex,type:elasticType,id:args.id,body:{script:{inline:'ctx._source.tags.add(params.tag)',params:{tag:args.tag},},},});},});
About
Hide Elastic Search REST API behind GraphQL.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors14
Uh oh!
There was an error while loading.Please reload this page.



