Skip to content

fix(url): adding in url resolving #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .run/tests.run.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="tests" type="tests" factoryName="py.test">
<module name="recommendation-api" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="Remote Python 3.11.2 Docker Compose (app)" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
Expand Down
10 changes: 6 additions & 4 deletions app/graphql/item.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
from typing import List, Optional
from typing import List, Optional, NewType

import strawberry
from strawberry.federation.schema_directives import Key, Requires

from app.graphql.corpus_recommendation import CorpusRecommendation
from app.graphql.directives import CacheControl
from app.graphql.link import Url
from app.graphql.resolvers.item2item_resolvers import resolve_similar_to_saved, resolve_after_article
from app.models.item import ItemModel


@strawberry.experimental.pydantic.type(
model=ItemModel,
directives=[Key(fields="itemId")])
directives=[Key(fields="itemId"), Key(fields="givenUrl")])
class Item:
item_id: str # This type is a 'str' and not an 'ID' in our graph.
given_url: Url # This type is a 'str' and not an 'ID' in our graph.

# a field from a different subgraph
# see https://www.apollographql.com/docs/federation/entities-advanced/#contributing-computed-entity-fields
language: Optional[str] = strawberry.federation.field(external=True)

relatedAfterArticle: List[CorpusRecommendation] = strawberry.field(
directives=[CacheControl(maxAge=3600),
Requires(fields='language')],
Requires(fields='language itemId')],
resolver=resolve_after_article,
description='Recommend similar articles to show in the bottom of an article.')

relatedAfterCreate: List[CorpusRecommendation] = strawberry.field(
directives=[CacheControl(maxAge=3600),
Requires(fields='language')],
Requires(fields='language itemId')],
resolver=resolve_similar_to_saved,
description='Recommend similar articles after saving.')
2 changes: 1 addition & 1 deletion app/graphql/recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Recommendation:
feed_item_id: Optional[strawberry.ID] = strawberry.field(deprecation_reason="Use `id`")
feed_id: auto
item_id: strawberry.ID
item: Item = strawberry.federation.field(shareable=True)
item: Item = strawberry.federation
rec_src: str
publisher: auto
1 change: 1 addition & 0 deletions app/models/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ class Candidate(BaseModel):
item_id: int
publisher: str
feed_id: Optional[int]
url: str
1 change: 1 addition & 0 deletions app/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

class ItemModel(BaseModel):
item_id: str
given_url: str
2 changes: 1 addition & 1 deletion app/models/recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def candidate_dict_to_recommendation(candidate: dict):
feed_id=candidate.get('feed_id'),
publisher=candidate.get('publisher'),
item_id=candidate.get('item_id'),
item=ItemModel(item_id=candidate.get('item_id'))
item=ItemModel(item_id=candidate.get('item_id'), url=candidate.get('url')),
)
recommendation.feed_item_id = recommendation.id
return recommendation
Expand Down
9 changes: 6 additions & 3 deletions tests/functional/models/test_candidate_set_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ async def test_verify_candidate_set(candidate_sets_dynamodb_table, aiocache_fixt
{
"feed_id": 1,
"item_id": 3208490410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org"
}
]
})
Expand All @@ -32,7 +33,8 @@ async def test_get_candidate_set(candidate_sets_dynamodb_table, aiocache_fixture
{
"feed_id": 1,
"item_id": 3208490410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org"
}
]
})
Expand All @@ -54,7 +56,8 @@ async def test_get_cached_candidate_set(candidate_sets_dynamodb_table, aiocache_
{
"feed_id": 1,
"item_id": 3208490410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org"
}
]
})
Expand Down
24 changes: 16 additions & 8 deletions tests/functional/models/test_slate_lineup_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{
"feed_id": 1,
"item_id": 3208490410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org"
}
]
}
Expand All @@ -31,12 +32,14 @@
{
"feed_id": 1,
"item_id": 11,
"publisher": "getpocket.com"
"publisher": "getpocket.com",
"url": "https://getpocket.com"
},
{
"feed_id": 1,
"item_id": 12,
"publisher": "getpocket.com"
"publisher": "getpocket.com",
"url": "https://getpocket.com/item/test"
}
]
}
Expand Down Expand Up @@ -119,17 +122,20 @@ async def test_get_slate_lineup_limited_recs(self, slate_config, slate_lineup_co
{
"feed_id": 1,
"item_id": 3208490410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org"
},
{
"feed_id": 1,
"item_id": 3208650410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org/2"
},
{
"feed_id": 1,
"item_id": 32084925410,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org/4"
}
]
})
Expand All @@ -156,12 +162,14 @@ async def test_get_slate_lineup_dedupe_recs(self, slate_lineup_config):
{
"feed_id": 1,
"item_id": 10,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org/3"
},
{
"feed_id": 1,
"item_id": 11,
"publisher": "hbr.org"
"publisher": "hbr.org",
"url": "https://hbr.org/123"
},
]
})
Expand Down
12 changes: 8 additions & 4 deletions tests/functional/models/test_slate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@
{
'feed_id': 1,
'item_id': 3208490410,
'publisher': 'hbr.org'
'publisher': 'hbr.org',
"url": "https://hbr.org/12"
},
{
'feed_id': 1,
'item_id': 32087704190,
'publisher': 'hbr.org'
'publisher': 'hbr.org',
"url": "https://hbr.org/16"
},
{
'feed_id': 12,
'item_id': 32087904100,
'publisher': 'hbr.org'
'publisher': 'hbr.org',
"url": "https://hbr.org/18"
},
{
'feed_id': 2,
'item_id': 32087904870,
'publisher': 'hbr.org'
'publisher': 'hbr.org',
"url": "https://hbr.org/17"
}
]

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def generate_recommendations(item_ids: List[int or str]) -> List[RecommendationM
for item_id in item_ids:
rec = RecommendationModel(
item_id=item_id,
item=ItemModel(item_id=item_id),
item=ItemModel(item_id=item_id, url="https://getpocket.com"),
rec_src='bowling',
feed_id=random.randint(0, 101),
id=random.randint(0, 101)
id=random.randint(0, 101),
)
rec.feed_item_id = id
recs.append(rec)
Expand Down