-
Notifications
You must be signed in to change notification settings - Fork 2.8k
server: cache default values (fix #3773) #4620
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
Changes from all commits
89f424e
7aa8c40
0f5a73c
89d4a8b
22f9500
2d64d15
83e62bd
21dba0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,8 @@ module Hasura.GraphQL.Validate.Types | |
|
|
||
| , ReusableVariableTypes(..) | ||
| , ReusableVariableValues | ||
| , ReusableDefaultValue(..) | ||
| , ReusableVariableInfo(..) | ||
|
|
||
| , QueryReusability(..) | ||
| , _Reusable | ||
|
|
@@ -711,6 +713,7 @@ data AnnInpVal | |
| { _aivType :: !G.GType | ||
| , _aivVariable :: !(Maybe G.Variable) | ||
| , _aivValue :: !AnnGValue | ||
| , _aivDefault :: !(Maybe AnnGValue) | ||
| } deriving (Show, Eq) | ||
|
|
||
| type AnnGObject = OMap.InsOrdHashMap G.Name AnnInpVal | ||
|
|
@@ -781,10 +784,21 @@ stripTypenames = map filterExecDef | |
| in Just $ G.SelectionField f{G._fSelectionSet = newSelset} | ||
| _ -> Just s | ||
|
|
||
| data ReusableDefaultValue a | ||
| = ReusableNoDefault | ||
| | ReusableDefault a | ||
| deriving (Show, Eq) | ||
| $(J.deriveToJSON J.defaultOptions{J.constructorTagModifier = drop 8} ''ReusableDefaultValue) | ||
|
|
||
| data ReusableVariableInfo | ||
| = ReusableVariableInfo RQL.PGColumnType (ReusableDefaultValue PGScalarValue) | ||
| deriving (Show, Eq) | ||
| $(J.deriveToJSON J.defaultOptions{J.constructorTagModifier = drop 8} ''ReusableVariableInfo) | ||
|
Member
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. Why do we need the extra data ReusableVariableInfo
= ReusableVariableInfo
{ _rviType :: RQL.PGColumnType,
, _rviDefault :: Maybe PGScalarValue
} deriving (Show, Eq)
Contributor
Author
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. Because Put differently: suppose you get a
Member
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. Right, I see what you mean. In that case wouldn't the standard pattern be a newtype ReusableDefaultValue = ReusableDefaultValue PGScalarValue
data ReusableVariableInfo
= ReusableVariableInfo
{ _rviType :: RQL.PGColumnType,
, _rviDefault :: Maybe ReusableDefaultValue
} deriving (Show, Eq)i.e, why parameterize
Contributor
Author
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. With the f : Maybe ReusableDefaultValue -> a
f (Just (ReusableDefaultValue pgScalarValue)) = _
f Nothing = _So since data types are really cheap in Haskell, we might as well re-define Maybe to get a more convenient API: f : ReusableDefaultValue -> a
f (ReusableDefault pgScalarValue) = _
f ReusableNoDefault = _Note that the two patterns directly give a lot of information about why we are doing pattern matching, in contrast with the Anyway, this is not a critical point, so I'm happy to make this into a newtype if that allows this PR to get merged without further debate. |
||
|
|
||
| -- | Used by 'Hasura.GraphQL.Validate.validateVariablesForReuse' to parse new sets of variables for | ||
| -- reusable query plans; see also 'QueryReusability'. | ||
| newtype ReusableVariableTypes | ||
| = ReusableVariableTypes { unReusableVarTypes :: Map.HashMap G.Variable RQL.PGColumnType } | ||
| = ReusableVariableTypes { unReusableVarTypes :: Map.HashMap G.Variable ReusableVariableInfo } | ||
| deriving (Show, Eq, Semigroup, Monoid, J.ToJSON) | ||
| type ReusableVariableValues = Map.HashMap G.Variable (WithScalarType PGScalarValue) | ||
|
|
||
|
|
@@ -819,22 +833,23 @@ instance Monoid QueryReusability where | |
| mempty = Reusable mempty | ||
|
|
||
| class (Monad m) => MonadReusability m where | ||
| recordVariableUse :: G.Variable -> RQL.PGColumnType -> m () | ||
| recordVariableUse :: G.Variable -> RQL.PGColumnType -> ReusableDefaultValue PGScalarValue -> m () | ||
| markNotReusable :: m () | ||
|
|
||
| instance (MonadReusability m) => MonadReusability (ReaderT r m) where | ||
| recordVariableUse a b = lift $ recordVariableUse a b | ||
| recordVariableUse a b c = lift $ recordVariableUse a b c | ||
| markNotReusable = lift markNotReusable | ||
| instance (MonadReusability m) => MonadReusability (StateT s m) where | ||
| recordVariableUse a b = lift $ recordVariableUse a b | ||
| recordVariableUse a b c = lift $ recordVariableUse a b c | ||
| markNotReusable = lift markNotReusable | ||
|
|
||
| newtype ReusabilityT m a = ReusabilityT { unReusabilityT :: StateT QueryReusability m a } | ||
| deriving (Functor, Applicative, Monad, MonadError e, MonadReader r, MonadIO) | ||
|
|
||
| instance (Monad m) => MonadReusability (ReusabilityT m) where | ||
| recordVariableUse varName varType = ReusabilityT $ | ||
| modify' (<> Reusable (ReusableVariableTypes $ Map.singleton varName varType)) | ||
| recordVariableUse varName varType defaultVal = ReusabilityT $ | ||
| modify' (<> Reusable (ReusableVariableTypes $ | ||
| Map.singleton varName (ReusableVariableInfo varType defaultVal))) | ||
| markNotReusable = ReusabilityT $ put NotReusable | ||
|
|
||
| runReusabilityT :: ReusabilityT m a -> m (a, QueryReusability) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| - description: Test that default values are properly cached (#3773) | ||
| url: /v1/graphql | ||
| status: 200 | ||
| response: | ||
| data: | ||
| users: | ||
| - name: Alyssa | ||
| query: | ||
| query: | | ||
| query ($id:Int!=1) { | ||
| users (where: {id: {_eq: $id}}) { | ||
| name | ||
| } | ||
| } | ||
|
|
||
| - description: Test that default values are properly cached (#3773) | ||
| url: /v1/graphql | ||
| status: 200 | ||
| response: | ||
| data: | ||
| users: | ||
| - name: Alyssa | ||
| query: | ||
| query: | | ||
| query ($id:Int!=1) { | ||
| users (where: {id: {_eq: $id}}) { | ||
| name | ||
| } | ||
| } |
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.
Let's change
G.VariabletoVariableInfowhereVariableInforepresents(G.Variable, Maybe AnnGValue).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.
Currently, we seem to be adding this value here, after this change we don't need to do that anymore,
validateVariablesitself can build thisVariableInfoand add it to theAnnInpVal.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.
Your link doesn't work for me, unfortunately, so I am not sure what you are suggesting.