Skip to content
Open
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
14 changes: 5 additions & 9 deletions packages/graphql-types-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@
},
"bin": "./lib/cli.js",
"scripts": {
"clean": "rimraf lib",
"clean": "node -e \"require('fs-extra').removeSync('lib')\"",
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"test:smoke": "npm install && npm run build && rimraf node_modules && npm install --prod && node ./lib/cli.js && echo 'Smoke Test Passed'",
"test:smoke": "npm install && npm run build && node -e \"require('fs-extra').removeSync('node_modules')\" && npm install --prod && node ./lib/cli.js && echo 'Smoke Test Passed'",
"extract-api": "ts-node ../../scripts/extract-api.ts"
},
"dependencies": {
"@babel/generator": "7.0.0-beta.4",
"@babel/types": "7.0.0-beta.4",
"babel-generator": "^6.26.1",
"babel-types": "^6.26.0",
"@babel/generator": "7.23.5",
"@babel/types": "7.23.5",
"change-case": "^4.1.1",
"common-tags": "^1.8.0",
"core-js": "^3.6.4",
Expand All @@ -47,18 +45,16 @@
"graphql": "^15.5.0",
"inflected": "^2.0.4",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"source-map-support": "^0.5.16",
"yargs": "^15.1.0"
},
"devDependencies": {
"@types/babel-generator": "^6.25.0",
"@types/babel__generator": "7.6.7",
"@types/common-tags": "^1.8.0",
"@types/fs-extra": "^8.1.0",
"@types/inflected": "^1.1.29",
"@types/node": "^10.17.13",
"@types/prettier": "^1.19.0",
"@types/rimraf": "^3.0.0",
"@types/yargs": "^15.0.1",
"@types/zen-observable": "^0.8.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GraphQLScalarType,
} from 'graphql';

import * as t from 'babel-types';
import * as t from '@babel/types';

import { createTypeAnnotationFromGraphQLTypeFunction } from '../helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ export class FlowAPIGenerator extends FlowGenerator {
if (variants.length === 1) {
const properties = this.getPropertiesForVariant(variants[0]);

const name = this.annotationFromScopeStack(this.scopeStack).id.name;
const name = this.scopeName(this.scopeStack);
const exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(name, properties));

this.printer.enqueue(exportedTypeAlias);
} else {
const unionMembers: t.FlowTypeAnnotation[] = [];
const unionMembers: t.FlowType[] = [];
variants.forEach(variant => {
this.scopeStackPush(variant.possibleTypes[0].toString());
const properties = this.getPropertiesForVariant(variant);

const name = this.annotationFromScopeStack(this.scopeStack).id.name;
const name = this.scopeName(this.scopeStack);
const exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(name, properties));

this.printer.enqueue(exportedTypeAlias);
Expand All @@ -175,7 +175,7 @@ export class FlowAPIGenerator extends FlowGenerator {
});

this.printer.enqueue(
this.exportDeclaration(this.typeAliasGenericUnion(this.annotationFromScopeStack(this.scopeStack).id.name, unionMembers))
this.exportDeclaration(this.typeAliasGenericUnion(this.scopeName(this.scopeStack), unionMembers))
);
}

Expand All @@ -199,14 +199,10 @@ export class FlowAPIGenerator extends FlowGenerator {

let res;
if (field.selectionSet) {
const genericAnnotation = this.annotationFromScopeStack(this.scopeStack);
if (field.type instanceof GraphQLNonNull) {
genericAnnotation.id.name = genericAnnotation.id.name;
} else {
genericAnnotation.id.name = '?' + genericAnnotation.id.name;
}

res = this.handleFieldSelectionSetValue(genericAnnotation, field);
const annotationName = field.type instanceof GraphQLNonNull ? this.scopeName(this.scopeStack) : `?${this.scopeName(this.scopeStack)}`;
const genericAnnotation = this.annotationFromName(annotationName);

res = this.handleFieldSelectionSetValue(annotationName, genericAnnotation, field);
} else {
res = this.handleFieldValue(field, variant);
}
Expand All @@ -216,7 +212,7 @@ export class FlowAPIGenerator extends FlowGenerator {
});
}

private handleFieldSelectionSetValue(genericAnnotation: t.GenericTypeAnnotation, field: Field) {
private handleFieldSelectionSetValue(annotationName: string, genericAnnotation: t.GenericTypeAnnotation, field: Field) {
const { selectionSet } = field;

const typeCase = this.getTypeCasesForSelectionSet(selectionSet as SelectionSet);
Expand All @@ -226,7 +222,7 @@ export class FlowAPIGenerator extends FlowGenerator {
if (variants.length === 1) {
const variant = variants[0];
const properties = this.getPropertiesForVariant(variant);
exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(this.annotationFromScopeStack(this.scopeStack).id.name, properties));
exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(this.scopeName(this.scopeStack), properties));
} else {
const propertySets = variants.map(variant => {
this.scopeStackPush(variant.possibleTypes[0].toString());
Expand All @@ -235,7 +231,7 @@ export class FlowAPIGenerator extends FlowGenerator {
return properties;
});

exportedTypeAlias = this.exportDeclaration(this.typeAliasObjectUnion(genericAnnotation.id.name, propertySets));
exportedTypeAlias = this.exportDeclaration(this.typeAliasObjectUnion(annotationName, propertySets));
}

this.printer.enqueue(exportedTypeAlias);
Expand All @@ -251,9 +247,7 @@ export class FlowAPIGenerator extends FlowGenerator {
let res;
if (field.name === '__typename') {
const annotations = variant.possibleTypes.map(type => {
const annotation = t.stringLiteralTypeAnnotation();
annotation.value = type.toString();
return annotation;
return t.stringLiteralTypeAnnotation(type.toString());
});

res = {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-types-generator/src/flow-modern/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isListType,
} from 'graphql';

import * as t from 'babel-types';
import * as t from '@babel/types';

import { CompilerOptions } from '../compiler';

Expand All @@ -28,7 +28,7 @@ export function createTypeAnnotationFromGraphQLTypeFunction(compilerOptions: Com
{ nullable }: { nullable: boolean } = {
nullable: true,
}
): t.FlowTypeAnnotation {
): t.FlowType {
if (isNonNullType(type)) {
return typeAnnotationFromGraphQLType(type.ofType, { nullable: false });
}
Expand Down
59 changes: 27 additions & 32 deletions packages/graphql-types-generator/src/flow-modern/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { createTypeAnnotationFromGraphQLTypeFunction } from './helpers';

import * as t from '@babel/types';

const commentLine = (value: string): t.CommentLine => ({
type: 'CommentLine',
value,
});

export type ObjectProperty = {
name: string;
description?: string | null | undefined;
annotation: t.FlowTypeAnnotation;
annotation: t.FlowType;
};

export interface FlowCompilerOptions extends CompilerOptions {
Expand All @@ -28,21 +33,15 @@ export class FlowGenerator {

public enumerationDeclaration(type: GraphQLEnumType) {
const { name, description } = type;
const unionValues = type.getValues().map(({ value }) => {
const type = t.stringLiteralTypeAnnotation();
type.value = value;

return type;
});
const unionValues = type.getValues().map(({ value }) =>
t.stringLiteralTypeAnnotation(value)
);

const typeAlias = t.exportNamedDeclaration(t.typeAlias(t.identifier(name), undefined, t.unionTypeAnnotation(unionValues)), []);

typeAlias.leadingComments = [
{
type: 'CommentLine',
value: ` ${description}`,
} as t.CommentLine,
];
if (description) {
typeAlias.leadingComments = [commentLine(` ${description}`)];
}

return typeAlias;
}
Expand All @@ -61,23 +60,16 @@ export class FlowGenerator {

const typeAlias = this.typeAliasObject(name, fields);

typeAlias.leadingComments = [
{
type: 'CommentLine',
value: ` ${description}`,
} as t.CommentLine,
];
if (description) {
typeAlias.leadingComments = [commentLine(` ${description}`)];
}

return typeAlias;
}

public objectTypeAnnotation(fields: ObjectProperty[], isInputObject: boolean = false) {
const objectTypeAnnotation = t.objectTypeAnnotation(
fields.map(({ name, description, annotation }) => {
if (annotation.type === 'NullableTypeAnnotation') {
t.identifier(name + '?');
}

const objectTypeProperty = t.objectTypeProperty(
t.identifier(
// Nullable fields on input objects do not have to be defined
Expand All @@ -88,12 +80,7 @@ export class FlowGenerator {
);

if (description) {
objectTypeProperty.trailingComments = [
{
type: 'CommentLine',
value: ` ${description}`,
} as t.CommentLine,
];
objectTypeProperty.trailingComments = [commentLine(` ${description}`)];
}

return objectTypeProperty;
Expand Down Expand Up @@ -123,15 +110,23 @@ export class FlowGenerator {
);
}

public typeAliasGenericUnion(name: string, members: t.FlowTypeAnnotation[]) {
public typeAliasGenericUnion(name: string, members: t.FlowType[]) {
return t.typeAlias(t.identifier(name), undefined, t.unionTypeAnnotation(members));
}

public exportDeclaration(declaration: t.Declaration) {
return t.exportNamedDeclaration(declaration, []);
}

public annotationFromScopeStack(scope: string[]) {
return t.genericTypeAnnotation(t.identifier(scope.join('_')));
public scopeName(scope: string[]): string {
return scope.join('_');
}

public annotationFromName(name: string): t.GenericTypeAnnotation {
return t.genericTypeAnnotation(t.identifier(name));
}

public annotationFromScopeStack(scope: string[]): t.GenericTypeAnnotation {
return this.annotationFromName(this.scopeName(scope));
}
}

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions packages/graphql-types-generator/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GraphQLSchema, DocumentNode, Source } from 'graphql';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as rimraf from 'rimraf';

import { loadSchema, loadAndMergeQueryDocuments, parseSchema, parseAndMergeQueryDocuments } from './loading';
import { validateQueryDocument } from './validation';
Expand Down Expand Up @@ -178,9 +177,9 @@ function generateTypesFlowModern(schema: GraphQLSchema, document: DocumentNode,
function writeGeneratedFiles(generatedFiles: BasicGeneratedFileMap, outputDirectory: string) {
// Clear all generated stuff to make sure there isn't anything
// unnecessary lying around.
rimraf.sync(outputDirectory);
fs.removeSync(outputDirectory);
// Remake the output directory
fs.mkdirSync(outputDirectory);
fs.ensureDirSync(outputDirectory);

for (const [fileName, generatedFile] of Object.entries(generatedFiles)) {
fs.writeFileSync(path.join(outputDirectory, fileName), generatedFile.output);
Expand Down
Loading