Skip to content

Commit 630b703

Browse files
committed
graphql-cli-load a data import extension to call mutations with data from json or csv
0 parents  commit 630b703

15 files changed

+650
-0
lines changed

.gitignore

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
yarn.lock
57+
58+
# dotenv environment variables file
59+
.env
60+
61+
# build
62+
dist/

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Michael Hunger
4+
5+
based on code from graphql-cli-codegen by Robin Richard (Thank you!)
6+
7+
Copyright (c) 2017 Robin Ricard
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# graphql-cli-load
2+
3+
Data import plugin for graphql-cli.
4+
5+
## Installation
6+
7+
```
8+
npm i -g graphql-cli graphql-cli-load
9+
```
10+
11+
## Configuration
12+
13+
Configure the plugin via the `.graphqlconfig` or command line.
14+
15+
The schema will be obtained from the `schemaPath` key, you can fetch it via `graphql get-schema`
16+
17+
All other options such as `csv`, `json`, `mutation` or `mapping` _can_ be placed in the `extensions.load` key
18+
or provided via commandline.
19+
20+
Here is an example:
21+
22+
`.graphqlconfig`
23+
```json
24+
{
25+
"schemaPath": "schema.graphql",
26+
"includes": [
27+
"src/**/*.graphql",
28+
"src/**/*.gql",
29+
],
30+
"extensions": {
31+
"load": { // this section is optional
32+
"csv" : "reviews.txt",
33+
"mutation": "createReview",
34+
"mapping": { "text" : "review", "rating" : "stars"}
35+
}
36+
}
37+
}
38+
```
39+
40+
## Usage
41+
42+
You can now run:
43+
44+
```
45+
graphql load
46+
```
47+
48+
or
49+
50+
```
51+
graphql load --csv reviews.txt --mutation createReview2 --mapping '{ "text" : "commentary", "rating" : "stars"}'
52+
53+
Using endpoint starwars: http://localhost:7474/graphql/
54+
Using mutation "createReview2".
55+
56+
Done parsing CSV-file /Users/mh/d/js/graphql-cli-load/example/reviews.txt rows: 3
57+
meta: {"delimiter":"\t","linebreak":"\n","aborted":false,"truncated":false,"cursor":566,"fields":["episode","text","rating"]}
58+
59+
Using mapping: {"text":"commentary","rating":"stars"}
60+
61+
Sending query:
62+
mutation {
63+
_0 : createReview2 ( episode: NEWHOPE,commentary: "A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possibilities of blockbuster filmmaki...
64+
65+
✔ Call succeeded:
66+
{"_0":"Nodes created: 1\nProperties set: 3\nLabels added: 1\n","_1":"Nodes created: 1\nProperties set: 3\nLabels added: 1\n","_2":"Nodes created: 1\nProperties set: 3\nLabels added: 1\n"}...
67+
```
68+
69+
## Options
70+
71+
```
72+
graphql load [--json=file.json] [--csv=file.csv] [--endpoint=name] [--mutation=createType] [--mapping='{"title":"name"}']
73+
74+
Optionen:
75+
--help show help
76+
--mapping, -p name mapping of input to mutation (json)
77+
--mutation, -m mutation to call
78+
--endpoint, -e endpoint name to use
79+
--json, -j json file to load
80+
--csv, -c csv file to load
81+
```
82+
83+
Which will take each line of the csv or json file and call the mutation with the data (optionally mapping columns).
84+
Non-absolute files are resolved relative to the directory containing `.graphqlconfig`.
85+
86+
## TODO
87+
88+
* parameters
89+
* type conversion
90+
* file urls
91+
92+
93+
## Test with Neo4j-GraphQL Extension
94+
95+
To test this with the neo4j-graphql extension:
96+
97+
1. `npm install -g neo4j-graphql-cli`
98+
2. `neo4j-graphql example/example-schema.graphql` (remember the auth header)
99+
4. `npm install -g graphql-cli graphql-cli-load`
100+
5. Run `graphql` in `example` to install the endpoint, and manually add the auth-header, like here:
101+
```
102+
"endpoints": {
103+
"starwars": {"url":"http://localhost:7474/graphql/","headers":{"Authorization": "Basic bmVvNGo6dGVzdA=="}}
104+
},
105+
```
106+
6. Run `graphql load --csv reviews2.txt` or `graphql load --csv reviews2.json`

example/.graphqlconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"schemaPath": "schema.graphql",
3+
"extensions": {
4+
"endpoints": {
5+
"starwars": {"url":"http://localhost:7474/graphql/",
6+
"headers":{"Authorization": "Basic bmVvNGo6dGVzdA=="}}
7+
},
8+
"load": {
9+
"json" : "reviews.json",
10+
"mutation": "createReview",
11+
"mapping": { "text" : "commentary", "rating" : "stars"}
12+
}
13+
}
14+
}

example/example-schema.graphql

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
schema {
2+
query: QueryType
3+
mutation: MutationType
4+
}
5+
6+
# The query type, represents all of the entry points into our object graph
7+
type QueryType {
8+
reviews(episode: Episode!): [Review]
9+
}
10+
11+
# The mutation type, represents all updates we can make to our data
12+
type MutationType {
13+
# createReview(episode: Episode, review: ReviewInput!): Review
14+
# createReview(episode: Episode, stars: Int!, commentary: String): Review
15+
createReview2(episode: Episode, stars: Int!, commentary: String): String @cypher(statement:"CREATE (r:Review {episode:{episode},stars:{stars},commentar:{commentary}})")
16+
}
17+
# The episodes in the Star Wars trilogy
18+
enum Episode {
19+
# Star Wars Episode IV: A New Hope, released in 1977.
20+
NEWHOPE
21+
# Star Wars Episode V: The Empire Strikes Back, released in 1980.
22+
EMPIRE
23+
# Star Wars Episode VI: Return of the Jedi, released in 1983.
24+
JEDI
25+
}
26+
27+
# Represents a review for a movie
28+
type Review {
29+
episode: Episode!
30+
# The number of stars this review gave, 1-5
31+
stars: Int!
32+
# Comment about the movie
33+
commentary: String
34+
}
35+
# The input object sent when someone is creating a new review
36+
input ReviewInput {
37+
# 0-5 stars
38+
stars: Int!
39+
# Comment about the movie, optional
40+
commentary: String
41+
}

example/reviews.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{"episode":"NEWHOPE", "stars":93,
3+
"commentary":"A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possibilities of blockbuster filmmaking and things have never been the same."
4+
},
5+
{"episode":"EMPIRE", "stars":94,
6+
"commentary":"Dark, sinister, but ultimately even more involving than A New Hope, The Empire Strikes Back defies viewer expectations and takes the series to heightened emotional levels."},
7+
{"episode":"JEDI", "stars":80,
8+
"commentary":"Though failing to reach the cinematic heights of its predecessors, Return of the Jedi remains an entertaining sci-fi adventure and a fitting end to the classic trilogy."}
9+
]

example/reviews.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
episode commentary stars
2+
NEWHOPE A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possibilities of blockbuster filmmaking and things have never been the same. 93
3+
EMPIRE Dark, sinister, but ultimately even more involving than A New Hope, The Empire Strikes Back defies viewer expectations and takes the series to heightened emotional levels. 94
4+
JEDI Though failing to reach the cinematic heights of its predecessors, Return of the Jedi remains an entertaining sci-fi adventure and a fitting end to the classic trilogy. 80

example/reviews_mapped.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{"episode":"NEWHOPE", "rating":93,
3+
"text":"A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possibilities of blockbuster filmmaking and things have never been the same."
4+
},
5+
{"episode":"EMPIRE", "rating":94,
6+
"text":"Dark, sinister, but ultimately even more involving than A New Hope, The Empire Strikes Back defies viewer expectations and takes the series to heightened emotional levels."},
7+
{"episode":"JEDI", "rating":80,
8+
"text":"Though failing to reach the cinematic heights of its predecessors, Return of the Jedi remains an entertaining sci-fi adventure and a fitting end to the classic trilogy."}
9+
]

example/reviews_mapped.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
episode text rating
2+
NEWHOPE A legendarily expansive and ambitious start to the sci-fi saga, George Lucas opened our eyes to the possibilities of blockbuster filmmaking and things have never been the same. 93
3+
EMPIRE Dark, sinister, but ultimately even more involving than A New Hope, The Empire Strikes Back defies viewer expectations and takes the series to heightened emotional levels. 94
4+
JEDI Though failing to reach the cinematic heights of its predecessors, Return of the Jedi remains an entertaining sci-fi adventure and a fitting end to the classic trilogy. 80

0 commit comments

Comments
 (0)