Skip to content

Commit

Permalink
Merge pull request #54 from OpenDive/graphql
Browse files Browse the repository at this point in the history
GraphQL Provider implementation
  • Loading branch information
MarcoDotIO authored Feb 5, 2024
2 parents 672b472 + 44a8d34 commit 752d1e1
Show file tree
Hide file tree
Showing 191 changed files with 11,536 additions and 178 deletions.
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "0.6.7"
}
},
{
"identity" : "apollo-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apollographql/apollo-ios.git",
"state" : {
"revision" : "7e28eb75e9970edaba346a3c1eab1f8fc479a04d",
"version" : "1.7.1"
}
},
{
"identity" : "bigint",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -63,6 +72,15 @@
"version" : "3.1.0"
}
},
{
"identity" : "sqlite.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stephencelis/SQLite.swift.git",
"state" : {
"revision" : "7a2e3cd27de56f6d396e84f63beefd0267b55ccb",
"version" : "0.14.1"
}
},
{
"identity" : "swiftyjson",
"kind" : "remoteSourceControl",
Expand Down
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ let package = Package(
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"),
.package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"),
.package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"),
.package(url: "https://github.com/auth0/JWTDecode.swift", from: "3.1.0")
.package(url: "https://github.com/auth0/JWTDecode.swift", from: "3.1.0"),
.package(url: "https://github.com/apollographql/apollo-ios.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
Expand All @@ -31,7 +32,8 @@ let package = Package(
.product(name: "AnyCodable", package: "AnyCodable"),
.product(name: "Bip39", package: "Bip39.swift"),
.product(name: "web3swift", package: "web3swift"),
.product(name: "JWTDecode", package: "JWTDecode.swift")
.product(name: "JWTDecode", package: "JWTDecode.swift"),
.product(name: "Apollo", package: "apollo-ios")
],
path: "Sources"
),
Expand Down
46 changes: 0 additions & 46 deletions Package@swift-5.5.swift

This file was deleted.

46 changes: 0 additions & 46 deletions Package@swift-5.6.swift

This file was deleted.

22 changes: 22 additions & 0 deletions Sources/SuiKit/Extensions/AnyHashable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// File.swift
//
//
// Created by Marcus Arnett on 1/17/24.
//

import Foundation

extension AnyHashable: ScalarType, OutputTypeConvertible, AnyScalarType {
public init(_jsonValue value: JSONValue) throws {
self = value
}

public static var _asOutputType: ApolloAPI.Selection.Field.OutputType {
.scalar(AnyHashable.self)
}

public var _jsonValue: ApolloAPI.JSONValue {
self
}
}
20 changes: 20 additions & 0 deletions Sources/SuiKit/Extensions/DateFormatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// File.swift
//
//
// Created by Marcus Arnett on 1/29/24.
//

import Foundation

extension DateFormatter {
static func unixTimestamp(from dateString: String) -> Int? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
guard let date = dateFormatter.date(from: dateString) else {
return nil
}
return Int(date.timeIntervalSince1970 * 1_000)
}
}
2 changes: 2 additions & 0 deletions Sources/SuiKit/Protocols/Sui/ConnectionProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public protocol ConnectionProtocol {
/// Optional URL for a faucet service to obtain tokens.
/// Default is nil, meaning no faucet is configured.
var faucet: String? { get }

var graphql: String? { get }

/// Optional URL for a WebSocket connection.
/// Default is nil, meaning no WebSocket is configured.
Expand Down
3 changes: 3 additions & 0 deletions Sources/SuiKit/Types/Enums/Errors/SuiError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,7 @@ public enum SuiError: Error, Equatable {

/// The Kiosk Data is invalid.
case invalidKioskData

/// The result for the GraphQL query is missing
case missingGraphQLData
}
2 changes: 1 addition & 1 deletion Sources/SuiKit/Types/Enums/Objects/ObjectOwner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Foundation
import SwiftyJSON

/// `ObjectOwner` represents the possible types of ownership for objects.
public enum ObjectOwner: KeyProtocol {
public enum ObjectOwner: KeyProtocol, Equatable {
/// Represents an object owned by an address. The associated value is a `String` containing the address.
case addressOwner(String)

Expand Down
46 changes: 45 additions & 1 deletion Sources/SuiKit/Types/Enums/Objects/RawData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,57 @@ import SwiftyJSON
///
/// This enum allows for either a `MoveObjectRaw` or `PackageRaw` data type, capturing
/// the raw information for either a Move object or a package object.
public enum RawData {
public enum RawData: Equatable {
/// Represents raw data for a Move object. The associated value is a `MoveObjectRaw` containing the Move object's raw data.
case moveObject(MoveObjectRaw)

/// Represents raw data for a package object. The associated value is a `PackageRaw` containing the package object's raw data.
case packageObject(PackageRaw)

public init(graphql: TryGetPastObjectQuery.Data.Object.AsMoveObject, version: String) {
self = .moveObject(
MoveObjectRaw(
bcsBytes: JSON(graphql.ifShowBcs!.contents!.bcs).stringValue,
hasPublicTransfer: graphql.ifShowBcs!.hasPublicTransfer,
type: graphql.ifShowBcs!.contents!.type.repr,
version: version
)
)
}

public init(graphql: GetOwnedObjectsQuery.Data.Address.ObjectConnection.Node.AsMoveObject, version: String) {
self = .moveObject(
MoveObjectRaw(
bcsBytes: JSON(graphql.ifShowBcs!.contents!.bcs).stringValue,
hasPublicTransfer: graphql.ifShowBcs!.hasPublicTransfer,
type: graphql.ifShowBcs!.contents!.type.repr,
version: version
)
)
}

public init(graphql: MultiGetObjectsQuery.Data.ObjectConnection.Node.AsMoveObject, version: String) {
self = .moveObject(
MoveObjectRaw(
bcsBytes: JSON(graphql.ifShowBcs!.contents!.bcs).stringValue,
hasPublicTransfer: graphql.ifShowBcs!.hasPublicTransfer,
type: graphql.ifShowBcs!.contents!.type.repr,
version: version
)
)
}

public init(graphql: GetObjectQuery.Data.Object.AsMoveObject, version: String) {
self = .moveObject(
MoveObjectRaw(
bcsBytes: JSON(graphql.ifShowBcs!.contents!.bcs).stringValue,
hasPublicTransfer: graphql.ifShowBcs!.hasPublicTransfer,
type: graphql.ifShowBcs!.contents!.type.repr,
version: version
)
)
}

/// Parses a `JSON` object to determine the type of raw data it contains and returns a corresponding `RawData` instance.
///
/// - Parameters:
Expand Down
58 changes: 57 additions & 1 deletion Sources/SuiKit/Types/Enums/Objects/SuiParsedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,69 @@ import SwiftyJSON
///
/// The enum handles two main types of parsed data: `MoveObject` and `MovePackage`, which represent
/// different kinds of data structures commonly used in Sui-related operations.
public enum SuiParsedData {
public enum SuiParsedData: Equatable {
/// Represents a parsed Move object. The associated value is a `MoveObject` that holds the parsed fields and other relevant data for a Move object.
case moveObject(MoveObject)

/// Represents a parsed Move package. The associated value is a `MovePackage` containing the disassembled or decomposed package information.
case movePackage(MovePackage)

public init(graphql: TryGetPastObjectQuery.Data.Object.AsMoveObject) {
let fields: [String: AnyHashable] = [
"data": graphql.ifShowContent!.contents!.data,
"layout": graphql.ifShowContent!.contents!.type.layout
]
self = .moveObject(
MoveObject(
fields: JSON(fields),
hasPublicTransfer: graphql.ifShowContent!.hasPublicTransfer,
type: graphql.ifShowContent!.contents!.type.repr
)
)
}

public init(graphql: GetOwnedObjectsQuery.Data.Address.ObjectConnection.Node.AsMoveObject) {
let fields: [String: AnyHashable] = [
"data": graphql.ifShowContent!.contents!.data,
"layout": graphql.ifShowContent!.contents!.type.layout
]
self = .moveObject(
MoveObject(
fields: JSON(fields),
hasPublicTransfer: graphql.ifShowContent!.hasPublicTransfer,
type: graphql.ifShowContent!.contents!.type.repr
)
)
}

public init(graphql: MultiGetObjectsQuery.Data.ObjectConnection.Node.AsMoveObject) {
let fields: [String: AnyHashable] = [
"data": graphql.ifShowContent!.contents!.data,
"layout": graphql.ifShowContent!.contents!.type.layout
]
self = .moveObject(
MoveObject(
fields: JSON(fields),
hasPublicTransfer: graphql.ifShowContent!.hasPublicTransfer,
type: graphql.ifShowContent!.contents!.type.repr
)
)
}

public init(graphql: GetObjectQuery.Data.Object.AsMoveObject) {
let fields: [String: AnyHashable] = [
"data": graphql.ifShowContent!.contents!.data,
"layout": graphql.ifShowContent!.contents!.type.layout
]
self = .moveObject(
MoveObject(
fields: JSON(fields),
hasPublicTransfer: graphql.ifShowContent!.hasPublicTransfer,
type: graphql.ifShowContent!.contents!.type.repr
)
)
}

/// Parses a JSON object to determine the type of parsed data and returns an instance of `SuiParsedData`.
///
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//

import Foundation
import SwiftyJSON

/// `SuiMoveFunctionArgType` represents the type of an argument in a Move function.
///
Expand All @@ -32,8 +33,34 @@ import Foundation
public enum SuiMoveFunctionArgType: Equatable {
/// The argument is a pure value and doesn't involve any object references.
case pure

/// The argument is an object. The kind of the object (immutable/mutable reference or by value) is specified.
case object(ObjectValueKind)

public init(graphql: GetMoveFunctionArgTypesQuery.Data.Object.AsMovePackage.Module.Function.Parameter) {
self = Self.fromHashable(hash: graphql.signature)
}

public init(graphql: RPC_MOVE_STRUCT_FIELDS.Field.Type_SelectionSet) {
self = Self.fromHashable(hash: graphql.signature)
}

private static func fromHashable(hash: AnyHashable) -> SuiMoveFunctionArgType {
let jsonGraphQL = JSON(hash)

if !(jsonGraphQL["body"]["datatype"].exists()) {
return .pure
}

if jsonGraphQL["ref"].stringValue == "&" {
return .object(.byImmutableReference)
}

if jsonGraphQL["ref"].stringValue == "&mut" {
return .object(.byMutableReference)
}

return .object(.byValue)
}
}

Loading

0 comments on commit 752d1e1

Please sign in to comment.