这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions server/src-lib/Hasura/GraphQL/Validate/InputValue.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}

module Hasura.GraphQL.Validate.InputValue
( validateInputValue
Expand Down Expand Up @@ -90,6 +92,23 @@ jsonParser =
jScalar J.Null = pNull
jScalar v = pVal v

toJValue :: (MonadError QErr m) => G.Value -> m J.Value
toJValue = \case
G.VVariable _ ->
throwVE "variables are not allowed in scalars"
G.VInt i -> return $ J.toJSON i
G.VFloat f -> return $ J.toJSON f
G.VString (G.StringValue t) -> return $ J.toJSON t
G.VBoolean b -> return $ J.toJSON b
G.VNull -> return J.Null
G.VEnum (G.EnumValue n) -> return $ J.toJSON n
G.VList (G.ListValueG vals) ->
J.toJSON <$> mapM toJValue vals
G.VObject (G.ObjectValueG objs) ->
J.toJSON . Map.fromList <$> mapM toTup objs
where
toTup (G.ObjectFieldG f v) = (f,) <$> toJValue v

valueParser
:: ( MonadError QErr m
, MonadReader ValidationCtx m)
Expand Down Expand Up @@ -121,8 +140,22 @@ valueParser =
pScalar (G.VBoolean b) = pVal $ J.Bool b
pScalar (G.VString sv) = pVal $ J.String $ G.unStringValue sv
pScalar (G.VEnum _) = throwVE "unexpected enum for a scalar"
pScalar (G.VObject _) = throwVE "unexpected object for a scalar"
pScalar (G.VList _) = throwVE "unexpected array for a scalar"
pScalar v = pVal =<< toJValue v

toJValueC :: G.ValueConst -> J.Value
toJValueC = \case
G.VCInt i -> J.toJSON i
G.VCFloat f -> J.toJSON f
G.VCString (G.StringValue t) -> J.toJSON t
G.VCBoolean b -> J.toJSON b
G.VCNull -> J.Null
G.VCEnum (G.EnumValue n) -> J.toJSON n
G.VCList (G.ListValueG vals) ->
J.toJSON $ map toJValueC vals
G.VCObject (G.ObjectValueG objs) ->
J.toJSON . Map.fromList $ map toTup objs
where
toTup (G.ObjectFieldG f v) = (f, toJValueC v)

constValueParser :: (MonadError QErr m) => InputValueParser G.ValueConst m
constValueParser =
Expand All @@ -148,8 +181,7 @@ constValueParser =
pScalar (G.VCBoolean b) = pVal $ J.Bool b
pScalar (G.VCString sv) = pVal $ J.String $ G.unStringValue sv
pScalar (G.VCEnum _) = throwVE "unexpected enum for a scalar"
pScalar (G.VCObject _) = throwVE "unexpected object for a scalar"
pScalar (G.VCList _) = throwVE "unexpected array for a scalar"
pScalar v = pVal $ toJValueC v

validateObject
:: ( MonadReader r m, Has TypeMap r
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- description: Inserts person data via GraphQL mutation (without Variable)
url: /v1alpha1/graphql
status: 200
response:
data:
insert_person:
returning:
- details:
name:
last: murphy
first: json

query:
query: |
mutation insert_person{
insert_person(
objects: [
{
details: {name: {first: json last: murphy}}
}
]
) {
returning {
details
}
}
}
- description: Delete the inserted persons
url: /v1/query
status: 200
query:
type: run_sql
args:
sql: |
delete from person;
SELECT setval('person_id_seq', 1, FALSE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
- description: Inserts persons data via GraphQL mutation (without variables)
url: /v1alpha1/graphql
status: 200
response:
data:
insert_person:
returning:
- details:
- name:
last: jaha
first: thelonious
- name:
last: griffin
first: clarke

query:
query: |
mutation insert_person{
insert_person(
objects: [
{
details: [
{name: {first: thelonious last: jaha}},
{name: {first: clarke last: griffin}}
]
}
]
) {
returning {
details
}
}
}
- description: Delete the inserted persons
url: /v1/query
status: 200
query:
type: run_sql
args:
sql: |
delete from person;
SELECT setval('person_id_seq', 1, FALSE);
8 changes: 8 additions & 0 deletions server/tests-py/test_graphql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def test_insert_using_array_variable(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/person_jsonb_variable_array.yaml")
hge_ctx.may_skip_test_teardown = True

def test_insert_person(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/person_jsonb.yaml")
hge_ctx.may_skip_test_teardown = True

def test_insert_person_array(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/person_jsonb_array.yaml")
hge_ctx.may_skip_test_teardown = True

def test_insert_null_col_value(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/order_col_shipped_null.yaml")
hge_ctx.may_skip_test_teardown = True
Expand Down