Skip to content

Commit 670a131

Browse files
committed
initial commit
0 parents  commit 670a131

File tree

15 files changed

+7195
-0
lines changed

15 files changed

+7195
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
insert_final_newline = false
15+
trim_trailing_whitespace = false

.eslintrc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
extends: [
4+
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
5+
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
6+
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
7+
],
8+
parserOptions: {
9+
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
10+
sourceType: 'module', // Allows for the use of imports
11+
},
12+
plugins: ['@typescript-eslint', 'prettier'],
13+
rules: {
14+
'@typescript-eslint/no-use-before-define': 0,
15+
},
16+
};

.github/workflows/release.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: release
2+
on:
3+
push:
4+
branches:
5+
- master
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v1
12+
with:
13+
node-version: '12.x'
14+
registry-url: 'https://registry.npmjs.org'
15+
- run: npm install
16+
- run: npm run test
17+
build:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v2
21+
- uses: actions/setup-node@v1
22+
with:
23+
node-version: '12.x'
24+
registry-url: 'https://registry.npmjs.org'
25+
- run: npm install
26+
- run: npm run build
27+
- run: npm publish --access public --tag latest
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.vscode
2+
.DS_STORE
3+
node_modules
4+
.module-cache
5+
*.log*
6+
build
7+
dist
8+
docs-dist
9+
lib
10+
.idea
11+
.docz
12+
.tscache
13+
package-lock.json
14+
*.tsbuildinfo*

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.vscode
2+
.DS_STORE
3+
.docz
4+
.tscache
5+
node_modules
6+
.module-cache
7+
*.log*
8+
build
9+
dist
10+
src
11+
example
12+
images
13+
.idea
14+
package-lock.json
15+
__tests__

.prettierrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
arrowParens: "always",
3+
printWidth: 80,
4+
semi: true,
5+
trailingComma: "all",
6+
singleQuote: true,
7+
printWidth: 120,
8+
tabWidth: 2
9+
};

LICENSE.md

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

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Transform GraphQL
2+
3+
[![npm](https://img.shields.io/npm/v/graphql-zeus.svg?style=flat-square)](https://www.npmjs.com/package/transform-graphql) [![npm downloads](https://img.shields.io/npm/dt/transform-graphql.svg?style=flat-square)](https://www.npmjs.com/package/transform-graphql)
4+
[![npm downloads](https://img.shields.io/github/workflow/status/graphql-editor/graphql-zeus/master.svg?style=flat-square)](https://www.npmjs.com/package/transform-graphql)
5+
6+
7+
We use GraphQL transformers. Examples are Graphback, Dgraph, AWS Amplify. This library provides function that given any GraphQL schema creates new GraphQL schemas basing on transformer functions.
8+
9+
## How it works
10+
11+
This short example simply shows what transform GraphQL is about:
12+
13+
Given the schema:
14+
```graphql
15+
type Post @model{
16+
name: String!
17+
content: String!
18+
createdAt: String!
19+
}
20+
21+
type Query{
22+
version:String
23+
}
24+
type Mutation{
25+
version:String
26+
}
27+
28+
directive @model on OBJECT
29+
```
30+
where model is our actual transformer
31+
32+
We expect schema to be transformed into
33+
34+
```graphql
35+
36+
directive @model on OBJECT
37+
38+
input CreatePost{
39+
name: String!
40+
content: String!
41+
createdAt: String!
42+
}
43+
44+
input DetailsPost{
45+
id: String!
46+
}
47+
48+
type Mutation{
49+
version: String
50+
post: PostMutation
51+
}
52+
53+
type Post @model{
54+
name: String!
55+
content: String!
56+
createdAt: String!
57+
}
58+
59+
type PostMutation{
60+
create(
61+
post: CreatePost
62+
): String!
63+
update(
64+
post: UpdatePost
65+
details: DetailsPost
66+
): String!
67+
remove(
68+
details: DetailsPost
69+
): String!
70+
}
71+
72+
type PostQuery{
73+
list: [Post!]!
74+
getByDetails(
75+
details: DetailsPost
76+
): Post
77+
}
78+
79+
type Query{
80+
version: String
81+
post: PostQuery
82+
}
83+
input UpdatePost{
84+
name: String!
85+
content: String!
86+
createdAt: String!
87+
}
88+
schema{
89+
query: Query,
90+
mutation: Mutation
91+
}
92+
```
93+
94+
And the transformer code should look like this
95+
96+
```ts
97+
const transformerCRUD: TransformerDef = {
98+
transformer: ({ field, operations }) => {
99+
if (!field.args) {
100+
throw new Error('Model can be used only for types');
101+
}
102+
if (!operations.query) {
103+
throw new Error('Query type required');
104+
}
105+
if (!operations.mutation) {
106+
throw new Error('Query type required');
107+
}
108+
return `
109+
input Create${field.name}{
110+
${TreeToGraphQL.parse({ nodes: field.args })}
111+
}
112+
input Update${field.name}{
113+
${TreeToGraphQL.parse({ nodes: field.args })}
114+
}
115+
input Details${field.name}{
116+
id: String!
117+
}
118+
type ${field.name}Query{
119+
list: [${field.name}!]!
120+
getByDetails(details: Details${field.name}): ${field.name}
121+
}
122+
type ${field.name}Mutation{
123+
create( ${field.name[0].toLowerCase() + field.name.slice(1)}: Create${field.name} ): String!
124+
update( ${field.name[0].toLowerCase() + field.name.slice(1)}: Update${field.name}, details: Details${
125+
field.name
126+
} ): String!
127+
remove( details: Details${field.name} ): String!
128+
}
129+
extend type ${operations.query.name}{
130+
${field.name[0].toLowerCase() + field.name.slice(1)}: ${field.name}Query
131+
}
132+
extend type ${operations.mutation.name}{
133+
${field.name[0].toLowerCase() + field.name.slice(1)}: ${field.name}Mutation
134+
}
135+
`;
136+
},
137+
directiveName: 'model',
138+
};
139+
140+
```
141+

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
moduleFileExtensions: ['ts', 'tsx', 'js'],
4+
moduleNameMapper: {
5+
'@/(.*)': ['<rootDir>/src/$1'],
6+
},
7+
testMatch: ['**/src/**/*.spec.(ts|tsx)'],
8+
watchPathIgnorePatterns: ['node_modules'],
9+
watchman: false,
10+
};

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "transform-graphql",
3+
"version": "0.0.1",
4+
"private": false,
5+
"license": "MIT",
6+
"description": "Generate GraphQL Schema from another schema with directives",
7+
"homepage": "https://graphqleditor.com",
8+
"main": "lib/index.js",
9+
"types": "lib/index.d.ts",
10+
"scripts": {
11+
"build": "ttsc -p tsconfig.build.json",
12+
"start": "ttsc -p tsconfig.build.json --watch",
13+
"test": "jest",
14+
"lint": "ttsc && eslint \"./src/**/*.{ts,js}\" --quiet --fix"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/graphql-editor/transform-graphql.git"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/graphql-editor/transform-graphql.git"
22+
},
23+
"devDependencies": {
24+
"@babel/core": "^7.2.2",
25+
"@babel/preset-env": "^7.11.5",
26+
"@types/jest": "^25.1.4",
27+
"@types/node": "^13.9.0",
28+
"@typescript-eslint/eslint-plugin": "^2.8.0",
29+
"@typescript-eslint/parser": "^2.8.0",
30+
"eslint": "^6.6.0",
31+
"eslint-config-prettier": "^6.7.0",
32+
"eslint-plugin-prettier": "^3.1.1",
33+
"jest": "^25.2.4",
34+
"prettier": "^2.0.2",
35+
"ts-jest": "^25.3.0",
36+
"ts-node": "^9.0.0",
37+
"ttypescript": "^1.5.11",
38+
"typescript": "^4.0.3",
39+
"typescript-transform-paths": "^2.0.0"
40+
},
41+
"dependencies": {
42+
"graphql-zeus": "^2.8.5"
43+
}
44+
}

0 commit comments

Comments
 (0)