Skip to content

Commit

Permalink
[GraphQL] Rename "graphql_object_list" to "graphql_object_to_id_map" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
parthpuri-elastic authored Apr 24, 2024
1 parent a06e067 commit 733b112
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
20 changes: 11 additions & 9 deletions connectors/sources/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, configuration):
self.authentication_method = self.configuration["authentication_method"]
self.pagination_model = self.configuration["pagination_model"]
self.pagination_key = self.configuration["pagination_key"]
self.graphql_object_list = {}
self.graphql_object_to_id_map = {}
self.valid_objects = []
self.variables = {}
self.headers = {}
Expand Down Expand Up @@ -120,15 +120,15 @@ def session(self):
)

def extract_graphql_data_items(self, data):
"""Returns sub objects from the response based on graphql_object_list
"""Returns sub objects from the response based on graphql_object_to_id_map
Args:
data (dict): data to extract
Yields:
dictionary/list: Documents from the response
"""
for keys, field_id in self.graphql_object_list.items():
for keys, field_id in self.graphql_object_to_id_map.items():
current_level_data = data
key_list = keys.split(".")

Expand Down Expand Up @@ -375,7 +375,7 @@ def get_default_configuration(cls):
"type": "str",
"required": False,
},
"graphql_object_list": {
"graphql_object_to_id_map": {
"display": "textarea",
"label": "GraphQL Objects to ID mapping",
"order": 9,
Expand Down Expand Up @@ -481,7 +481,9 @@ async def validate_config(self):

headers = self.graphql_client.configuration["headers"]
graphql_variables = self.graphql_client.configuration["graphql_variables"]
graphql_object_list = self.graphql_client.configuration["graphql_object_list"]
graphql_object_to_id_map = self.graphql_client.configuration[
"graphql_object_to_id_map"
]
invalid_json_msg = (
"GraphQL {field} must be in valid JSON format. Exception: {exception}"
)
Expand All @@ -500,10 +502,10 @@ async def validate_config(self):
msg = invalid_json_msg.format(field="Variables", exception=exception)
raise ConfigurableFieldValueError(msg) from exception

if graphql_object_list:
if graphql_object_to_id_map:
try:
self.graphql_client.graphql_object_list = json.loads(
graphql_object_list
self.graphql_client.graphql_object_to_id_map = json.loads(
graphql_object_to_id_map
)
except Exception as exception:
msg = invalid_json_msg.format(
Expand All @@ -517,7 +519,7 @@ async def validate_config(self):
for (
graphql_object,
graphql_field_id,
) in self.graphql_client.graphql_object_list.items():
) in self.graphql_client.graphql_object_to_id_map.items():
object_present, id_present = self.check_field_existence(
ast=ast,
field_path=graphql_object,
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ A json of key value pairs of variables used in the GraphQL query. The connector

Example: If the GraphQL query is `query getUser($id: ID!) { user(id: $id) { name } }` and the value of `graphql_variables` is `{"id": "123"}`, the final query will be `query getUser { user(id: "123") { name } }` and the connector will make a GraphQL query to the source with the modified query.

#### `graphql_object_list` (required)
#### `graphql_object_to_id_map` (required)

A JSON mapping between GraphQL response objects to index and their ID fields. The connector will fetch data for each object (JSON key) and use the provided ID field (JSON value) to index the object into Elasticsearch. The connector will index all fields for each object specified in the mapping. Use a dot `(.)` notation to specify the full path from the root of the GraphQL response to the desired object.

Expand Down
2 changes: 1 addition & 1 deletion tests/sources/fixtures/graphql/connector.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"type": "str",
"required": false
},
"graphql_object_list": {
"graphql_object_to_id_map": {
"label": "GraphQL Objects to ID mapping",
"order": 9,
"type": "str",
Expand Down
22 changes: 11 additions & 11 deletions tests/sources/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ async def create_graphql_source(
headers=None,
graphql_variables=None,
graphql_query="{users {name {firstName } } }",
graphql_object_list='{"users": "id"}',
graphql_object_to_id_map='{"users": "id"}',
):
async with create_source(
GraphQLDataSource,
http_endpoint="http://127.0.0.1:1234",
authentication_method="none",
graphql_query=graphql_query,
graphql_object_list=graphql_object_list,
graphql_object_to_id_map=graphql_object_to_id_map,
headers=headers,
graphql_variables=graphql_variables,
) as source:
Expand Down Expand Up @@ -88,7 +88,7 @@ async def create_graphql_source(
async def test_extract_graphql_data_items(object_list, data, expected_result):
actual_response = []
async with create_graphql_source() as source:
source.graphql_client.graphql_object_list = object_list
source.graphql_client.graphql_object_to_id_map = object_list
for doc in source.graphql_client.extract_graphql_data_items(data):
actual_response.append(doc)
assert actual_response == expected_result
Expand Down Expand Up @@ -244,7 +244,7 @@ async def test_fetch_data():
actual_response = []
async with create_graphql_source() as source:
source.graphql_client.pagination_model = "no_pagination"
source.graphql_client.graphql_object_list = {"users": "id"}
source.graphql_client.graphql_object_to_id_map = {"users": "id"}
source.graphql_client.post = AsyncMock(
return_value={
"users": [{"id": "1", "name": {"firstName": "xyz"}, "_id": "1"}]
Expand Down Expand Up @@ -274,7 +274,7 @@ async def test_fetch_data_with_pagination():
actual_response = []
async with create_graphql_source() as source:
source.graphql_client.pagination_model = "cursor_pagination"
source.graphql_client.graphql_object_list = {"users": "id"}
source.graphql_client.graphql_object_to_id_map = {"users": "id"}
source.graphql_client.pagination_key = "users"
source.graphql_client.post = AsyncMock(
side_effect=[
Expand Down Expand Up @@ -307,7 +307,7 @@ async def test_fetch_data_with_pagination():
async def test_fetch_data_without_pageinfo():
async with create_graphql_source() as source:
source.graphql_client.pagination_model = "cursor_pagination"
source.graphql_client.graphql_object_list = {"users": id}
source.graphql_client.graphql_object_to_id_map = {"users": id}
source.graphql_client.post = AsyncMock(
side_effect=[{"users": {"id": "123", "name": {"firstName": "xyz"}}}]
)
Expand Down Expand Up @@ -372,7 +372,7 @@ async def test_get_docs_with_dict_id():
@pytest.mark.asyncio
async def test_extract_graphql_data_items_with_invalid_key():
async with create_graphql_source() as source:
source.graphql_client.graphql_object_list = {"user": "id"}
source.graphql_client.graphql_object_to_id_map = {"user": "id"}
data = {"users": {"namexyzid": "123"}}
with pytest.raises(ConfigurableFieldValueError):
for _ in source.graphql_client.extract_graphql_data_items(data):
Expand Down Expand Up @@ -411,7 +411,7 @@ async def test_validate_config_with_invalid_objects():
source.graphql_client.graphql_query = (
"query {organization {repository { issues {name}}}}"
)
source.graphql_client.graphql_object_list = {
source.graphql_client.graphql_object_to_id_map = {
"organization.repository.pullRequest": "id"
}
with pytest.raises(ConfigurableFieldValueError):
Expand All @@ -421,7 +421,7 @@ async def test_validate_config_with_invalid_objects():
@pytest.mark.asyncio
async def test_validate_config_with_invalid_pagination_key():
async with create_graphql_source(
graphql_object_list='{"organization.repository.issues": "id"}'
graphql_object_to_id_map='{"organization.repository.issues": "id"}'
) as source:
source.graphql_client.graphql_query = (
"query {organization {repository { issues {id name}}}}"
Expand All @@ -435,7 +435,7 @@ async def test_validate_config_with_invalid_pagination_key():
@pytest.mark.asyncio
async def test_validate_config_with_missing_config_field():
async with create_graphql_source(
graphql_object_list='{"organization.repository.issues": "id"}'
graphql_object_to_id_map='{"organization.repository.issues": "id"}'
) as source:
source.graphql_client.graphql_query = (
"query {organization {repository { issues {name}}}}"
Expand All @@ -447,7 +447,7 @@ async def test_validate_config_with_missing_config_field():
@pytest.mark.asyncio
async def test_validate_config_with_invalid_json():
async with create_graphql_source(
graphql_object_list='{"organization.repository.issues": "id"'
graphql_object_to_id_map='{"organization.repository.issues": "id"'
) as source:
source.graphql_client.graphql_query = (
"query {organization {repository { issues {name}}}}"
Expand Down

0 comments on commit 733b112

Please sign in to comment.