diff --git a/server/lib/api-tests/src/Test/API/Metadata/NativeQueries/ValidationSpec.hs b/server/lib/api-tests/src/Test/API/Metadata/NativeQueries/ValidationSpec.hs index 4dd9bcd3196..7b85380d03d 100644 --- a/server/lib/api-tests/src/Test/API/Metadata/NativeQueries/ValidationSpec.hs +++ b/server/lib/api-tests/src/Test/API/Metadata/NativeQueries/ValidationSpec.hs @@ -350,6 +350,34 @@ tests = do path: $.args |] + it "when the native query name has a non-standard character" $ + \testEnvironment -> do + let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment + sourceName = BackendType.backendSourceName backendTypeMetadata + + dividedStuffNativeQuery :: Schema.NativeQuery + dividedStuffNativeQuery = + (Schema.nativeQuery "Divided-Stuff" simpleQuery "divided_stuff") + { Schema.nativeQueryArguments = + [ Schema.nativeQueryColumn "denominator" Schema.TInt, + Schema.nativeQueryColumn "target_date" Schema.TUTCTime + ] + } + + Schema.trackCustomReturnType sourceName dividedReturnType testEnvironment + + actual <- + GraphqlEngine.postMetadataWithStatus + 400 + testEnvironment + (Schema.trackNativeQueryCommand sourceName backendTypeMetadata dividedStuffNativeQuery) + let expected = + [yaml| + code: parse-failed + path: $.args.root_field_name + |] + actual `shouldAtLeastBe` expected + describe "Validation succeeds" do it "when tracking then untracking then re-tracking a native query" $ \testEnvironment -> do @@ -372,3 +400,21 @@ tests = do Schema.untrackNativeQuery sourceName dividedStuffNativeQuery testEnvironment Schema.trackNativeQuery sourceName dividedStuffNativeQuery testEnvironment + + it "when the native query name has an uppercase letter" $ + \testEnvironment -> do + let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment + sourceName = BackendType.backendSourceName backendTypeMetadata + + dividedStuffNativeQuery :: Schema.NativeQuery + dividedStuffNativeQuery = + (Schema.nativeQuery "DividedStuff" simpleQuery "divided_stuff") + { Schema.nativeQueryArguments = + [ Schema.nativeQueryColumn "denominator" Schema.TInt, + Schema.nativeQueryColumn "target_date" Schema.TUTCTime + ] + } + + Schema.trackCustomReturnType sourceName dividedReturnType testEnvironment + + Schema.trackNativeQuery sourceName dividedStuffNativeQuery testEnvironment diff --git a/server/lib/api-tests/src/Test/Queries/NativeQueries/NativeQueriesSpec.hs b/server/lib/api-tests/src/Test/Queries/NativeQueries/NativeQueriesSpec.hs index a524ce66bf8..89e52152ccc 100644 --- a/server/lib/api-tests/src/Test/Queries/NativeQueries/NativeQueriesSpec.hs +++ b/server/lib/api-tests/src/Test/Queries/NativeQueries/NativeQueriesSpec.hs @@ -244,6 +244,42 @@ tests = do shouldReturnYaml testEnvironment actual expected + it "Runs the a simple query with uppercase letters in the name" $ \testEnvironment -> do + let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment + source = BackendType.backendSourceName backendTypeMetadata + + uppercaseNativeQuery :: Schema.NativeQuery + uppercaseNativeQuery = + (Schema.nativeQuery "UppercaseNativeQuery" query "hello_world_return_type") + + Schema.trackCustomReturnType source helloWorldReturnType testEnvironment + Schema.trackNativeQuery source uppercaseNativeQuery testEnvironment + + let expected = + [yaml| + data: + UppercaseNativeQuery: + - one: "hello" + two: "world" + - one: "welcome" + two: "friend" + |] + + actual :: IO Value + actual = + GraphqlEngine.postGraphql + testEnvironment + [graphql| + query { + UppercaseNativeQuery { + one + two + } + } + |] + + shouldReturnYaml testEnvironment actual expected + it "Runs simple query with a basic where clause" $ \testEnvironment -> do let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment source = BackendType.backendSourceName backendTypeMetadata diff --git a/server/src-lib/Hasura/Backends/Postgres/Instances/NativeQueries.hs b/server/src-lib/Hasura/Backends/Postgres/Instances/NativeQueries.hs index 6f44eb95cd5..176ab7d56ea 100644 --- a/server/src-lib/Hasura/Backends/Postgres/Instances/NativeQueries.hs +++ b/server/src-lib/Hasura/Backends/Postgres/Instances/NativeQueries.hs @@ -34,7 +34,7 @@ import Hasura.NativeQuery.Metadata NativeQueryArgumentName, NativeQueryMetadata (..), ) -import Hasura.NativeQuery.Types (NullableScalarType (nstType), getNativeQueryName) +import Hasura.NativeQuery.Types (NullableScalarType (nstType)) import Hasura.Prelude import Hasura.SQL.Backend @@ -205,11 +205,10 @@ nativeQueryToPreparedStatement :: NativeQueryMetadata ('Postgres pgKind) -> m (BS.ByteString, Text) nativeQueryToPreparedStatement customReturnType model = do - let name = getNativeQueryName $ _nqmRootFieldName model let (preparedIQ, argumentMapping) = renameIQ $ _nqmCode model logimoCode :: Text logimoCode = renderIQ preparedIQ - prepname = "_logimo_vali_" <> toTxt name + prepname = "_logimo_vali_" occurringArguments, declaredArguments, undeclaredArguments :: Set NativeQueryArgumentName occurringArguments = Set.fromList (Map.elems argumentMapping) diff --git a/server/src-test/Hasura/Backends/Postgres/NativeQueries/NativeQueriesSpec.hs b/server/src-test/Hasura/Backends/Postgres/NativeQueries/NativeQueriesSpec.hs index 0c9f9ce73f0..e8b4cdccbe1 100644 --- a/server/src-test/Hasura/Backends/Postgres/NativeQueries/NativeQueriesSpec.hs +++ b/server/src-test/Hasura/Backends/Postgres/NativeQueries/NativeQueriesSpec.hs @@ -110,7 +110,7 @@ spec = do (first showQErr actual) `shouldSatisfy` isRight let Right rendered = actual rendered - `shouldBe` "PREPARE _logimo_vali_root_field_name(varchar) AS WITH _cte_logimo_vali_root_field_name AS (\nSELECT $1, $1\n)\nSELECT \nFROM _cte_logimo_vali_root_field_name" + `shouldBe` "PREPARE _logimo_vali_(varchar) AS WITH _cte_logimo_vali_ AS (\nSELECT $1, $1\n)\nSELECT \nFROM _cte_logimo_vali_" it "Handles multiple variables " do let Right code = parseInterpolatedQuery "SELECT {{hey}}, {{ho}}" @@ -131,4 +131,4 @@ spec = do (first showQErr actual) `shouldSatisfy` isRight let Right rendered = actual rendered - `shouldBe` "PREPARE _logimo_vali_root_field_name(varchar, integer) AS WITH _cte_logimo_vali_root_field_name AS (\nSELECT $1, $2\n)\nSELECT \nFROM _cte_logimo_vali_root_field_name" + `shouldBe` "PREPARE _logimo_vali_(varchar, integer) AS WITH _cte_logimo_vali_ AS (\nSELECT $1, $2\n)\nSELECT \nFROM _cte_logimo_vali_"