Skip to content

Instantly share code, notes, and snippets.

@dafurman
Created March 29, 2023 19:42
Show Gist options
  • Save dafurman/faf04069a8c5ed76354b9eab9c1b3328 to your computer and use it in GitHub Desktop.
Save dafurman/faf04069a8c5ed76354b9eab9c1b3328 to your computer and use it in GitHub Desktop.
Apollo GraphQLNullable Conveniences
import ApolloAPI
import Foundation
public extension EnumType {
var gqlEnum: GraphQLEnum<Self> {
GraphQLEnum(self)
}
/// Converts an `EnumType` into a `GraphQLNullable<GraphQLEnum<EnumType>>` for compatibility with GQL query and mutation inputs.
var gqlEnumNullable: GraphQLNullable<GraphQLEnum<Self>> {
.some(.case(self))
}
}
public extension Optional where Wrapped: EnumType {
/// Converts an optional `EnumType` into a `GraphQLEnum<EnumType>` for compatibility with GQL query and mutation inputs.
var gqlEnum: GraphQLEnum<Wrapped> {
if let self {
return .case(self)
}
return .unknown("")
}
/// Converts an optional `EnumType` into a `GraphQLNullable<GraphQLEnum<EnumType>>` for compatibility with GQL query and mutation inputs.
var gqlEnumNullable: GraphQLNullable<GraphQLEnum<Wrapped>> {
if let self {
return .some(.case(self))
}
return .none
}
}
public extension String {
/// Converts a `String` into a `GraphQLNullable<String>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
public extension Numeric {
/// Converts a `Numeric` into a `GraphQLNullable<Numeric>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
public extension Bool {
/// Converts a `Bool` into a `GraphQLNullable<Bool>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
public extension URL {
/// Converts a `URL` into a `GraphQLNullable<URL>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
public extension Array {
/// Converts an `Array<T>` into a `GraphQLNullable<Array<T>>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
public extension Array where Element: EnumType {
/// Converts an `Array<EnumType>` into a `GraphQLNullable<Array<GraphQLEnum<Element>>>` for compatibility with GQL query and mutation inputs.
var gqlEnumArrayNullable: GraphQLNullable<Array<GraphQLEnum<Element>>> {
gqlEnumArray.gqlNullable
}
/// Converts an `Array<EnumType>` into a `Array<GraphQLEnum<Element>>` for compatibility with GQL query and mutation inputs.
var gqlEnumArray: Array<GraphQLEnum<Element>> {
compactMap { $0.gqlEnum }
}
}
public extension Optional {
/// Converts an optional `Wrapped` into a `GraphQLNullable<Wrapped>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Wrapped> {
if let self {
return .some(self)
}
return .none
}
}
public extension InputObject {
/// Converts a `InputObject` into a `GraphQLNullable<InputObject>` for compatibility with GQL query and mutation inputs.
var gqlNullable: GraphQLNullable<Self> {
.some(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment