Skip to content

Commit

Permalink
feat(core): added support for generated fragments and it's use inside…
Browse files Browse the repository at this point in the history
… documents
  • Loading branch information
dotansimha committed Dec 14, 2016
1 parent e2fb340 commit fbd268c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 15 deletions.
3 changes: 3 additions & 0 deletions generators/typescript/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"strategy": "SINGLE_FILE"
}
12 changes: 6 additions & 6 deletions generators/typescript/graphql-types.d.ts.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each ~}}
};
}

{{/if ~}}

Expand All @@ -13,7 +13,7 @@
{{#each enumValues }}
{{ name }} = <any>"{{ value }}"{{#unless @last}},{{/unless}}
{{/each ~}}
};
}

{{/if ~}}
{{/each ~}}
Expand All @@ -24,7 +24,7 @@
{{#each variables}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}}{{#unless isRequired}} | null{{/unless}};
{{/each ~}}
};
}

{{/if ~}}

Expand All @@ -41,7 +41,7 @@
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
};
}
{{/if}}

{{/each ~}}
Expand All @@ -51,9 +51,9 @@
{{#each fields}}
{{ name }}: {{#if isArray}}Array<{{/if}}{{ type }}{{#if isArray}}>{{/if}};
{{/each}}
};
}

{{/each ~}}
};
}

{{/each ~}}
22 changes: 16 additions & 6 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@ import {Codegen, Model, CodegenDocument} from './interfaces';
import {GraphQLNamedType, DefinitionNode, DocumentNode, Kind} from "graphql";
import {handleType} from "./model-handler";
import {handleOperation} from "./operation-handler";
import {handleFragment} from "./fragment-handler";

export const prepareCodegen = (schema: GraphQLSchema, document: DocumentNode): Codegen => {
let models: Model[] = [];
let documents: CodegenDocument[];
let documents: CodegenDocument[] = [];
let typesMap: GraphQLNamedType = schema.getTypeMap();

Object.keys(typesMap).forEach(typeName => {
models.push(handleType(typeName, typesMap[typeName]));
});

documents = document.definitions.map<CodegenDocument>((definition: DefinitionNode) => {
document.definitions.forEach((definition: DefinitionNode) => {
switch (definition.kind) {
case Kind.OPERATION_DEFINITION:
return handleOperation(schema, definition);
default:
return null;
case Kind.OPERATION_DEFINITION: {
documents.push(handleOperation(schema, definition));

break;
}
case Kind.FRAGMENT_DEFINITION: {
documents.push(handleFragment(schema, definition));

break;
}
default: {
break;
}
}
});

Expand Down
37 changes: 37 additions & 0 deletions src/fragment-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {GraphQLSchema} from "graphql/type/schema";
import {FragmentDefinitionNode} from "graphql/language/ast";
import {Model, CodegenDocument} from "./interfaces";
import pascalCase = require("pascal-case");
import {buildInnerModelsArray} from "./operation-handler";
import {typeFromAST} from "graphql/utilities/typeFromAST";

export const handleFragment = (schema: GraphQLSchema, fragmentNode: FragmentDefinitionNode): CodegenDocument => {
const rawName = fragmentNode.name.value;
const fragmentName = pascalCase(rawName);

let result: CodegenDocument = {
name: fragmentName,
rawName: rawName,
isQuery: false,
isSubscription: false,
isMutation: false,
isFragment: true,
innerTypes: [],
hasInnerTypes: false,
variables: [],
hasVariables: false
};

let appendTo: Model = {
name: 'Fragment',
fields: [],
isObject: true,
isFragment: true
};

const root = typeFromAST(schema, fragmentNode.typeCondition);
result.innerTypes = [appendTo, ...buildInnerModelsArray(schema, root, fragmentNode.selectionSet, appendTo)];
result.hasInnerTypes = result.innerTypes.length > 0;

return result;
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const schema = loadSchema('./dev-test/githunt/schema.json');
const documents = loadDocumentsSources([
'./dev-test/githunt/comment.query.graphql' ,
//'./dev-test/githunt/comment-added.subscription.graphql',
// //'./dev-test/githunt/comments-page-comment.fragment.graphql',
'./dev-test/githunt/comments-page-comment.fragment.graphql',
//'./dev-test/githunt/current-user.query.graphql',
//'./dev-test/githunt/vote.mutation.graphql'
]);
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface CodegenDocument {
isQuery: boolean;
isMutation: boolean;
isSubscription: boolean;
isFragment: boolean;
name: string;
rawName: string;
innerTypes: Model[];
Expand Down
4 changes: 2 additions & 2 deletions src/operation-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const handleNameDuplications = (name: string, existing: Model[]): string => {
return name;
};

const buildInnerModelsArray = (schema: GraphQLSchema, rootObject: GraphQLType, selections: SelectionSetNode, appendTo?: Model, result: Model[] = []): Model[] => {
export const buildInnerModelsArray = (schema: GraphQLSchema, rootObject: GraphQLType, selections: SelectionSetNode, appendTo?: Model, result: Model[] = []): Model[] => {
(selections ? selections.selections : []).forEach((selectionNode: SelectionNode) => {
switch (selectionNode.kind) {
case FIELD: {
Expand Down Expand Up @@ -93,7 +93,7 @@ const buildInnerModelsArray = (schema: GraphQLSchema, rootObject: GraphQLType, s

case FRAGMENT_SPREAD: {
const fragmentName = selectionNode.name.value;
appendTo.fragmentsUsed.push(fragmentName);
appendTo.fragmentsUsed.push(pascalCase(fragmentName) + '.Fragment');
appendTo.usingFragments = appendTo.fragmentsUsed.length > 0;

break;
Expand Down

0 comments on commit fbd268c

Please sign in to comment.