# How to create custom scalar type for ClosedRange when using Apollo for iOS?

**URL:** <https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137>\
**Category:** Client SDKs\
**Tags:** client, ios\
**Created:** [January 19, 2024, 7:17pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137 "2024-01-19T19:17:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cameroncooke](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/cameroncooke/32/4867_2.png) [@cameroncooke](https://community.apollographql.com/u/cameroncooke)\
**Post date:** [January 19, 2024, 7:17pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137/1 "2024-01-19T19:17:55Z")

</div>

I’ve implemented a Swift GraphQL web service using Vapor and Graphiti which has a custom schema for `ClosedRange<Dobule>` which is encoded into an array of two Doubles `[5, 8]`.

On the client side I’m using Apollo for iOS and have tried to implement the custom scehma decoding by conforming `ClosedRange<Dobule>` to `CustomScalarType` but I get the error:

> Conditional conformance of type ‘ClosedRange’ to protocol ‘CustomScalarType’ does not imply conformance to inherited protocol ‘AnyHashableConvertible’

```swift
// @generated
// This file was automatically generated and should not be edited.

import ApolloAPI
import Foundation

public extension ClientAPI {
  typealias DoubleClosedRange = ClosedRange<Double>
}

extension ClosedRange<Double>: CustomScalarType {
    public init(_jsonValue value: JSONValue) throws {
        guard let elements = value as? [Double] else {
            throw JSONDecodingError.couldNotConvert(value: value, to: [Double].self)
        }

        self = ClosedRange(uncheckedBounds: (lower: elements[0], upper: elements[1]))
    }

    public var _jsonValue: JSONValue {
        [lowerBound, upperBound]
    }
}

```

I’m not sure I really understand the error, I’ve created conformance for other Foundation types like `URL` in the same way without issues.

---

<div class="post-metadata">

**Author:** ![calvincestari](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/calvincestari/32/339_2.png) [@calvincestari](https://community.apollographql.com/u/calvincestari)\
**Post date:** [January 19, 2024, 9:02pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137/2 "2024-01-19T21:02:07Z")

</div>

Hi @cameroncooke

Being generic `ClosedRange` itself has no unit type, it’s `Bound` that does instead. I believe it’s this that is tripping you up and you’ll need to provide conformance for all the required protocols narrowing the conformance to `Double`.

This builds but there might be some additional work needed in some of the empty extensions:

```auto
extension ClosedRange: AnyHashableConvertible where Bound == Double {}
extension ClosedRange: GraphQLOperationVariableValue where Bound == Double {}
extension ClosedRange: OutputTypeConvertible where Bound == Double {}
extension ClosedRange: AnyScalarType where Bound == Double {}
extension ClosedRange: CustomScalarType where Bound == Double {}

extension ClosedRange: JSONEncodable where Bound == Double {
  public var _jsonValue: JSONValue {
      [lowerBound, upperBound]
  }
}

extension ClosedRange: JSONDecodable where Bound == Double {
  public init(_jsonValue value: JSONValue) throws {
      guard let elements = value as? [Double] else {
          throw JSONDecodingError.couldNotConvert(value: value, to: [Double].self)
      }

      self = ClosedRange(uncheckedBounds: (lower: elements[0], upper: elements[1]))
  }
}

```

---

<div class="post-metadata">

**Author:** ![calvincestari](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/calvincestari/32/339_2.png) [@calvincestari](https://community.apollographql.com/u/calvincestari)\
**Post date:** [January 19, 2024, 9:05pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137/3 "2024-01-19T21:05:46Z")

</div>

I’m not sure why `extension ClosedRange: CustomScalarType where Bound == Double {..}` doesn’t work. I assume it wants you to explicitly state the conformance?

---

<div class="post-metadata">

**Author:** ![cameroncooke](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/cameroncooke/32/4867_2.png) [@cameroncooke](https://community.apollographql.com/u/cameroncooke)\
**Post date:** [January 19, 2024, 9:38pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137/4 "2024-01-19T21:38:39Z")

</div>

Amazing, was starting to pull my hair out, thank you so much, that works!

I did try:

```swift
extension Double: CustomScalarType {
   ...
}

```

I thought that should work but maybe my understanding of conditional conformance was flawed.

---

<div class="post-metadata">

**Author:** ![cameroncooke](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/cameroncooke/32/4867_2.png) [@cameroncooke](https://community.apollographql.com/u/cameroncooke)\
**Post date:** [January 19, 2024, 9:40pm UTC](https://community.apollographql.com/t/how-to-create-custom-scalar-type-for-closedrange-when-using-apollo-for-ios/7137/5 "2024-01-19T21:40:25Z")

</div>

We really need an open-source package of common Swift scalars conformances like URL, Range etc. I believe the JS community has a similar package.
