{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":246,"path":"api-reference/cllent-mobile-api-reference/NowGraphQLServiceiOSAPI.md","title":"NowGraphQLService class - iOS","bundle":null,"doc_type":null,"release":"australia","body":"\n# NowGraphQLService class- iOS\n\nThe NowGraphQLService class provides functions that enable you to make requests using GraphQL queries against data on your ServiceNow instance through itsGraphQL REST API.\n\n|Name|Type|Description|\n|----|----|-----------|\n|configuration|[NowServiceConfiguration](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowServiceConfigurationiOSStruct.md)|Configuration settings provided when the service was initialized.|\n\n**Parent Topic:**[Mobile SDK - iOS](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/MobileSDKiOSAPI.md)\n\n## NowGraphQLService - execute\\(query: GraphQLQuery\\) async throws\n\nExecutes the specified GraphQL query.\n\n|Name|Type|Description|\n|----|----|-----------|\n|query|String|GraphQL query to execute.|\n\n<table id=\"table_tfm_c4h_ppb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nData\n\n</td><td>\n\nReturned when the method is successful. Query data generated from the specified query.\n\n</td></tr><tr><td>\n\nNowDataError\n\n</td><td>\n\nThrown when the method fails. -   `accessToken(AccessTokenProviderError)`\n\n    -   **AccessTokenProviderError**: The access token provider's error code or message.\n        -   accessTokenRetrievalFailed\n        -   userSessionError\\(\\_ error: Error\\)\nThrown when there is an error in the access token.\n\n-   `attachmentValidation`\n\nThrown when an attachment fails validation.\n\n-   `badResponse(statusCode: HTTPStatusCode)`\n\n    -   **HTTPStatusCode**: Status code received from the instance.\nThrown when a request returns an unexpected response\n\n-   `cannotDecodeModel(DecodingError)`\n\n    -   **DecodingError**: Decoding error detected.\nThrown when a Codable model cannot be decoded from JSON.\n\n-   `cannotDecodeProperty(type: Any, from: String)`\n\n    -   **type**: Wrapped type to decode from a string.\n    -   **from**: String to decode to the specified type.\nThrown when a string-wrapped value cannot be decoded from JSON.\n\n-   `cannotEncodeModel(EncodingError)`\n\n    -   **EncodingError**: Encoding error detected.\nThrown when a Codable model cannot be encoded to JSON.\n\n-   `cannotParseResponse`\n\nThrown when a response from the instance cannot be parsed into its expected format.\n\n-   `invalidURL`\n\nThrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.\n\n-   `missingAttachmentMetadata`\n\nThrown when the attachment metadata header is missing.\n\n-   `missingServiceConfiguration`\n\nThrown when an expected service configuration is missing.\n\n-   `missingSysID`\n\nThrown when an expected sys\\_id parameter is missing.\n\n-   `network(NetworkServiceError)`\n\n    -   genericError\\(String\\)\n    -   operationCanceled\n    -   service​Disabled\n    -   serverError\\(Error\\)\n    -   systemError\\(Error\\)\nThrown when a network service encountered an error.\n\n\n</td></tr></tbody>\n</table>The following code example shows how to call this method.\n\n```\nprivate func fetchArticles() async throws -> [Article] { \n    guard let graphQLService = graphQLService else { \n        throw ArticleListError.invalidGraphQLService \n    }         \n\n    do {\n        let data = try await graphQLService.execute(GraphQLQuery(query: recentlyPublishedQuery))\n        let articles = try dataToArticleList(data)\n    } catch let error as NowDataError {\n        throw ArticleListError.fetchError(error)\n    } catch {\n        throw error\n    }\n} \n  \nprivate let recentlyPublishedQuery = \n\"\"\" \n\n{\n  GlideRecord_Query {\n    kb_knowledge(queryConditions: \"active=true^ORDERBYpublishedDESC\" pagination: { limit: 10, offset: 0 }) {\n      _results { \n        sys_id {\n          value\n        },\n        number {\n          displayValue\n        },\n        short_description {\n          displayValue\n        },\n        author {\n          displayValue\n        }\n        published {\n          displayValue\n        }\n      }\n    }\n  }\n}\n\"\"\"\n```\n\n## NowGraphQLService - execute\\(query: GraphQLQuery, completion: @escaping \\(Result&lt;Data, NowDataError&gt;\\)\\)\n\nExecutes the specified GraphQL query.\n\n<table id=\"table_sfm_c4h_ppb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nquery\n\n</td><td>\n\nString\n\n</td><td>\n\nGraphQL query to execute.\n\n</td></tr><tr><td>\n\ncompletion\n\n</td><td>\n\n@escaping \\(Result&lt;Data, NowDataError&gt;\\)\n\n</td><td>\n\nCompletion handler to execute after the query is complete. Return values:\n\n-   Success: Returns query data.\n-   Error: NowDataError\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\n```\ngqlService.execute(GraphQLQuery(query: myQuery)) { result in \n  switch result { \n  case .success(let data): \n    // use data \n  case .failure(let error): \n    // handle error \n  } \n}\n```\n\n## NowGraphQLService - init\\(configuration: NowServiceConfiguration, coreServiceProvider: NowCoreServiceProviding\\)\n\nCreates a NowGraphQLService based on the specified parameters.\n\n<table id=\"table_k4t_mnh_ppb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nconfiguration\n\n</td><td>\n\n[NowServiceConfiguration](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowServiceConfigurationiOSStruct.md)\n\n</td><td>\n\nConfiguration 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.\n\n</td></tr><tr><td>\n\ncoreServiceProvider\n\n</td><td>\n\nNowCoreServiceProviding\n\n</td><td>\n\nOptional. Core service provider.**Note:** Currently this parameter is required even though it is shown as optional.\n\nDefault: nil\n\n</td></tr></tbody>\n</table>```\n\n```\n\n## NowGraphQLService - publisher\\(for query: GraphQLQuery\\)\n\nCreates a publisher to request GraphQL data.\n\n**Note:** This method has been deprecated. You should use the async/await implementation of the method instead.\n\n|Name|Type|Description|\n|----|----|-----------|\n|for query|GraphQLQuery|GraphQL query string to execute.|\n\n<table id=\"table_ksv_5wh_ppb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nAnyPublisher&lt;Data, NowDataError&gt;\n\n</td><td>\n\nSuccess: GraphQL data set.Failure: NowDataError\n\n</td></tr></tbody>\n</table>The following code example shows how to call this function.\n\n```\nprivate func fetchArticles() -> AnyPublisher<[Article], ArticleListError> {\n  guard let graphQLService = graphQLService else {\n    return Fail(error: .invalidGraphQLService).eraseToAnyPublisher()\n  }\n        \n  return graphQLService.publisher(for: GraphQLQuery(query: recentlyPublishedQuery))\n    .mapError({ dataError -> ArticleListError in\n      .fetchError(dataError)\n    })\n    .flatMap(dataToArticleList)\n    .eraseToAnyPublisher()\n}\n```\n\n"}}