Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke the classes on generated code #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"plugins": ["@typescript-eslint", "prettier"],
"env": {
"node": true,
"jest": true,
"es6": true
},
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/member-delimiter-style": "off"
},
"parser": "@typescript-eslint/parser"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ coverage
graphql.*.json
junit.xml

.vs
.vs*

test/globalConfig.json
distTs
Expand Down
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always",
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true
}
53 changes: 30 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,46 @@
"graphql2ts": "./bin/cli"
},
"dependencies": {
"@babel/generator": "^7.6.2",
"@babel/parser": "^7.6.2",
"@babel/generator": "^7.7.7",
"@babel/parser": "^7.7.7",
"ast-types": "^0.13.2",
"graphql": "^14.5.8",
"prettier": "^1.18.2",
"yargs": "^14.0.0"
"prettier": "^1.19.1",
"yargs": "^15.0.2"
},
"devDependencies": {
"@babel/cli": "7.6.2",
"@babel/core": "7.6.2",
"@babel/node": "7.6.2",
"@babel/plugin-proposal-async-generator-functions": "7.2.0",
"@babel/plugin-proposal-class-properties": "7.5.5",
"@babel/plugin-proposal-export-default-from": "7.5.2",
"@babel/plugin-proposal-export-namespace-from": "7.5.2",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "7.6.2",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/plugin-transform-async-to-generator": "7.5.0",
"@babel/plugin-transform-flow-strip-types": "7.4.4",
"@babel/preset-env": "7.6.2",
"@babel/preset-flow": "7.0.0",
"@babel/preset-react": "7.0.0",
"@babel/preset-typescript": "7.6.0",
"@babel/register": "7.6.2",
"@babel/cli": "7.7.7",
"@babel/core": "7.7.7",
"@babel/node": "7.7.7",
"@babel/plugin-proposal-async-generator-functions": "7.7.4",
"@babel/plugin-proposal-class-properties": "7.7.4",
"@babel/plugin-proposal-export-default-from": "7.7.4",
"@babel/plugin-proposal-export-namespace-from": "7.7.4",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.7.4",
"@babel/plugin-proposal-object-rest-spread": "7.7.7",
"@babel/plugin-proposal-optional-chaining": "^7.7.5",
"@babel/plugin-transform-async-to-generator": "7.7.4",
"@babel/plugin-transform-flow-strip-types": "7.7.4",
"@babel/preset-env": "7.7.7",
"@babel/preset-flow": "7.7.4",
"@babel/preset-react": "7.7.4",
"@babel/preset-typescript": "7.7.7",
"@babel/register": "7.7.7",
"@types/graphql": "^14.5.0",
"@types/jest": "^24.0.25",
"@types/jscodeshift": "^0.6.3",
"@types/node": "^13.1.1",
"@types/prettier": "^1.19.0",
"@typescript-eslint/eslint-plugin": "^2.13.0",
"@typescript-eslint/parser": "^2.13.0",
"babel-jest": "^24.9.0",
"babel-plugin-jest-hoist": "^24.9.0",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.2",
"esm": "^3.2.25",
"jest": "^24.9.0",
"jscodeshift": "^0.6.4",
"typescript": "^3.6.3"
"jscodeshift": "^0.7.0",
"typescript": "^3.7.4"
},
"scripts": {
"start": "node babelx.js ./src/index.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/graphql2ts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from "path";
import fs from "fs";
import path from 'path';
import fs from 'fs';

import { graphql2ts } from '../graphql2ts';

const defineTest = (dirName: string, testFilePrefix: string, only: boolean = false) => {
const defineTest = (dirName: string, testFilePrefix: string, only = false) => {
const testName = `transforms correctly ${testFilePrefix}`;

const myIt = only ? it.only : it;
Expand Down
41 changes: 13 additions & 28 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
import yargs from 'yargs';

import fs from "fs";
import fs from 'fs';
import { graphql2ts } from './graphql2ts';
import path from "path";
import path from 'path';
import { promisify } from 'util';

const cwd = process.cwd();

const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);

type Argv = {
src: string,
keyMaxLength: number,
}

export const run = async (argv: Argv) => {
argv = yargs(argv || process.argv.slice(2))
.usage(
'Transform .graphql file to .graphql-js .ts file'
)
export const run = async () => {
const { graphql, output } = yargs(process.argv.slice(2))
.usage('Transform .graphql file to .graphql-js .ts file')
.default('graphql', 'schema.graphql')
.describe(
'graphql',
'The .graphql file'
)
.describe('graphql', 'The .graphql file')
.default('output', 'schema.ts')
.describe(
'output',
'the schema.ts output file',
)
.argv;

const filename = path.join(cwd, argv.graphql);

const source = fs.readFileSync(filename, { encoding: 'utf8' });
.describe('output', 'the schema.ts output file').argv;

const result = await graphql2ts(source);
const filename = path.join(cwd, graphql);
const source = await readFileAsync(filename, { encoding: 'utf8' });
const result = graphql2ts(source);

console.log('result: ', result);
console.log(result);

await writeFileAsync(path.join(cwd, argv.output), result);
await writeFileAsync(path.join(cwd, output), result);
};
102 changes: 102 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
ObjectTypeDefinitionNode,
FieldDefinitionNode,
TypeNode,
StringValueNode,
InputValueDefinitionNode,
DefinitionNode,
} from 'graphql';
import { parse as babelParser } from '@babel/parser';
import generate from '@babel/generator';

export const typedNode = (type: TypeNode): string => {
switch (type.kind) {
case 'NamedType':
return type.name.value;
case 'ListType':
return `new GraphQLList(${typedNode(type.type)})`;
case 'NonNullType':
return `new GraphQLNonNull(${typedNode(type.type)})`;
default:
return '';
}
};

export const descriptionDefinition = (description?: StringValueNode) => {
return description ? `description: '${description.value}',` : '';
};

export const inputValueDefinitionNode = (input: InputValueDefinitionNode) => {
const { name, description, type } = input;

const code = `
${name.value}: {
type: ${typedNode(type)},
${descriptionDefinition(description)}
},
`;

return code;
};

export const fieldDefinition = (field: FieldDefinitionNode) => {
const { name, description, type } = field;

const argCode =
field.arguments?.map((arg) => inputValueDefinitionNode(arg)) ?? [];

const args =
argCode.length > 0
? `
args: {
${argCode.join('\n')}
}
`
: '';

const code = `
${name.value}: {
type: ${typedNode(type)},
${descriptionDefinition(description)}
${args}
},
`;

return code;
};

export const objectTypeDefinition = (definition: ObjectTypeDefinitionNode) => {
const { name, description, fields } = definition;

const fieldsCode =
fields?.map((field) => fieldDefinition(field)).join('\n') ?? [];

const code = `
const ${name.value} = new GraphQLObjectType({
name: '${name.value}',
${descriptionDefinition(description)}
fields: () => ({
${fieldsCode}
})
});
`;
const ast = babelParser(code);

const output = generate(ast);

return output.code;
};

export const passThroughDefinition = () => '';

type Definitions = {
[K in DefinitionNode['kind']]: (def?: DefinitionNode) => string;
};

export const definitions = {
ObjectTypeDefinition: objectTypeDefinition,
InputObjectTypeDefinition: passThroughDefinition,
EnumTypeDefinition: passThroughDefinition,
UnionTypeDefinition: passThroughDefinition,
InterfaceTypeDefinition: passThroughDefinition,
} as Definitions;
Loading