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

parse parameter type annotations when generating native query configuration #128

Merged
merged 15 commits into from
Nov 22, 2024

Conversation

hallettj
Copy link
Collaborator

@hallettj hallettj commented Nov 20, 2024

This is work an an in-progress feature that is gated behind a feature flag, native-query-subcommand.

Parses type annotations in placeholders that reference native query parameters. We already have an error message suggesting doing this, now the system actually reads these. For example,

{
  "$match": {
    "imdb.rating": { "$gt": "{{ min_rating | int! }}" }
  }
}

The generated query for that configuration includes a parameter named min_rating with type int. Without the annotation it would have inferred double.

Type annotations are checked against the inferred type for the position so you will see errors if the annotated type is not compatible. Parameters are treated as contravariant so you can only provide a subtype of the inferred type - for example you can constrain a parameter type to be non-nullable even if it is in a nullable context. There are cases where the type checker cannot infer a type in which case annotations are necessary.

I know we already have a similar parser for type expressions in hml files. I thought it would be easier to write a new one specialized for this connector and for MongoDB scalar types since it's about 100 LOC.

Type expressions use GraphQL syntax to match types as seen in GraphQL and in hml. There is one addition: I invented a syntax for predicate types, predicate<ObjectTypeName>. The parser happens to be written so that if the angle brackets are absent the word predicate will be interpreted as an object type so we don't have a problem if a user want to use an object type named "predicate".

On the other hand this parser does not allow object type names that match MongoDB scalar type names.

While I was working on this I noticed some issues with unifying types in the presence of nullability, and with displaying errors when a type annotation can't be unified with inferred constraints for a parameter's context. So I included some fixes in those areas.

@hallettj hallettj self-assigned this Nov 20, 2024
@hallettj hallettj changed the base branch from main to jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs November 20, 2024 23:27
@hallettj hallettj changed the base branch from jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs to main November 20, 2024 23:28
@hallettj hallettj force-pushed the jessehallett/eng-1249-parse-parameter-type-annotations branch from 1ceb7f9 to aacb803 Compare November 22, 2024 02:12
@hallettj hallettj changed the base branch from main to jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs November 22, 2024 02:12
@@ -114,7 +117,8 @@ fn unable_to_infer_types_message(
for name in problem_parameter_types {
message += &format!("- {name}\n");
}
message += "\nTry adding type annotations of the form: {{parameter_name|[int!]!}}\n";
message += "\nTry adding type annotations of the form: {{ parameter_name | [int!]! }}\n";
message += "\nIf you added an annotation, and you are still seeing this error then the type you gave may not be compatible with the context where the parameter is used.\n";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we link to somewhere they can read on supported types?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I need to do a docs pass on the whole native query DX feature, then I'll have something to link to

},

// TODO: We probably want a separate step that swaps ElementOf and FieldOf constraints with
// constraint of the targeted structure. We might do a similar thing with
// WithFieldOverrides.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Follow up tickets?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed the TODO because I don't think we need to worry about this

Ok(())
}

// TODO:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unsupported right now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes - I wrote the test to see if it works, and it doesn't. I want to fix that later so I want to keep the test, but I don't want to commit a failing test.

Base automatically changed from jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs to main November 22, 2024 22:50
@hallettj hallettj force-pushed the jessehallett/eng-1249-parse-parameter-type-annotations branch from aacb803 to 30bdfa8 Compare November 22, 2024 22:54
@hallettj hallettj merged commit 19da8ab into main Nov 22, 2024
1 check passed
@hallettj hallettj deleted the jessehallett/eng-1249-parse-parameter-type-annotations branch November 22, 2024 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants