diff --git a/dev-test/githunt/API.swift b/dev-test/githunt/API.swift new file mode 100644 index 00000000000..976357d5dc9 --- /dev/null +++ b/dev-test/githunt/API.swift @@ -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" +} + diff --git a/generators/swift-apollo-single-file/config.json b/generators/swift-apollo-single-file/config.json new file mode 100644 index 00000000000..3d42dae92d4 --- /dev/null +++ b/generators/swift-apollo-single-file/config.json @@ -0,0 +1,11 @@ +{ + "strategy": "SINGLE_FILE", + "template": "./template.handlebars", + "primitives": { + "String": "String", + "Int": "Int", + "Float": "Float", + "Boolean": "Bool", + "ID": "String" + } +} \ No newline at end of file diff --git a/generators/swift-apollo-single-file/template.handlebars b/generators/swift-apollo-single-file/template.handlebars new file mode 100644 index 00000000000..21453070ffc --- /dev/null +++ b/generators/swift-apollo-single-file/template.handlebars @@ -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 ~}} diff --git a/src/fragment-handler.ts b/src/fragment-handler.ts index 9b4435fb478..25caa8c151b 100644 --- a/src/fragment-handler.ts +++ b/src/fragment-handler.ts @@ -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; @@ -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 = { diff --git a/src/interfaces.ts b/src/interfaces.ts index 61ba46e6285..7b3863ddaba 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -31,6 +31,7 @@ export interface CodegenDocument { isSubscription: boolean; isFragment: boolean; name: string; + document: string; rawName: string; innerTypes: Model[]; variables: Field[]; diff --git a/src/operation-handler.ts b/src/operation-handler.ts index 8222a62ee69..798fcdd6c60 100644 --- a/src/operation-handler.ts +++ b/src/operation-handler.ts @@ -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', @@ -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; @@ -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); diff --git a/src/templates.ts b/src/templates.ts index 33fc2c132fc..7030438ccc9 100644 --- a/src/templates.ts +++ b/src/templates.ts @@ -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/') } ];