graphql-compose/graphql-compose-elasticsearch
Fork: 77 Star: 583 (更新于 2024-10-24 21:45:23)
license: MIT
Language: TypeScript .
Hide Elastic Search REST API behind GraphQL.
最后发布版本: v5.2.3 ( 2022-09-30 14:28:17)
graphql-compose-elasticsearch
This module expose Elastic Search REST API via GraphQL.
Elastic Search REST API proxy
Supported all elastic versions that support official 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.
import { GraphQLSchema, GraphQLObjectType } from 'graphql';
import elasticsearch from 'elasticsearch';
import { elasticApiFieldConfig } from 'graphql-compose-elasticsearch';
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
elastic50: elasticApiFieldConfig(
// you may provide existed Elastic Client instance
new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '5.0',
})
),
// or may provide just config
elastic24: elasticApiFieldConfig({
host: 'http://user:pass@localhost:9200',
apiVersion: '2.4',
}),
elastic17: elasticApiFieldConfig({
host: 'http://user:pass@localhost:9200',
apiVersion: '1.7',
}),
},
}),
});
Full code example
Live demo of Introspection of Elasticsearch API via Graphiql
ObjectTypeComposer from Elastic mapping
In other side this module is a plugin for graphql-compose, which derives GraphQLType from your elastic 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 Connection spec for infinite lists. Internally it uses cheap search_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';
import elasticsearch from 'elasticsearch';
import { composeWithElastic } from 'graphql-compose-elasticsearch';
const mapping = {
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',
},
},
};
const UserTC = composeWithElastic({
graphqlTypeName: 'UserES',
elasticIndex: 'user',
elasticType: 'user',
elasticMapping: mapping,
elasticClient: new elasticsearch.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 GraphQL
pluralFields: ['skills', 'languages'],
});
const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
user: UserTC.getResolver('search').getFieldConfig(),
userPagination: UserTC.getResolver('searchPagination').getFieldConfig(),
userConnection: UserTC.getResolver('searchConnection').getFieldConfig(),
},
}),
});
Full code example
Installation
yarn add graphql graphql-compose elasticsearch graphql-compose-elasticsearch
// or
npm install graphql graphql-compose elasticsearch graphql-compose-elasticsearch --save
Modules graphql
, graphql-compose
, elasticsearch
are in peerDependencies
, so should be installed explicitly in your app.
Screenshots
API proxy: Raw search method
API proxy: Getting several raw elastic metric in one request
Mapping: Relay Cursor Connection
Mapping: Generated GraphQL Types and Documentation
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 }) => {
return elasticClient.update({
index: elasticIndex,
type: elasticType,
id: args.id,
body: {
script: {
inline: 'ctx._source.tags.add(params.tag)',
params: { tag: args.tag },
},
},
});
},
});
License
最近版本更新:(数据更新于 2024-10-13 21:16:22)
2022-09-30 14:28:17 v5.2.3
2022-08-16 05:43:14 v5.2.2
2022-07-18 22:28:55 v5.2.1
2022-07-13 02:47:54 v5.2.0
2022-01-15 22:31:39 v5.1.0
2021-10-07 01:26:52 v5.0.1
2021-06-28 03:12:38 v5.0.0
2020-09-17 02:19:46 v4.0.10
2020-02-18 22:25:26 v4.0.9
2020-02-16 05:09:55 v4.0.8
主题(topics):
elastic, elasticsearch, graphql, graphql-compose
graphql-compose/graphql-compose-elasticsearch同语言 TypeScript最近更新仓库
2024-11-05 17:55:23 langgenius/dify
2024-11-05 17:35:52 siyuan-note/siyuan
2024-11-05 16:59:32 laurent22/joplin
2024-11-05 12:39:27 lobehub/lobe-chat
2024-11-05 11:52:31 RSSNext/Follow
2024-11-05 07:18:03 microsoft/genaiscript