swift 在Swift中实现的GraphQL数据结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 在Swift中实现的GraphQL数据结构相关的知识,希望对你有一定的参考价值。

import Foundation


protocol GQLNodeArgument {}
extension String: GQLNodeArgument {}
extension NSNumber: GQLNodeArgument {}


class GQLNode: StringLiteralConvertible, ArrayLiteralConvertible, Printable, DebugPrintable {
    let name: String?
    let arguments: [String : GQLNodeArgument]
    let properties: [GQLNode]
    
    var isRootNode: Bool {
        return self.name?.isEmpty ?? true
    }
    
    init(name: String, arguments: [String : GQLNodeArgument] = [:], properties: [GQLNode] = []) {
        for node in properties {
            assert(!node.isRootNode, "You cannot include a root node, or a node with an empty name as a child of another node.")
        }
        
        self.name = name
        self.arguments = arguments
        self.properties = properties
    }
    
    required convenience init(stringLiteral value: StringLiteralType) {
        self.init(name: value)
    }
    
    required convenience init(extendedGraphemeClusterLiteral value: StringLiteralType) {
        self.init(name: value)
    }
    
    required convenience init(unicodeScalarLiteral value: StringLiteralType) {
        self.init(name: value)
    }
    
    required convenience init(arrayLiteral elements: GQLNode...) {
        self.init(name: "", properties: elements)
    }
    
    
    func render(pretty: Bool = false, indentationLevel: Int = 0) -> String {
        var rendered: String = ""
        let indentationText: String
        if pretty {
            indentationText = "".join(Array(count: indentationLevel, repeatedValue: "    "))
        } else {
            indentationText = ""
        }
        
        rendered += indentationText
        
        if let name = self.name {
            rendered += name
        }
        
        if self.arguments.count > 0 {
            rendered += "("
            var argumentTexts: [String] = []
            for (name, argument) in self.arguments {
                argumentTexts.append("\(name): \(argument)")
            }
            rendered += ", ".join(argumentTexts)
            if pretty {
                rendered += ") "
            } else {
                rendered += ")"
            }
        }
        
        if self.properties.count > 0 {
            rendered += "{"
            
            if pretty {
                rendered += "\n"
            }
            
            var propertyTexts: [String] = []
            for property in self.properties {
                propertyTexts.append(property.render(pretty: pretty, indentationLevel: indentationLevel + 1))
            }
            if pretty {
                rendered += ",\n".join(propertyTexts)
            } else {
                rendered += ",".join(propertyTexts)
            }
            
            if pretty {
                rendered += "\n"
            }
            
            rendered += indentationText + "}"
        }
        
        return rendered
    }
    
    var description: String {
        return self.render(pretty: true)
    }
    
    var debugDescription: String {
        return "GQLNode(\(self.render(pretty: true)))"
    }
}



let query: GQLNode = [
    GQLNode(name: "user", arguments: ["id" : 3500401], properties: [
        "id",
        "name",
        "isViewerFriend",
        GQLNode(name: "profilePicture", arguments: ["size" : 50], properties: [
            "uri",
            "width",
            "height",
        ]),
    ])
]

query.render(pretty: true)
query.render(pretty: false)

以上是关于swift 在Swift中实现的GraphQL数据结构的主要内容,如果未能解决你的问题,请参考以下文章

swift 纯粹的Swift中实现的极简主义响应链

调用 Swift 协议扩展方法而不是子类中实现的方法

具有最终类的多态性,可在 swift 中实现关联类型协议

使用 Swift 协议和扩展覆盖对象方法

Swift 3:UISwipeGestureRecognizer 适用于 View 但不适用于 ViewContainer

如何在 Swift 中读取视频文件的第一个和最后一个 64kb