How to mock the ApolloClientProtocol, apollo-ios v1.0.6

Hello all, does anyone have any examples of how to mock the ApolloClientProtocol? Have been trying for a while now but cant get it to click, nor find any code examples seemingly doing this.

Im trying to get the previews and UITests to run currently (SwiftUI), I see theres a React { MockedProvider} and MockServer for Kotlin is there any equivalent in iOS?

I have read Issue 983 amongst others which has led me here.

Thanks for any help

Hi - here’s a very quick and dirty sample SPM project that mocks ApolloClientProtocol and stubs out the responses. Hope this helps, it should be enough to get you moving forward.

1 Like

Thanks for your help, I will have a go with that, much appreciated.

Hi calvincestari,

I have ApolloClient class as a dependency initializer for one of my class, when I use your MockApolloClientProtocl, I ws getting "Can not convert of type “MockApolloClient” to expected arguemnt type “ApolloClient” "

My class looks like this

class Test {
var apollo: ApolloClient?
public init(apollo: ApolloClient) {
self.apollo = apollo
}
}
I am trying to test the above class.
Any help is highly appreciated.

Thanks in advance

Hi @harinath - that error is because MockApolloClient is not the same as ApolloClient. They both conform to ApolloClientProtocol which has the function stubs for fetch, watch, perform, etc. but they are not the same type.

What you probably want is for your init function to accept an instance that conforms to ApolloClientProtocol, like public init(client: ApolloClientProtocol) {, then you can pass in an ApolloClient or MockApolloClient instance.

Hi @calvincestari This is great and working fine.

I have another problem with hiding types with GraphQLQuery/GraphQLMutation

class Test {

func fetchQuery(queryable: Queryable) {

var query = queryable.query

// Getting compile error saying "Type ‘any GraphQLQuery’ cannot confirm to ‘GraphQLQuery’ "
ApolloClient(url: URL(string: “”)).fetch(query: query)

}

}

protocol Queryable {
var query: any GraphQLQuery {get set}
}

I would highly appreciate if you could help me solve this error.

You’re running into this - Type 'any Protocol' cannot conform to 'Protocol' - Using Swift - Swift Forums

I went through the blog some time back, But I could not able to apply the solution here. If you have any thoughts please share.

Thanks in advance