NowGraphQLService class- iOS
The NowGraphQLService class provides functions that enable you to make requests using GraphQL queries against data on your ServiceNow instance through itsGraphQL REST API.
| Name | Type | Description |
|---|---|---|
| configuration | NowServiceConfiguration | Configuration settings provided when the service was initialized. |
Parent Topic:Mobile SDK - iOS
NowGraphQLService - execute(query: GraphQLQuery) async throws
Executes the specified GraphQL query.
| Name | Type | Description |
|---|---|---|
| query | String | GraphQL query to execute. |
| Type | Description |
|---|---|
| Data | Returned when the method is successful. Query data generated from the specified query. |
| NowDataError | Thrown when the method fails. -
Thrown when an attachment fails validation.
Thrown when a response from the instance cannot be parsed into its expected format.
Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.
Thrown when the attachment metadata header is missing.
Thrown when an expected service configuration is missing.
Thrown when an expected sys_id parameter is missing.
|
The following code example shows how to call this method.
private func fetchArticles() async throws -> [Article] {
guard let graphQLService = graphQLService else {
throw ArticleListError.invalidGraphQLService
}
do {
let data = try await graphQLService.execute(GraphQLQuery(query: recentlyPublishedQuery))
let articles = try dataToArticleList(data)
} catch let error as NowDataError {
throw ArticleListError.fetchError(error)
} catch {
throw error
}
}
private let recentlyPublishedQuery =
"""
{
GlideRecord_Query {
kb_knowledge(queryConditions: "active=true^ORDERBYpublishedDESC" pagination: { limit: 10, offset: 0 }) {
_results {
sys_id {
value
},
number {
displayValue
},
short_description {
displayValue
},
author {
displayValue
}
published {
displayValue
}
}
}
}
}
"""
NowGraphQLService - execute(query: GraphQLQuery, completion: @escaping (Result<Data, NowDataError>))
Executes the specified GraphQL query.
| Name | Type | Description |
|---|---|---|
| query | String | GraphQL query to execute. |
| completion | @escaping \(Result<Data, NowDataError>\) | Completion handler to execute after the query is complete. Return values: - Success: Returns query data. - Error: NowDataError |
| Type | Description |
|---|---|
| None |
gqlService.execute(GraphQLQuery(query: myQuery)) { result in
switch result {
case .success(let data):
// use data
case .failure(let error):
// handle error
}
}
NowGraphQLService - init(configuration: NowServiceConfiguration, coreServiceProvider: NowCoreServiceProviding)
Creates a NowGraphQLService based on the specified parameters.
| Name | Type | Description |
|---|---|---|
| configuration | NowServiceConfiguration | Configuration parameters to use when initializing the service, such as the bundle ID of the application integrating with the service and the URL of the ServiceNow instance that the NowGraphQLService wants to access. |
| coreServiceProvider | NowCoreServiceProviding | Optional. Core service provider.Note: Currently this parameter is required even though it is shown as optional. Default: nil |
NowGraphQLService - publisher(for query: GraphQLQuery)
Creates a publisher to request GraphQL data.
Note: This method has been deprecated. You should use the async/await implementation of the method instead.
| Name | Type | Description |
|---|---|---|
| for query | GraphQLQuery | GraphQL query string to execute. |
| Type | Description |
|---|---|
| AnyPublisher<Data, NowDataError> | Success: GraphQL data set.Failure: NowDataError |
The following code example shows how to call this function.
private func fetchArticles() -> AnyPublisher<[Article], ArticleListError> {
guard let graphQLService = graphQLService else {
return Fail(error: .invalidGraphQLService).eraseToAnyPublisher()
}
return graphQLService.publisher(for: GraphQLQuery(query: recentlyPublishedQuery))
.mapError({ dataError -> ArticleListError in
.fetchError(dataError)
})
.flatMap(dataToArticleList)
.eraseToAnyPublisher()
}