Skip to content

Commit

Permalink
feat(core): added document content, added basic Swift generated content
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Dec 14, 2016
1 parent 2816a3e commit 2d144ff
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
60 changes: 60 additions & 0 deletions dev-test/githunt/API.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
public enum FeedType: String {
case HOT = "HOT"
case NEW = "NEW"
case TOP = "TOP"
}

public struct Entry: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(repository: Repository, postedBy: User, createdAt: Float, score: Int, hotScore: Float, comments: [Comment], commentCount: Int, id: Int, vote: Vote) {
fieldsMap = ["repository": repository, "postedBy": postedBy, "createdAt": createdAt, "score": score, "hotScore": hotScore, "comments": comments, "commentCount": commentCount, "id": id, "vote": vote]
}
}

public struct Repository: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(name: String, full_name: String, description: String?, html_url: String, stargazers_count: Int, open_issues_count: Int?, owner: User?) {
fieldsMap = ["name": name, "full_name": full_name, "description": description, "html_url": html_url, "stargazers_count": stargazers_count, "open_issues_count": open_issues_count, "owner": owner]
}
}

public struct User: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(login: String, avatar_url: String, html_url: String) {
fieldsMap = ["login": login, "avatar_url": avatar_url, "html_url": html_url]
}
}

public struct Comment: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(id: Int, postedBy: User, createdAt: Float, content: String, repoName: String) {
fieldsMap = ["id": id, "postedBy": postedBy, "createdAt": createdAt, "content": content, "repoName": repoName]
}
}

public struct Vote: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(vote_value: Int) {
fieldsMap = ["vote_value": vote_value]
}
}

public struct A: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init(test: MyType) {
fieldsMap = ["test": test]
}
}

public enum VoteType: String {
case UP = "UP"
case DOWN = "DOWN"
case CANCEL = "CANCEL"
}

11 changes: 11 additions & 0 deletions generators/swift-apollo-single-file/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"strategy": "SINGLE_FILE",
"template": "./template.handlebars",
"primitives": {
"String": "String",
"Int": "Int",
"Float": "Float",
"Boolean": "Bool",
"ID": "String"
}
}
21 changes: 21 additions & 0 deletions generators/swift-apollo-single-file/template.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{#each models ~}}
{{#if isObject ~}}
public struct {{ name }}: GraphQLMapConvertible {
public var fieldsMap: GraphQLMap

public init({{#each fields ~}}{{ name }}: {{#if isArray}}[{{/if}}{{ type }}{{#if isArray}}]{{/if}}{{#unless isRequired}}?{{/unless}}{{#unless @last}}, {{/unless}}{{/each ~}}) {
fieldsMap = [{{#each fields ~}}"{{ name }}": {{ name }}{{#unless @last}}, {{/unless}}{{/each ~}}]
}
}

{{/if ~}}

{{#if isEnum}}
public enum {{ name }}: String {
{{#each enumValues }}
case {{name}} = "{{ value }}"
{{/each}}
}

{{/if ~}}
{{/each ~}}
4 changes: 3 additions & 1 deletion src/fragment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Model, CodegenDocument} from './interfaces';
import pascalCase = require('pascal-case');
import {buildInnerModelsArray} from './operation-handler';
import {typeFromAST} from 'graphql/utilities/typeFromAST';
import {print} from 'graphql/language/printer';

export const handleFragment = (schema: GraphQLSchema, fragmentNode: FragmentDefinitionNode, primitivesMap: any): CodegenDocument => {
const rawName = fragmentNode.name.value;
Expand All @@ -19,7 +20,8 @@ export const handleFragment = (schema: GraphQLSchema, fragmentNode: FragmentDefi
innerTypes: [],
hasInnerTypes: false,
variables: [],
hasVariables: false
hasVariables: false,
document: print(fragmentNode)
};

let appendTo: Model = {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface CodegenDocument {
isSubscription: boolean;
isFragment: boolean;
name: string;
document: string;
rawName: string;
innerTypes: Model[];
variables: Field[];
Expand Down
5 changes: 3 additions & 2 deletions src/operation-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {CodegenDocument, Field, Model} from './interfaces';
import {getTypeName, isArray, isRequired, isPrimitive} from './model-handler';
import {getFieldDef} from './utils';
import pascalCase = require('pascal-case');
import {print} from 'graphql/language/printer';

const typesMap = {
query: 'Query',
Expand Down Expand Up @@ -46,7 +47,6 @@ export const buildInnerModelsArray = (schema: GraphQLSchema,
appendTo?: Model,
result: Model[] = []): Model[] => {
(selections ? selections.selections : []).forEach((selectionNode: SelectionNode) => {
console.log(primitivesMap);
switch (selectionNode.kind) {
case FIELD:
const fieldName = selectionNode.name.value;
Expand Down Expand Up @@ -144,7 +144,8 @@ export const handleOperation = (schema: GraphQLSchema, definitionNode: Operation
innerTypes: [],
hasVariables: false,
hasInnerTypes: false,
imports: []
imports: [],
document: print(definitionNode)
};

document.variables = buildVariables(schema, definitionNode, primitivesMap);
Expand Down
5 changes: 5 additions & 0 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ export const generators: GeneratorTemplate[] = [
language: 'TypeScript Multiple Files',
aliases: ['ts-multiple', 'typescript-multiple'],
config: getConfig('../generators/typescript-multiple-files/')
},
{
language: 'Swift (Apollo) Single File',
aliases: ['swift-apollo', 'swift', 'swift-apollo-single'],
config: getConfig('../generators/swift-apollo-single-file/')
}
];

0 comments on commit 2d144ff

Please sign in to comment.