Skip to content

Commit f6af76f

Browse files
committed
chore(example): add seedData, small cleanups
1 parent 6647c43 commit f6af76f

File tree

6 files changed

+520
-142
lines changed

6 files changed

+520
-142
lines changed

examples/elastic50/index.js

Lines changed: 2 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,15 @@
33

44
import express from 'express';
55
import graphqlHTTP from 'express-graphql';
6-
import { graphql } from 'graphql-compose';
7-
import elasticsearch from 'elasticsearch';
8-
import { composeWithElastic, elasticApiFieldConfig } from '../../src'; // from 'graphql-compose-elasticsearch';
9-
10-
const { GraphQLSchema, GraphQLObjectType } = graphql;
6+
import schema from './schema';
117

128
const expressPort = process.env.port || process.env.PORT || 9201;
139

14-
// mapping obtained from ElasticSearch server
15-
// GET http://user:pass@localhost:9200/user/_mapping
16-
const indexMapping = {
17-
user: {
18-
mappings: {
19-
user: {
20-
properties: {
21-
name: {
22-
type: 'text',
23-
fields: {
24-
keyword: {
25-
type: 'keyword',
26-
},
27-
},
28-
},
29-
gender: {
30-
type: 'text',
31-
},
32-
birthday: {
33-
type: 'date',
34-
},
35-
position: {
36-
type: 'text',
37-
},
38-
relocation: {
39-
type: 'boolean',
40-
},
41-
salary: {
42-
properties: {
43-
currency: {
44-
type: 'text',
45-
},
46-
total: {
47-
type: 'double',
48-
},
49-
},
50-
},
51-
skills: {
52-
type: 'text',
53-
},
54-
languages: {
55-
type: 'keyword',
56-
},
57-
location: {
58-
properties: {
59-
name: {
60-
type: 'text',
61-
},
62-
point: {
63-
type: 'geo_point',
64-
},
65-
},
66-
},
67-
experience: {
68-
properties: {
69-
company: {
70-
type: 'text',
71-
},
72-
description: {
73-
type: 'text',
74-
},
75-
end: {
76-
type: 'date',
77-
},
78-
position: {
79-
type: 'text',
80-
},
81-
start: {
82-
type: 'date',
83-
},
84-
tillNow: {
85-
type: 'boolean',
86-
},
87-
},
88-
},
89-
createdAt: {
90-
type: 'date',
91-
},
92-
},
93-
},
94-
},
95-
},
96-
};
97-
98-
const UserEsTC = composeWithElastic({
99-
graphqlTypeName: 'UserES',
100-
elasticIndex: 'cv',
101-
elasticType: 'cv',
102-
elasticMapping: indexMapping.user.mappings.user,
103-
elasticClient: new elasticsearch.Client({
104-
host: 'http://localhost:9200',
105-
apiVersion: '5.0',
106-
log: 'trace',
107-
}),
108-
// elastic mapping does not contain information about is fields are arrays or not
109-
// so provide this information explicitly for obtaining correct types in GraphQL
110-
pluralFields: ['skills', 'languages'],
111-
});
112-
113-
const generatedSchema = new GraphQLSchema({
114-
query: new GraphQLObjectType({
115-
name: 'Query',
116-
fields: {
117-
user: UserEsTC.get('$search').getFieldConfig(),
118-
userConnection: UserEsTC.get('$searchConnection').getFieldConfig(),
119-
elastic50: elasticApiFieldConfig({
120-
host: 'http://user:pass@localhost:9200',
121-
apiVersion: '5.0',
122-
log: 'trace',
123-
}),
124-
},
125-
}),
126-
});
127-
12810
const server = express();
12911
server.use(
13012
'/',
13113
graphqlHTTP({
132-
schema: (generatedSchema: any),
14+
schema: (schema: any),
13315
graphiql: true,
13416
formatError: error => ({
13517
message: error.message,

examples/elastic50/schema.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* @flow */
2+
3+
import elasticsearch from 'elasticsearch';
4+
import { graphql } from 'graphql-compose';
5+
import { composeWithElastic, elasticApiFieldConfig } from '../../src'; // from 'graphql-compose-elasticsearch';
6+
7+
const { GraphQLSchema, GraphQLObjectType } = graphql;
8+
9+
// Mapping obtained from ElasticSearch server
10+
// If you have existed index in ES you may load mapping via
11+
// GET http://user:pass@localhost:9200/demo_user/_mapping
12+
// and then get subtree of returned document which contains
13+
// properties definitions (which looks like following data):
14+
const demoUserMapping = {
15+
properties: {
16+
name: {
17+
type: 'text',
18+
fields: {
19+
keyword: {
20+
type: 'keyword',
21+
},
22+
},
23+
},
24+
gender: {
25+
type: 'text',
26+
},
27+
birthday: {
28+
type: 'date',
29+
},
30+
position: {
31+
type: 'text',
32+
},
33+
relocation: {
34+
type: 'boolean',
35+
},
36+
salary: {
37+
properties: {
38+
currency: {
39+
type: 'text',
40+
},
41+
total: {
42+
type: 'double',
43+
},
44+
},
45+
},
46+
skills: {
47+
type: 'text',
48+
},
49+
languages: {
50+
type: 'keyword',
51+
},
52+
location: {
53+
properties: {
54+
name: {
55+
type: 'text',
56+
},
57+
point: {
58+
type: 'geo_point',
59+
},
60+
},
61+
},
62+
experience: {
63+
properties: {
64+
company: {
65+
type: 'text',
66+
},
67+
description: {
68+
type: 'text',
69+
},
70+
end: {
71+
type: 'date',
72+
},
73+
position: {
74+
type: 'text',
75+
},
76+
start: {
77+
type: 'date',
78+
},
79+
tillNow: {
80+
type: 'boolean',
81+
},
82+
},
83+
},
84+
createdAt: {
85+
type: 'date',
86+
},
87+
},
88+
};
89+
90+
const UserEsTC = composeWithElastic({
91+
graphqlTypeName: 'UserES',
92+
elasticIndex: 'demo_user',
93+
elasticType: 'demo_user',
94+
elasticMapping: demoUserMapping,
95+
elasticClient: new elasticsearch.Client({
96+
host: 'http://localhost:9200',
97+
apiVersion: '5.0',
98+
log: 'trace',
99+
}),
100+
// elastic mapping does not contain information about is fields are arrays or not
101+
// so provide this information explicitly for obtaining correct types in GraphQL
102+
pluralFields: ['skills', 'languages'],
103+
});
104+
105+
const schema = new GraphQLSchema({
106+
query: new GraphQLObjectType({
107+
name: 'Query',
108+
fields: {
109+
user: UserEsTC.getResolver('search').getFieldConfig(),
110+
userConnection: UserEsTC.getResolver('searchConnection').getFieldConfig(),
111+
elastic50: elasticApiFieldConfig({
112+
host: 'http://user:pass@localhost:9200',
113+
apiVersion: '5.0',
114+
log: 'trace',
115+
}),
116+
},
117+
}),
118+
});
119+
120+
export default schema;

examples/elastic50/seedData.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* @flow */
2+
3+
import elasticsearch from 'elasticsearch';
4+
import seedData from './seedData.json';
5+
6+
const client = new elasticsearch.Client({
7+
host: 'localhost:9200',
8+
log: 'trace',
9+
});
10+
11+
const body = [];
12+
seedData.forEach(row => {
13+
const { id, ...restData } = row;
14+
body.push(
15+
{ index: { _index: 'demo_user', _type: 'demo_user', _id: id } },
16+
restData
17+
);
18+
});
19+
20+
client
21+
.bulk({
22+
index: 'demo_user',
23+
type: 'demo_user',
24+
body,
25+
})
26+
.then(() => {
27+
console.log('Data successfully seeded!');
28+
});

examples/elastic50/seedData.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "Pavel",
5+
"gender": "male",
6+
"birthday": "1985-07-15",
7+
"position": "CTO",
8+
"relocation": true,
9+
"salary": {
10+
"total": 333,
11+
"currency": "BTC"
12+
},
13+
"skills": [ "nodejs", "graphql", "babel", "webpack", "oss contributor", "devOps" ],
14+
"languages": [ "ru", "en" ],
15+
"location": {
16+
"name": "Almaty",
17+
"point": [43.238949, 76.889709]
18+
},
19+
"createdAt": "2017-10-19T04:29:08Z"
20+
},
21+
{
22+
"id": 2,
23+
"name": "Helen",
24+
"gender": "female",
25+
"birthday": "1986-03-06",
26+
"position": "sales manager",
27+
"relocation": true,
28+
"salary": {
29+
"total": 12345,
30+
"currency": "USD"
31+
},
32+
"skills": [ "Communication", "Ability to Attain Targets", "Analytical Ability", "Judgment"],
33+
"languages": [ "ru", "en" ],
34+
"location": {
35+
"name": "Almaty",
36+
"point": [43.238949, 76.889709]
37+
},
38+
"createdAt": "2017-10-19T04:29:09Z"
39+
},
40+
{
41+
"id": 3,
42+
"name": "Andrew",
43+
"gender": "male",
44+
"birthday": "1952-02-19",
45+
"position": "Vice President Sales",
46+
"relocation": false,
47+
"salary": {
48+
"total": 23456,
49+
"currency": "USD"
50+
},
51+
"skills": [ "BTS commercial", "international marketing"],
52+
"languages": [ "en", "fr", "ge" ],
53+
"location": {
54+
"name": "Tacoma"
55+
},
56+
"createdAt": "2017-10-19T04:29:10Z"
57+
},
58+
{
59+
"id": 4,
60+
"name": "Steven",
61+
"gender": "male",
62+
"birthday": "1955-03-04",
63+
"position": "Sales Representative",
64+
"relocation": false,
65+
"salary": {
66+
"total": 12222,
67+
"currency": "USD"
68+
},
69+
"skills": [ "BA in psychology", "Art of the Cold Call"],
70+
"languages": [ "en" ],
71+
"location": {
72+
"name": "Seattle"
73+
},
74+
"createdAt": "2017-10-19T04:29:11Z"
75+
}
76+
]

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"graphql": "^0.11.3",
5252
"graphql-compose": "^2.9.0",
5353
"jest": "^21.2.1",
54+
"nodemon": "^1.12.1",
5455
"npm-run-all": "^4.1.1",
5556
"prettier": "^1.7.4",
5657
"rimraf": "^2.6.2",
@@ -71,7 +72,8 @@
7172
"build:lib": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
7273
"build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
7374
"dev": "npm run demo1",
74-
"demo1": "nodemon -e js --ignore *test* --exec ./node_modules/.bin/babel-node ./examples/elastic50/index.js",
75+
"demo1": "yarn demo1:seed && nodemon -e js --ignore *test* --exec ./node_modules/.bin/babel-node ./examples/elastic50/index.js",
76+
"demo1:seed": "./node_modules/.bin/babel-node ./examples/elastic50/seedData.js",
7577
"demo2": "nodemon -e js --ignore *test* --exec ./node_modules/.bin/babel-node ./examples/differentVersions/index.js",
7678
"watch": "jest --watch",
7779
"coverage": "jest --coverage",

0 commit comments

Comments
 (0)