Project target name: GraphQLDemo
xcode -> add Apollo package dependency
Select project in xcode and install CLI for Apollo (via context menu)
apollo-ios-cli must added
switch to terminal, navigate to project root dir
./apollo-ios-cli init --schema-namespace MyAPI --target-name GraphQLDemo --module-type embeddedInTarget
apollo-codegen-config.json must generated
edit apollo-codegen-config.json and add section:
"schemaDownloadConfiguration": {
"downloadMethod": {
"introspection": {
"endpointURL": "http://localhost:4000/graphql",
"httpMethod": {
"POST": {}
},
"includeDeprecatedInputValues": false,
"outputFormat": "SDL"
}
},
"downloadTimeout": 60,
"headers": [],
"outputPath": "./graphql/schema.graphqls"
}
execute
./apollo-ios-cli fetch-schema
./graphql/schema.graphqls generated
create new file
./graphql/query.graphql
query DemoRequest {
authorById(id: 1) {
id
firstName
lastName
posts {
id
title
text
views
}
}
testString
}
execute
./apollo-ios-cli generate
GraphQLDemo/MyAPI generated
Drag MyAPI to xcode project
import Apollo
...
let store = ApolloStore(cache: InMemoryNormalizedCache())
let interceptorProvider = DefaultInterceptorProvider(store: store)
let networkTransport = RequestChainNetworkTransport(
interceptorProvider: interceptorProvider,
endpointURL: URL(string: "http://localhost:4000/graphql")!,
autoPersistQueries: true
)
let client = ApolloClient(networkTransport: networkTransport, store: store)
let r = MyAPI.DemoRequestQuery()
client.fetch(query: r) { result in
guard let data = try? result.get().data else {
print(result)
return
}
print(data.testString)
}
in my case fetch failed with error
failure(Apollo.ResponseCodeInterceptor.ResponseCodeError.invalidResponseCode(response: Optional(<NSHTTPURLResponse: 0x600000226960> { URL: http://localhost:4000/graphql } { Status Code: 400,
as a workaround :
struct NetworkInterceptorProvider: InterceptorProvider {
private let store: ApolloStore
private let client: URLSessionClient
init(store: ApolloStore, client: URLSessionClient) {
self.store = store
self.client = client
}
func interceptors<Operation: GraphQLOperation>(for operation: Operation) -> [ApolloInterceptor] {
return [
MaxRetryInterceptor(),
CacheReadInterceptor(store: self.store),
NetworkFetchInterceptor(client: self.client),
ResponseCodeInterceptor(),
JSONResponseParsingInterceptor(),
AutomaticPersistedQueryInterceptor(),
CacheWriteInterceptor(store: self.store)
]
}
}
...
let store = ApolloStore(cache: InMemoryNormalizedCache())
// let interceptorProvider = DefaultInterceptorProvider(store: store)
let cl = URLSessionClient()
let interceptorProvider = NetworkInterceptorProvider(store: store, client: cl)
let networkTransport = RequestChainNetworkTransport(
interceptorProvider: interceptorProvider,
endpointURL: URL(string: "http://localhost:4000/graphql")!,
autoPersistQueries: false
)