mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
b4f7e96665
### Motivation While we strive to write clear code, we have historically struggled at Hasura with having very different styles and standards across the codebase. There's been efforts to standardize our coding style, we have an official styleguide that isn't maintained as closely as it should... We still have some work in front of us. However, in the last ~year or so, there's been a huge push towards incrementally improving the situation. As part of this we've been blocking PRs that don't add enough comments, or don't improve the files that they touch. While looking at `Hasura.GraphQL.Analyse`, it became apparent that this file did not meet the engineering standards that I would expect to see addressed during a code review. Some ways in which I think it falls short: - lack of documentation - no clear distinction between public / internal components - "unidiomatic" Haskell code (such as using `Either Result Error`) While there's no problem with a file looking like this during development, those issues should have been caught at review time. The fact that they weren't indicates a problem in our process that we will need to address: code quality and maintainability is paramount, and we all need to do our part. ### Description This PR rewrites all of `Hasura.GraphQL.Analyze`, and adapts `Hasura.Server.OpenAPI` accordingly where needed. I've attempted to clarify names and add documentation based on my understanding of the code, and to clean what was unused (such as field variables). I don't think this PR is good enough as is, and I welcome criticism where I got my comments wrong / am happy to help y'all add more. This PR makes one small change in the way error messages are reported (and adjusts the corresponding test accordingly); each error message is now prefixed with the path within the selection set: ``` ⚠️ $.test.foo.bar.baz.mizpelled: field 'mizpelled' not found in object 'Baz' ``` ### Note This PR is currently **on top of #3962**. You can preview the changes in isolation by [diffing the branches](https://github.com/hasura/graphql-engine-mono/compare/nicuveo/clean-rest-endpoint-inconsistency-check..nicuveo/rewrite-analysis). PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3963 Co-authored-by: paritosh-08 <85472423+paritosh-08@users.noreply.github.com> GitOrigin-RevId: 5ec38e0e753f0c12096a350db0737658495e2f15
118 lines
4.6 KiB
Python
118 lines
4.6 KiB
Python
import collections
|
|
import pytest
|
|
import os
|
|
from validate import check_query_f, check_query, get_conf_f
|
|
from context import PytestConf
|
|
|
|
@pytest.mark.parametrize("transport", ['http'])
|
|
@pytest.mark.usefixtures('per_class_tests_db_state')
|
|
class TestOpenAPISpec:
|
|
|
|
@classmethod
|
|
def dir(cls):
|
|
return 'queries/openapi'
|
|
|
|
def test_empty_openapi_json(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_empty.yaml', transport)
|
|
|
|
def test_endpoint_simple(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_get_endpoint_test_simple.yaml', transport)
|
|
|
|
def test_endpoint_with_aliases(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_endpoint_with_aliases.yaml', transport)
|
|
|
|
def test_endpoint_with_args(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_post_endpoint_test_with_args.yaml', transport)
|
|
|
|
def test_endpoint_with_args_url(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_post_endpoint_test_with_args_url.yaml', transport)
|
|
|
|
def test_endpoint_with_default_arg(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_post_endpoint_test_with_default_arg.yaml', transport)
|
|
|
|
def test_endpoint_with_multiple_methods(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_endpoint_with_multiple_methods.yaml', transport)
|
|
|
|
def test_multiple_endpoints(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_multiple_endpoints_test.yaml', transport)
|
|
|
|
def test_multiple_endpoints_same_path(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_multiple_endpoints_same_path.yaml', transport)
|
|
|
|
def test_multiple_endpoints_with_path_segments(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_multiple_endpoints_with_path_segments.yaml', transport)
|
|
|
|
def test_endpoint_with_complex_arg(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_get_endpoint_test_complex_arg.yaml', transport)
|
|
|
|
def test_endpoint_with_complex_args(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_get_endpoint_test_complex_args.yaml', transport)
|
|
|
|
def test_endpoint_with_recursive_arg(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_post_endpoint_test_recursive_arg.yaml', transport)
|
|
|
|
def test_duplicate_field_name(self, hge_ctx, transport):
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_get_endpoint_test_duplicate_field_name.yaml', transport)
|
|
|
|
def test_inconsistent_schema_openAPI(self, hge_ctx, transport):
|
|
# export metadata and create a backup
|
|
st_code, backup_metadata = hge_ctx.v1q(
|
|
q = {
|
|
"type": "export_metadata",
|
|
"args": {}
|
|
}
|
|
)
|
|
assert st_code == 200, backup_metadata
|
|
|
|
new_metadata = backup_metadata.copy()
|
|
|
|
#create inconsistent metadata
|
|
inconsistent_query = [collections.OrderedDict([
|
|
('name', 'wrong_queries'),
|
|
('definition', collections.OrderedDict([
|
|
('queries', [collections.OrderedDict([
|
|
('name', 'random_query'),
|
|
('query', 'query { random_field_name test_table { random_col_name } }')
|
|
])])
|
|
]))
|
|
])]
|
|
res_endpoint = [collections.OrderedDict([
|
|
("definition", collections.OrderedDict([
|
|
("query", collections.OrderedDict([
|
|
("collection_name", "wrong_queries"),
|
|
("query_name", "random_query")
|
|
]))
|
|
])),
|
|
("url", "some_url"),
|
|
("methods", ["GET"]),
|
|
("name", "wrong_endpoint"),
|
|
("comment", None)
|
|
])]
|
|
new_metadata["query_collections"] = inconsistent_query
|
|
new_metadata["rest_endpoints"] = res_endpoint
|
|
|
|
# apply inconsistent metadata
|
|
st_code, resp = hge_ctx.v1q(
|
|
q={
|
|
"type": "replace_metadata",
|
|
"version": 2,
|
|
"args": {
|
|
"allow_inconsistent_metadata": True,
|
|
"metadata": new_metadata
|
|
}
|
|
}
|
|
)
|
|
assert st_code == 200, resp
|
|
|
|
# check openAPI schema
|
|
check_query_f(hge_ctx, self.dir() + '/openapi_inconsistent_schema.yaml', transport)
|
|
|
|
# revert to old metadata
|
|
st_code, resp = hge_ctx.v1q(
|
|
q={
|
|
"type": "replace_metadata",
|
|
"args": backup_metadata
|
|
}
|
|
)
|
|
assert st_code == 200, resp
|