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: Default scalar field values #2997

Merged
merged 10 commits into from
Sep 16, 2024

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Sep 11, 2024

Relevant issue(s)

Resolves #2952

Description

This PR adds support for default field values using a new @default directive.

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?

Added integration tests

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

  • MacOS

@nasdf nasdf added feature New feature or request area/query Related to the query component labels Sep 11, 2024
@nasdf nasdf self-assigned this Sep 11, 2024
@nasdf nasdf added this to the DefraDB v0.14 milestone Sep 11, 2024
Copy link

codecov bot commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 82.75862% with 20 lines in your changes missing coverage. Please review.

Project coverage is 79.40%. Comparing base (f5567f5) to head (0c3f6d1).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
client/document.go 57.14% 6 Missing and 6 partials ⚠️
internal/request/graphql/schema/collection.go 87.10% 4 Missing ⚠️
http/client_collection.go 33.33% 1 Missing and 1 partial ⚠️
internal/db/fetcher/encoded_doc.go 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2997      +/-   ##
===========================================
- Coverage    79.46%   79.40%   -0.06%     
===========================================
  Files          329      329              
  Lines        25133    25225      +92     
===========================================
+ Hits         19971    20029      +58     
- Misses        3750     3772      +22     
- Partials      1412     1424      +12     
Flag Coverage Δ
all-tests 79.40% <82.76%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
client/collection_field_description.go 76.47% <100.00%> (+1.47%) ⬆️
client/definitions.go 80.12% <100.00%> (+0.25%) ⬆️
internal/request/graphql/schema/errors.go 42.86% <100.00%> (+13.03%) ⬆️
internal/request/graphql/schema/generate.go 87.97% <100.00%> (+0.02%) ⬆️
internal/request/graphql/schema/manager.go 98.43% <100.00%> (+0.01%) ⬆️
internal/request/graphql/schema/types/types.go 100.00% <100.00%> (ø)
http/client_collection.go 49.32% <33.33%> (-0.45%) ⬇️
internal/db/fetcher/encoded_doc.go 78.38% <33.33%> (-2.18%) ⬇️
internal/request/graphql/schema/collection.go 88.84% <87.10%> (-0.38%) ⬇️
client/document.go 70.39% <57.14%> (-1.43%) ⬇️

... and 12 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f5567f5...0c3f6d1. Read the comment docs.

@nasdf nasdf changed the title feat: Default field values feat: Default scalar field values Sep 11, 2024
@nasdf nasdf marked this pull request as ready for review September 11, 2024 22:40
@nasdf nasdf requested a review from a team September 11, 2024 22:40
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.

It looks good and is easy to read, but please add some error tests, validation for the default values (in definition_validation.go), View behaviour, and either test PatchCollection works, or temporary block it off (and test that).

) (any, error) {
astNamed, ok := field.Type.(*ast.Named)
if !ok {
return nil, NewErrDefaultValueNotAllowed(field.Name.Value, field.Type.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: Please add tests for this error condition

Copy link
Member Author

Choose a reason for hiding this comment

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

done

var value any
for _, arg := range directive.Arguments {
if defaultPropNameToType[arg.Name.Value] != astNamed.Name.Value {
return nil, NewErrDefaultValueInvalid(astNamed.Name.Value, arg.Name.Value)
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: Please add tests for this error condition

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -38,6 +38,9 @@ type CollectionFieldDescription struct {
//
// Otherwise will be [None].
RelationName immutable.Option[string]

// DefaultValue contains the default value for this field.
DefaultValue any
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: Please test the PatchCollection behaviour for this new property

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -38,6 +38,9 @@ type CollectionFieldDescription struct {
//
// Otherwise will be [None].
RelationName immutable.Option[string]

// DefaultValue contains the default value for this field.
DefaultValue any
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: Please test and document what happens when this is set on a View - and consider adding a validation rule to prevent that (as it makes no sense to me) (and test that, including on Patch).

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a test to show the current behavior. Since it has no effect on the results of the view, I think allowing it is better than having the schema definition and view definition languages diverge and have slightly different rules.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay cheers, I'm happy with that :) - can you please document that the property is ignored for views on the directive, and on this CollectionFieldDescription prop so that users know?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Comment on lines -508 to -542
const (
typeID string = "ID"
typeBoolean string = "Boolean"
typeInt string = "Int"
typeFloat string = "Float"
typeDateTime string = "DateTime"
typeString string = "String"
typeBlob string = "Blob"
typeJSON string = "JSON"
)

Copy link
Member

Choose a reason for hiding this comment

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

praise: Thanks for moving

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.

Cheers thanks for adding the requested tests

`,
},
testUtils.Request{
Request: `mutation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: Would it be worth using the CreateDoc action instead to test across all clients?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This applies to all the integration tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please use the CreateDoc action instead Keenan (nice spot Fred)

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for all the tests. Lets just resolve my question before merging please.

Description: "Simple create mutation, with default value, no value provided, and created twice",
SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// This test will fail if using the collection save
// method because it does not create two unique docs
Copy link
Contributor

Choose a reason for hiding this comment

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

question: I can't spot anything super obvious in the test code as to why Save would not work, would you mind expanding a little?

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like calling save does not actually create two unique documents when the fields have the same value. I'm guessing it checks if the docID exists and calls update instead of create.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh true - it does - thanks Keenan. Might be worth adding that to this code-comment then.

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.

LGTM, thanks Keenan :)

@nasdf nasdf merged commit ea3a74f into sourcenetwork:develop Sep 16, 2024
40 of 42 checks passed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Support for default values
4 participants