-
Notifications
You must be signed in to change notification settings - Fork 9
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: Initial support for apiv2 #1085
Draft
jgadling
wants to merge
18
commits into
main
Choose a base branch
from
jgadling/apiv2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
def0f82
Early support for apiv2
jgadling c8d69a7
Start working on tests.
jgadling c36cdd8
Support mapping camelCase field names in the api to snake_case field
jgadling 1ef4b2a
Sort models by name.
jgadling 223aa29
Resolved field discrepancies.
jgadling 66e3767
regen without sorting fields
jgadling 6fbc18b
Remove unneeded field
jgadling acb7d41
Last few fields.
jgadling bf9f226
More schema changes.
jgadling 2ff1e0e
Checkpoint: improve tests.
jgadling 7c6b1da
Get tests to pass.
jgadling 8ad5563
Start updating ci for apiv2
jgadling 041769b
Update CI to use the latest `staging` api service to run tests against.
jgadling 0f6435f
Update schema to reflect prod.
jgadling cf871d8
Lint.
jgadling ad50024
include setuptools-scm to make pip installable from github.
uermel ecdee93
fix converting list of string to camelcase
uermel 9c3ddff
make sure .graphql is included.
uermel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,6 +20,7 @@ | |||||||||||
GraphQLType, | ||||||||||||
build_schema, | ||||||||||||
print_schema, | ||||||||||||
get_named_type, | ||||||||||||
) | ||||||||||||
from jinja2 import Environment, FileSystemLoader | ||||||||||||
|
||||||||||||
|
@@ -31,7 +32,7 @@ | |||||||||||
"Float": ("FloatField()", "float"), | ||||||||||||
"Int": ("IntField()", "int"), | ||||||||||||
"String": ("StringField()", "str"), | ||||||||||||
"date": ("DateField()", "date"), | ||||||||||||
"DateTime": ("DateField()", "date"), | ||||||||||||
"numeric": ("FloatField()", "float"), | ||||||||||||
"_numeric": ("StringField()", "str"), | ||||||||||||
"tomogram_type_enum": ("StringField()", "str"), | ||||||||||||
|
@@ -40,19 +41,24 @@ | |||||||||||
|
||||||||||||
"""Maps GraphQL type names to model class names.""" | ||||||||||||
GQL_TO_MODEL_TYPE = { | ||||||||||||
"datasets": "Dataset", | ||||||||||||
"dataset_authors": "DatasetAuthor", | ||||||||||||
"dataset_funding": "DatasetFunding", | ||||||||||||
"runs": "Run", | ||||||||||||
"tomogram_voxel_spacings": "TomogramVoxelSpacing", | ||||||||||||
"tomograms": "Tomogram", | ||||||||||||
"tomogram_authors": "TomogramAuthor", | ||||||||||||
"annotations": "Annotation", | ||||||||||||
"annotation_files": "AnnotationFile", | ||||||||||||
"annotation_authors": "AnnotationAuthor", | ||||||||||||
"tiltseries": "TiltSeries", | ||||||||||||
"depositions": "Deposition", | ||||||||||||
"deposition_authors": "DepositionAuthor", | ||||||||||||
"Alignment": "Alignment", | ||||||||||||
"AnnotationAuthor": "AnnotationAuthor", | ||||||||||||
"AnnotationFile": "AnnotationFile", | ||||||||||||
"AnnotationShape": "AnnotationShape", | ||||||||||||
"Annotation": "Annotation", | ||||||||||||
"DatasetAuthor": "DatasetAuthor", | ||||||||||||
"DatasetFunding": "DatasetFunding", | ||||||||||||
"Dataset": "Dataset", | ||||||||||||
"DepositionAuthor": "DepositionAuthor", | ||||||||||||
"Deposition": "Deposition", | ||||||||||||
"Frame": "Frame", | ||||||||||||
"PerSectionAlignmentParameters": "PerSectionAlignmentParameters", | ||||||||||||
"PerSectionParameters": "PerSectionParameters", | ||||||||||||
"Run": "Run", | ||||||||||||
"Tiltseries": "Tiltseries", | ||||||||||||
"TomogramAuthor": "TomogramAuthor", | ||||||||||||
"TomogramVoxelSpacing": "TomogramVoxelSpacing", | ||||||||||||
"Tomogram": "Tomogram", | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
||||||||||||
|
@@ -81,7 +87,8 @@ class ModelInfo: | |||||||||||
"""The information about a parsed model.""" | ||||||||||||
|
||||||||||||
name: str | ||||||||||||
gql_name: str | ||||||||||||
gql_type: str | ||||||||||||
root_field: str | ||||||||||||
fields: Tuple[FieldInfo, ...] | ||||||||||||
description: Optional[str] = None | ||||||||||||
|
||||||||||||
|
@@ -127,7 +134,8 @@ def get_models(schema: GraphQLSchema) -> Tuple[ModelInfo, ...]: | |||||||||||
models.append( | ||||||||||||
ModelInfo( | ||||||||||||
name=model, | ||||||||||||
gql_name=gql_type.name, | ||||||||||||
gql_type=gql_type.name, | ||||||||||||
root_field=get_root_field_name(schema, gql_type), | ||||||||||||
description=gql_type.description, | ||||||||||||
fields=fields, | ||||||||||||
), | ||||||||||||
|
@@ -156,6 +164,17 @@ def load_schema(path: Path) -> GraphQLSchema: | |||||||||||
return build_schema(schema_str) | ||||||||||||
|
||||||||||||
|
||||||||||||
def get_root_field_name(schema, gql_type: GraphQLObjectType) -> str: | ||||||||||||
"""Look up the root field name that represents the given GQL Type""" | ||||||||||||
"""NOTE that this assumes all queried types are present at the query root!""" | ||||||||||||
root = schema.get_type("Query") | ||||||||||||
for name, field in root.fields.items(): | ||||||||||||
field_type = get_named_type(field.type) | ||||||||||||
if field_type.name == gql_type.name: | ||||||||||||
return name | ||||||||||||
raise RuntimeError(f"Could not root field for {gql_type.name}") | ||||||||||||
|
||||||||||||
|
||||||||||||
def parse_fields(gql_type: GraphQLObjectType) -> Tuple[FieldInfo, ...]: | ||||||||||||
"""Returns the field information parsed from a GraphQL object type.""" | ||||||||||||
fields = [] | ||||||||||||
|
@@ -181,7 +200,7 @@ def _parse_field( | |||||||||||
) -> Optional[FieldInfo]: | ||||||||||||
logging.debug("_parse_field: %s, %s", name, field) | ||||||||||||
field_type = _maybe_unwrap_non_null(field.type) | ||||||||||||
if isinstance(field_type, GraphQLList): | ||||||||||||
if field_type.name.endswith("Connection"): # TODO can we clean this up? | ||||||||||||
return _parse_model_list_field(gql_type, name, field_type) | ||||||||||||
if isinstance(field_type, GraphQLObjectType) and ( | ||||||||||||
field_type.name in GQL_TO_MODEL_TYPE | ||||||||||||
|
@@ -225,7 +244,7 @@ def _parse_model_field( | |||||||||||
name=name, | ||||||||||||
description=f"The {model_name} this {source_model_name} is a part of", | ||||||||||||
annotation_type=model, | ||||||||||||
default_value=f'ItemRelationship("{model}", "{model_field}_id", "id")', | ||||||||||||
default_value=f'ItemRelationship("{model}", "{model_field}Id", "id")', | ||||||||||||
) | ||||||||||||
return None | ||||||||||||
|
||||||||||||
|
@@ -236,7 +255,9 @@ def _parse_model_list_field( | |||||||||||
field_type: GraphQLList[GraphQLType], | ||||||||||||
) -> Optional[FieldInfo]: | ||||||||||||
logging.debug("_parse_model_list_field: %s", field_type) | ||||||||||||
of_type = _maybe_unwrap_non_null(field_type.of_type) | ||||||||||||
of_type = get_named_type( | ||||||||||||
get_named_type(field_type.fields["edges"].type).fields["node"].type | ||||||||||||
) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I find this chained call pretty hard to read. Consider unwrapping it with something like.
Suggested change
|
||||||||||||
if not isinstance(of_type, GraphQLNamedType): | ||||||||||||
return None | ||||||||||||
of_model = GQL_TO_MODEL_TYPE.get(of_type.name) | ||||||||||||
|
@@ -249,7 +270,7 @@ def _parse_model_list_field( | |||||||||||
name=name, | ||||||||||||
description=f"The {of_model_name} of this {source_model_name}", | ||||||||||||
annotation_type=f"List[{of_model}]", | ||||||||||||
default_value=f'ListRelationship("{of_model}", "id", "{source_field}_id")', | ||||||||||||
default_value=f'ListRelationship("{of_model}", "id", "{source_field}Id")', | ||||||||||||
) | ||||||||||||
return None | ||||||||||||
|
||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are always expected to be identical now, maybe this should just be a tuple of gql/model names instead of a mapping?