在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):graphql-compose/graphql-compose-elasticsearch开源软件地址(OpenSource Url):https://github.com/graphql-compose/graphql-compose-elasticsearch开源编程语言(OpenSource Language):TypeScript 89.9%开源软件介绍(OpenSource Introduction):graphql-compose-elasticsearchThis module expose Elastic Search REST API via GraphQL. Elastic Search REST API proxySupported 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 mappingIn 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:
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 Installationyarn add graphql graphql-compose elasticsearch graphql-compose-elasticsearch
// or
npm install graphql graphql-compose elasticsearch graphql-compose-elasticsearch --save Modules ScreenshotsAPI proxy: Raw search methodAPI proxy: Getting several raw elastic metric in one requestMapping: Relay Cursor ConnectionMapping: Generated GraphQL Types and DocumentationFAQCreating custom ResolversIf 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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论