Skip to content

Commit

Permalink
Add in a sub-query to get content matching the tags
Browse files Browse the repository at this point in the history
  • Loading branch information
fredex42 committed Jul 14, 2024
1 parent bd1e54b commit 53bd6b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gu.contentapi.porter.graphql

import com.gu.contentapi.porter.model.{Content, Tag}
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder
import sangria.schema._
import datastore.GQLQueryContext
import deprecated.anotherschema.Edge
Expand All @@ -24,14 +25,32 @@ object RootQuery {
)
)

val TagEdge: ObjectType[Unit, Edge[Tag]] = ObjectType(
val TagEdge: ObjectType[GQLQueryContext, Edge[Tag]] = ObjectType(
"TagEdge",
"A list of tags with pagination features",
() => fields[Unit, Edge[Tag]](
() => fields[GQLQueryContext, Edge[Tag]](
Field("totalCount", LongType, Some("Total number of results that match your query"), resolve = _.value.totalCount),
Field("endCursor", OptionType(StringType), Some("The last record cursor in the set"), resolve = _.value.endCursor),
Field("hasNextPage", BooleanType, Some("Whether there are any more records to retrieve"), resolve = _.value.hasNextPage),
Field("nodes", ListType(com.gu.contentapi.porter.graphql.Tags.Tag), Some("The actual tags returned"), resolve = _.value.nodes)
Field("nodes", ListType(com.gu.contentapi.porter.graphql.Tags.Tag), Some("The actual tags returned"), resolve = _.value.nodes),
Field("matching_content", ArticleEdge, Some("Content which matches any of the tags returned"),
arguments= ContentQueryParameters.AllContentQueryParameters,
resolve = { ctx=>
ctx.ctx.repo.marshalledDocs(ctx arg ContentQueryParameters.QueryString,
queryFields=ctx arg ContentQueryParameters.QueryFields,
atomId = None,
forChannel = ctx arg ContentQueryParameters.ChannelArg,
userTier = ctx.ctx.userTier,
tagIds = Some(ctx.value.nodes.map(_.id)),
excludeTags = ctx arg ContentQueryParameters.ExcludeTagArg,
sectionIds = ctx arg ContentQueryParameters.SectionArg,
excludeSections = ctx arg ContentQueryParameters.ExcludeSectionArg,
orderDate = ctx arg PaginationParameters.OrderDate,
orderBy = ctx arg PaginationParameters.OrderBy,
limit = ctx arg PaginationParameters.Limit,
cursor = ctx arg PaginationParameters.Cursor,
)
})
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@ object TagQueryParameters {
val AllTagQueryParameters = QueryString :: tagId :: Section :: TagType :: Fuzziness :: Category ::
Reference :: Cursor :: OrderBy :: Limit :: Nil

val NonPaginatedTagQueryParameters = Section :: TagType :: Nil
val NonPaginatedTagQueryParameters = QueryString :: tagId :: Section :: TagType :: Fuzziness :: Category ::
Reference :: Nil
}
2 changes: 1 addition & 1 deletion src/main/scala/datastore/ElasticsearchRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ElasticsearchRepo(endpoint:ElasticNodeEndpoint, val defaultPageSize:Int=20
Some(limitToChannelQuery(selectedChannel)),
queryString.map(MultiMatchQuery(_, fields = fieldsToQuery)),
atomId.map(MatchQuery("atomIds.id", _)),
tagIds.map(tags=>BoolQuery(must=tags.map(MatchQuery("tags", _)))) ,
tagIds.map(tags=>BoolQuery(should=tags.map(MatchQuery("tags", _)))) ,
excludeTags.map(tags=>BoolQuery(not=Seq(BoolQuery(should=tags.map(MatchQuery("tags", _)))))),
sectionIds.map(s=>BoolQuery(should=s.map(MatchQuery("sectionId", _)))),
excludeSections.map(s=>BoolQuery(not=Seq(BoolQuery(should=s.map(MatchQuery("sectionId", _))))))
Expand Down

0 comments on commit 53bd6b0

Please sign in to comment.