Skip to content

Commit

Permalink
feat(gql): support operationName (datahub-project#10210)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 authored and sleeperdeep committed Jun 25, 2024
1 parent 689e3b8 commit 72b1f99
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private GraphQLEngine(

public ExecutionResult execute(
@Nonnull final String query,
@Nullable final String operationName,
@Nullable final Map<String, Object> variables,
@Nonnull final QueryContext context) {
/*
Expand All @@ -100,6 +101,7 @@ public ExecutionResult execute(
ExecutionInput executionInput =
ExecutionInput.newExecutionInput()
.query(query)
.operationName(operationName)
.variables(variables)
.dataLoaderRegistry(register)
.context(context)
Expand Down
10 changes: 8 additions & 2 deletions metadata-ingestion/src/datahub/ingestion/graph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,15 +859,21 @@ def get_aspect_counts(self, aspect: str, urn_like: Optional[str] = None) -> int:
results = self._post_generic(self._aspect_count_endpoint, args)
return results["value"]

def execute_graphql(self, query: str, variables: Optional[Dict] = None) -> Dict:
def execute_graphql(
self,
query: str,
variables: Optional[Dict] = None,
operation_name: Optional[str] = None,
) -> Dict:
url = f"{self.config.server}/api/graphql"

body: Dict = {
"query": query,
}

if variables:
body["variables"] = variables
if operation_name:
body["operationName"] = operation_name

logger.debug(
f"Executing graphql query: {query} with variables: {json.dumps(variables)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ CompletableFuture<ResponseEntity<String>> postGraphQL(HttpEntity<String> httpEnt
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}

/*
* Extract "operationName" field
*/
JsonNode operationNameJson = bodyJson.get("operationName");
final String operationName =
(operationNameJson != null && !operationNameJson.isNull())
? operationNameJson.asText()
: null;

/*
* Extract "variables" map
*/
Expand Down Expand Up @@ -118,7 +127,8 @@ CompletableFuture<ResponseEntity<String>> postGraphQL(HttpEntity<String> httpEnt
/*
* Execute GraphQL Query
*/
ExecutionResult executionResult = _engine.execute(queryJson.asText(), variables, context);
ExecutionResult executionResult =
_engine.execute(queryJson.asText(), operationName, variables, context);

if (executionResult.getErrors().size() != 0) {
// There were GraphQL errors. Report in error logs.
Expand Down

0 comments on commit 72b1f99

Please sign in to comment.