Skip to content
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

feat: Add GraphQL subscriptions #934

Merged
merged 16 commits into from
Nov 15, 2022

Conversation

fredcarle
Copy link
Collaborator

Relevant issue(s)

Resolves #724

Description

This PR adds the functionalities related to GQL subscriptions. Subscriptions can happen through the HTTP API or using db.ExecQuery directly.

It piggy backs on the previously implemented event system to listen to DB updates and then using a new Publisher, broadcasts the updates to the listening clients. The new Publisher is designed to allow the listener to close the channel hence why you'll notice many thread safety mechanics in the related methods.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

unit and integration tests

Specify the platform(s) on which this was tested:

  • MacOS

@fredcarle fredcarle added feature New feature or request area/query Related to the query component area/parser Related to the parser components area/api Related to the external API component area/db-system Related to the core system related components of the DB action/no-benchmark Skips the action that runs the benchmark. area/planner Related to the planner system labels Nov 10, 2022
@fredcarle fredcarle added this to the DefraDB v0.4 milestone Nov 10, 2022
@fredcarle fredcarle requested a review from a team November 10, 2022 23:52
@fredcarle fredcarle self-assigned this Nov 10, 2022
@codecov
Copy link

codecov bot commented Nov 10, 2022

Codecov Report

Merging #934 (f608bee) into develop (c0f8a31) will increase coverage by 0.23%.
The diff coverage is 75.63%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #934      +/-   ##
===========================================
+ Coverage    60.05%   60.29%   +0.23%     
===========================================
  Files          159      164       +5     
  Lines        17532    17700     +168     
===========================================
+ Hits         10529    10672     +143     
- Misses        6066     6081      +15     
- Partials       937      947      +10     
Impacted Files Coverage Δ
api/http/handler.go 100.00% <ø> (ø)
api/http/server.go 100.00% <ø> (ø)
planner/planner.go 77.18% <50.00%> (-0.52%) ⬇️
db/query.go 61.76% <55.55%> (+0.36%) ⬆️
db/subscriptions.go 57.14% <57.14%> (ø)
api/http/handlerfuncs.go 85.44% <64.51%> (-3.63%) ⬇️
query/graphql/parser/query.go 82.56% <71.42%> (-0.21%) ⬇️
query/graphql/parser.go 89.09% <80.00%> (+0.41%) ⬆️
query/graphql/parser/subscription.go 84.21% <84.21%> (ø)
api/http/logger.go 100.00% <100.00%> (ø)
... and 11 more

db/subscriptions.go Outdated Show resolved Hide resolved
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments to come, looks good in general

client/request/subscription.go Outdated Show resolved Hide resolved
client/request/subscription.go Show resolved Hide resolved
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Was a really really nice cleanup Fred of the pub/sub stuff.

PR looks very good overall, but I've added a few small/localized comments which would be good to get sorted pre-merge

db/subscriptions.go Show resolved Hide resolved
db/subscriptions.go Outdated Show resolved Hide resolved
db/subscriptions.go Outdated Show resolved Hide resolved
}

for _, def := range doc.Definitions {
switch node := def.(type) {
case *ast.OperationDefinition:
if node.Operation == "query" {
switch node.Operation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Thanks for making this a switch

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're welcome :)

tests/integration/utils.go Show resolved Hide resolved
@@ -66,6 +67,17 @@ var (
mapStore bool
)

// Represents a query assigned to a particular transaction.
type SubscriptionQuery struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: I really like this test setup, thank you for finding a nice solution for this :)

query/graphql/parser/subscription.go Outdated Show resolved Hide resolved
events/publisher.go Show resolved Hide resolved
Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Fairly clean review, I like the subscription test util modification as well.

@fredcarle fredcarle merged commit 0b9719f into develop Nov 15, 2022
@fredcarle fredcarle deleted the fredcarle/feat/I724-graphql-subscription branch November 15, 2022 02:37
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
Relevant issue(s)
Resolves sourcenetwork#724

Description
This PR adds the functionalities related to GQL subscriptions. Subscriptions can happen through the HTTP API or using db.ExecQuery directly.

It piggy backs on the previously implemented event system to listen to DB updates and then using a new Publisher, broadcasts the updates to the listening clients. The new Publisher is designed to allow the listener to close the channel hence why you'll notice many thread safety mechanics in the related methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/no-benchmark Skips the action that runs the benchmark. area/api Related to the external API component area/db-system Related to the core system related components of the DB area/parser Related to the parser components area/planner Related to the planner system area/query Related to the query component feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Events: Support GraphQL subscription operation
3 participants