Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Hide Elastic Search REST API behind GraphQL.

License

NotificationsYou must be signed in to change notification settings

graphql-compose/graphql-compose-elasticsearch

Repository files navigation

npmtrendsCommitizen friendlysemantic-release

This module expose Elastic Search REST API via GraphQL.

Elastic Search REST API proxy

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


ObjectTypeComposer from Elastic mapping

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 elasticsearch 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 toopts input field.
  • searchConnection - elasticsearch method that implements Relay Cursor Connectionspec for infinite lists. Internally it uses cheapsearch_after API. One downside, Elastic does not support backward scrolling, sobefore argument will not work.
  • searchPagination - elasticsearch method that haspage andperPage arguments
  • findById - get elastic record by id
  • updateById - 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

Installation

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.

Screenshots

API proxy: Raw search method

screen shot 2017-03-07 at 22 26 17

API proxy: Getting several raw elastic metric in one request

screen shot 2017-03-07 at 22 34 01

Mapping: Relay Cursor Connection

screen shot 2017-03-22 at 19 34 09

Mapping: Generated GraphQL Types and Documentation

screen shot 2017-03-22 at 19 33 24

FAQ

Creating custom Resolvers

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},},},});},});

License

MIT

Sponsor this project

  •  

Packages

No packages published

Contributors14


[8]ページ先頭

©2009-2025 Movatter.jp