---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# NowGraphQLService class - iOS

# NowGraphQLService class - iOS {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read

The NowGraphQLService class provides functions that enable you to
make requests using GraphQL queries against data on your ServiceNow
instance through its GraphQL REST API.
{#NowGraphQLServiceiOSAPI__table_vx2_klw_5pb__entry__3}

| Name | Type | Description |
|-|-|-|
| configuration | [NowServiceConfiguration](https://servicenow-prod.fluidtopics.net/A6W2zQ0fOO_VQ9lbo3VKwQ#NowServiceConfigurationiOSStruct "The NowServiceConfiguration structure defines configuration information for a feature service.") | Configuration settings provided when the service was initialized. |
[Table 1. Properties]

{#NowGraphQLServiceiOSAPI__table_vx2_klw_5pb}

## NowGraphQLService - execute(query: GraphQLQuery) async throws {#ariaid-title2}

Executes the specified GraphQL query.
{#NGQLServ-execute-async_S__table_sfm_c4h_ppb__entry__3}

| Name | Type | Description |
|-|-|-|
| query | String | GraphQL query to execute. |
[Table 2. Parameters]

{#NGQLServ-execute-async_S__table_sfm_c4h_ppb} {#NGQLServ-execute-async_S__table_tfm_c4h_ppb__entry__2}

| Type | Description |
|-|-|
| Data | Returned when the method is successful. Query data generated from the specified query. |
| NowDataError | Thrown when the method fails. * `accessToken(AccessTokenProviderError)` * AccessTokenProviderError: The access token provider's error code or message. * accessTokenRetrievalFailed * userSessionError(_ error: Error) Thrown when there is an error in the access token. * `attachmentValidation` Thrown when an attachment fails validation. * `badResponse(statusCode: HTTPStatusCode)` * HTTPStatusCode: Status code received from the instance. Thrown when a request returns an unexpected response * `cannotDecodeModel(DecodingError)` * DecodingError: Decoding error detected. Thrown when a Codable model cannot be decoded from JSON. * `cannotDecodeProperty(type: Any, from: String)` * type: Wrapped type to decode from a string. * from: String to decode to the specified type. Thrown when a string-wrapped value cannot be decoded from JSON. * `cannotEncodeModel(EncodingError)` * EncodingError: Encoding error detected. Thrown when a Codable model cannot be encoded to JSON. * `cannotParseResponse` Thrown when a response from the instance cannot be parsed into its expected format. * `invalidURL` 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. * `missingAttachmentMetadata` Thrown when the attachment metadata header is missing. * `missingServiceConfiguration` Thrown when an expected service configuration is missing. * `missingSysID` Thrown when an expected sys_id parameter is missing. * `network(NetworkServiceError)` * genericError(String) * operationCanceled * serviceDisabled * serverError(Error) * systemError(Error) Thrown when a network service encountered an error. {#NGQLServ-execute-async_S__NowDataErroriOS-ul-enum} |
[Table 3. Returns]

{#NGQLServ-execute-async_S__table_tfm_c4h_ppb}  
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\>)) {#ariaid-title3}

Executes the specified GraphQL query.
{#NGQLServ-execute_S_O__table_sfm_c4h_ppb__entry__3}

| 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 {#NGQLServ-execute_S_O__ul_tst_1qh_ppb} |
[Table 4. Parameters]

{#NGQLServ-execute_S_O__table_sfm_c4h_ppb} {#NGQLServ-execute_S_O__table_tfm_c4h_ppb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 5. Returns]

{#NGQLServ-execute_S_O__table_tfm_c4h_ppb}  
<br />

    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) {#ariaid-title4}

Creates a NowGraphQLService based on the specified parameters.
{#NGQLServ-init_O_O__table_k4t_mnh_ppb__entry__3}

| Name | Type | Description |
|-|-|-|
| configuration | [NowServiceConfiguration](https://servicenow-prod.fluidtopics.net/A6W2zQ0fOO_VQ9lbo3VKwQ#NowServiceConfigurationiOSStruct "The NowServiceConfiguration structure defines configuration information for a feature service.") | 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 |
[Table 6. Parameters]

{#NGQLServ-init_O_O__table_k4t_mnh_ppb}  
<br />

```

```

## NowGraphQLService - publisher(for query: GraphQLQuery) {#ariaid-title5}

Creates a publisher to request GraphQL data.
Note:  
This method has been deprecated. You should use the async/await implementation of the method instead.
{#NGQLServ-publisher_S__table_jsv_5wh_ppb__entry__3}

| Name | Type | Description |
|-|-|-|
| for query | GraphQLQuery | GraphQL query string to execute. |
[Table 7. Parameters]

{#NGQLServ-publisher_S__table_jsv_5wh_ppb} {#NGQLServ-publisher_S__table_ksv_5wh_ppb__entry__2}

| Type | Description |
|-|-|
| AnyPublisher\<Data, NowDataError\> | Success: GraphQL data set. Failure: NowDataError |
[Table 8. Returns]

{#NGQLServ-publisher_S__table_ksv_5wh_ppb}  
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()
    }


