New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when adding additional type to interface #400

Open
koenpunt opened this Issue Nov 5, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@koenpunt

koenpunt commented Nov 5, 2018

We have generated a Swift API for a schema that includes an interface.

When we now add an additional type that implements this interface to the server, the app crashes. This can of course be fixed by regenerating the api, but this doesn't help for apps already running on a user's device.

What I think should happen is that the API should ignore any type that is not available in the possibleTypes array.

This can be achieved by making GraphQLSelectionSet.init a failable initializer, in which we then check if the possibleTypes array contains the __typename and otherwise return nil. To remove the nil entries from a result set we can simply call .compactMap { $0 }.

@koenpunt

This comment has been minimized.

koenpunt commented Nov 5, 2018

Another option is to only update the generator, and filter out any ResultMap that has an unknown typename

For an array of objects that would be something like this;

public var nodes: [Node] {
  get {
    return (resultMap["nodes"] as! [ResultMap]).map { (value: ResultMap) -> Node in Node(unsafeResultMap: value) }.filter({
        Node.possibleTypes.contains($0.__typename)
    })
  }
  set {
    resultMap.updateValue(newValue.map { (value: Node) -> ResultMap in value.resultMap }, forKey: "nodes")
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment