Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
graphql-compose-elasticsearch
Advanced tools
Readme
This module expose Elastic Search REST API via GraphQL.
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
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 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.searchConnection
- elastic search
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, so before
argument will not work.searchPagination
- elastic search
method that has page
and perPage
argumentsfindById
- get elastic record by idupdateById
- update elastic record by idimport { 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
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.
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 },
},
},
});
},
});
FAQs
Elastic search via GraphQL
The npm package graphql-compose-elasticsearch receives a total of 206 weekly downloads. As such, graphql-compose-elasticsearch popularity was classified as not popular.
We found that graphql-compose-elasticsearch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.