Skip to content

Commit fe03260

Browse files
committed
Don't pass schema through Resolver
1 parent 43c95b4 commit fe03260

File tree

2 files changed

+45
-56
lines changed

2 files changed

+45
-56
lines changed

src/GraphQL.hs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,7 @@ executeQuery
9494
executeQuery handler document name variables =
9595
case getOperation document name variables of
9696
Left e -> pure (ExecutionFailure (singleError e))
97-
Right operation ->
98-
case makeSchema @api of
99-
Left err -> pure (PreExecutionFailure (toError err :| []))
100-
Right allTypes ->
101-
-- TODO: At this point we know that the schema is valid (at least
102-
-- name-wise, which is all we check at the moment. Would be great to
103-
-- pass evidence of that down to resolvers.
104-
toResult <$> resolve @m @api allTypes handler (Just operation)
97+
Right operation -> toResult <$> resolve @m @api handler (Just operation)
10598
where
10699
toResult (Result errors result) =
107100
case result of

src/GraphQL/Resolver.hs

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import GraphQL.Value.ToValue (ToValue(..))
5454
import GraphQL.Internal.Name (Name, NameError(..), HasName(..), nameFromSymbol)
5555
import qualified GraphQL.Internal.OrderedMap as OrderedMap
5656
import GraphQL.Internal.Output (GraphQLError(..))
57-
import GraphQL.Internal.Schema (Schema)
5857
import GraphQL.Internal.Validation
5958
( SelectionSetByType
6059
, SelectionSet(..)
@@ -144,10 +143,7 @@ ok = pure
144143

145144
class HasResolver m a where
146145
type Handler m a
147-
-- XXX: Might be nicer to make Schema a ReaderT -- easier to ignore when
148-
-- not wanted, and wouldn't have required me to change so many call sites.
149-
-- OTOH, with all our type weirdness, I got bored trying to figure it out.
150-
resolve :: Schema -> Handler m a -> Maybe (SelectionSetByType Value) -> m (Result Value)
146+
resolve :: Handler m a -> Maybe (SelectionSetByType Value) -> m (Result Value)
151147

152148
-- | Specify a default value for a type in a GraphQL schema.
153149
--
@@ -184,41 +180,41 @@ instance Defaultable (Maybe a) where
184180

185181
instance forall m. (Applicative m) => HasResolver m Int32 where
186182
type Handler m Int32 = m Int32
187-
resolve _ handler Nothing = map (ok . toValue) handler
188-
resolve _ _ (Just ss) = throwE (SubSelectionOnLeaf ss)
183+
resolve handler Nothing = map (ok . toValue) handler
184+
resolve _ (Just ss) = throwE (SubSelectionOnLeaf ss)
189185

190186
instance forall m. (Applicative m) => HasResolver m Double where
191187
type Handler m Double = m Double
192-
resolve _ handler Nothing = map (ok . toValue) handler
193-
resolve _ _ (Just ss) = throwE (SubSelectionOnLeaf ss)
188+
resolve handler Nothing = map (ok . toValue) handler
189+
resolve _ (Just ss) = throwE (SubSelectionOnLeaf ss)
194190

195191
instance forall m. (Applicative m) => HasResolver m Text where
196192
type Handler m Text = m Text
197-
resolve _ handler Nothing = map (ok . toValue) handler
198-
resolve _ _ (Just ss) = throwE (SubSelectionOnLeaf ss)
193+
resolve handler Nothing = map (ok . toValue) handler
194+
resolve _ (Just ss) = throwE (SubSelectionOnLeaf ss)
199195

200196
instance forall m. (Applicative m) => HasResolver m Bool where
201197
type Handler m Bool = m Bool
202-
resolve _ handler Nothing = map (ok . toValue) handler
203-
resolve _ _ (Just ss) = throwE (SubSelectionOnLeaf ss)
198+
resolve handler Nothing = map (ok . toValue) handler
199+
resolve _ (Just ss) = throwE (SubSelectionOnLeaf ss)
204200

205201
-- XXX: jml really doesn't understand this. What happens to the selection set? What if it's a nullable object?
206202
instance forall m hg. (HasResolver m hg, Functor m, ToValue (Maybe hg)) => HasResolver m (Maybe hg) where
207203
type Handler m (Maybe hg) = m (Maybe hg)
208-
resolve _ handler _ = map (ok . toValue) handler
204+
resolve handler _ = map (ok . toValue) handler
209205

210206
instance forall m hg. (Monad m, Applicative m, HasResolver m hg) => HasResolver m (API.List hg) where
211207
type Handler m (API.List hg) = m [Handler m hg]
212-
resolve schema handler selectionSet = do
208+
resolve handler selectionSet = do
213209
h <- handler
214-
let a = traverse (flip (resolve @m @hg schema) selectionSet) h
210+
let a = traverse (flip (resolve @m @hg) selectionSet) h
215211
map aggregateResults a
216212

217213

218214
instance forall m ksN enum. (Applicative m, API.GraphQLEnum enum) => HasResolver m (API.Enum ksN enum) where
219215
type Handler m (API.Enum ksN enum) = enum
220-
resolve _ handler Nothing = (pure . ok . GValue.ValueEnum . API.enumToValue) handler
221-
resolve _ _ (Just ss) = throwE (SubSelectionOnLeaf ss)
216+
resolve handler Nothing = (pure . ok . GValue.ValueEnum . API.enumToValue) handler
217+
resolve _ (Just ss) = throwE (SubSelectionOnLeaf ss)
222218

223219
-- TODO: A parametrized `Result` is really not a good way to handle the
224220
-- "result" for resolveField, but not sure what to use either. Tom liked the
@@ -240,14 +236,14 @@ type family FieldName (a :: Type) = (r :: Symbol) where
240236

241237
resolveField :: forall dispatchType (m :: Type -> Type).
242238
(BuildFieldResolver m dispatchType, Monad m, KnownSymbol (FieldName dispatchType))
243-
=> Schema -> FieldHandler m dispatchType -> m ResolveFieldResult -> Field Value -> m ResolveFieldResult
244-
resolveField schema handler nextHandler field =
239+
=> FieldHandler m dispatchType -> m ResolveFieldResult -> Field Value -> m ResolveFieldResult
240+
resolveField handler nextHandler field =
245241
-- check name before
246242
case nameFromSymbol @(FieldName dispatchType) of
247243
Left err -> pure (Result [SchemaError err] (Just GValue.ValueNull))
248244
Right name'
249245
| getName field == name' ->
250-
case buildFieldResolver @m @dispatchType schema handler field of
246+
case buildFieldResolver @m @dispatchType handler field of
251247
Left err -> pure (Result [err] (Just GValue.ValueNull))
252248
Right resolver -> do
253249
Result errs value <- resolver
@@ -275,13 +271,13 @@ type family FieldHandler (m :: Type -> Type) (a :: Type) = (r :: Type) where
275271
FieldHandler m (EnumArgument (API.Argument ksF (API.Enum name t)) f) = t -> FieldHandler m f
276272

277273
class BuildFieldResolver m fieldResolverType where
278-
buildFieldResolver :: Schema -> FieldHandler m fieldResolverType -> Field Value -> Either ResolverError (m (Result Value))
274+
buildFieldResolver :: FieldHandler m fieldResolverType -> Field Value -> Either ResolverError (m (Result Value))
279275

280276
instance forall ksG t m.
281277
( KnownSymbol ksG, HasResolver m t, HasAnnotatedType t, Monad m
282278
) => BuildFieldResolver m (JustHandler (API.Field ksG t)) where
283-
buildFieldResolver schema handler field = do
284-
pure (resolve @m @t schema handler (getSubSelectionSet field))
279+
buildFieldResolver handler field = do
280+
pure (resolve @m @t handler (getSubSelectionSet field))
285281

286282
instance forall ksH t f m.
287283
( KnownSymbol ksH
@@ -291,13 +287,13 @@ instance forall ksH t f m.
291287
, HasAnnotatedInputType t
292288
, Monad m
293289
) => BuildFieldResolver m (PlainArgument (API.Argument ksH t) f) where
294-
buildFieldResolver schema handler field = do
290+
buildFieldResolver handler field = do
295291
argument <- first SchemaError (API.getArgumentDefinition @(API.Argument ksH t))
296292
let argName = getName argument
297293
value <- case lookupArgument field argName of
298294
Nothing -> valueMissing @t argName
299295
Just v -> first (InvalidValue argName) (fromValue @t v)
300-
buildFieldResolver @m @f schema (handler value) field
296+
buildFieldResolver @m @f (handler value) field
301297

302298
instance forall ksK t f m name.
303299
( KnownSymbol ksK
@@ -307,13 +303,13 @@ instance forall ksK t f m name.
307303
, API.GraphQLEnum t
308304
, Monad m
309305
) => BuildFieldResolver m (EnumArgument (API.Argument ksK (API.Enum name t)) f) where
310-
buildFieldResolver schema handler field = do
306+
buildFieldResolver handler field = do
311307
argName <- first SchemaError (nameFromSymbol @ksK)
312308
value <- case lookupArgument field argName of
313309
Nothing -> valueMissing @t argName
314310
Just (ValueEnum enum) -> first (InvalidValue argName) (API.enumFromValue @t enum)
315311
Just value -> Left (InvalidValue argName (show value <> " not an enum: " <> show (API.enumValues @t)))
316-
buildFieldResolver @m @f schema (handler value) field
312+
buildFieldResolver @m @f (handler value) field
317313

318314
-- Note that we enumerate all ks variables with capital letters so we
319315
-- can figure out error messages like the following that don't come
@@ -348,7 +344,7 @@ class RunFields m a where
348344
-- Individual implementations are responsible for calling 'runFields' if
349345
-- they haven't matched the field and there are still candidate fields
350346
-- within the handler.
351-
runFields :: Schema -> RunFieldsHandler m a -> Field Value -> m ResolveFieldResult
347+
runFields :: RunFieldsHandler m a -> Field Value -> m ResolveFieldResult
352348

353349
instance forall f fs m dispatchType.
354350
( BuildFieldResolver m dispatchType
@@ -357,19 +353,19 @@ instance forall f fs m dispatchType.
357353
, KnownSymbol (FieldName dispatchType)
358354
, Monad m
359355
) => RunFields m (f :<> fs) where
360-
runFields schema (handler :<> nextHandlers) field =
361-
resolveField @dispatchType @m schema handler nextHandler field
356+
runFields (handler :<> nextHandlers) field =
357+
resolveField @dispatchType @m handler nextHandler field
362358
where
363-
nextHandler = runFields @m @fs schema nextHandlers field
359+
nextHandler = runFields @m @fs nextHandlers field
364360

365361
instance forall ksM t m dispatchType.
366362
( BuildFieldResolver m dispatchType
367363
, KnownSymbol ksM
368364
, dispatchType ~ FieldResolverDispatchType (API.Field ksM t)
369365
, Monad m
370366
) => RunFields m (API.Field ksM t) where
371-
runFields schema handler field =
372-
resolveField @dispatchType @m schema handler nextHandler field
367+
runFields handler field =
368+
resolveField @dispatchType @m handler nextHandler field
373369
where
374370
nextHandler = pure (Result [FieldNotFoundError (getName field)] Nothing)
375371

@@ -379,8 +375,8 @@ instance forall m a b dispatchType.
379375
, KnownSymbol (FieldName dispatchType)
380376
, Monad m
381377
) => RunFields m (a :> b) where
382-
runFields schema handler field =
383-
resolveField @dispatchType @m schema handler nextHandler field
378+
runFields handler field =
379+
resolveField @dispatchType @m handler nextHandler field
384380
where
385381
nextHandler = pure (Result [FieldNotFoundError (getName field)] Nothing)
386382

@@ -391,16 +387,16 @@ instance forall typeName interfaces fields m.
391387
) => HasResolver m (API.Object typeName interfaces fields) where
392388
type Handler m (API.Object typeName interfaces fields) = m (RunFieldsHandler m (RunFieldsType m fields))
393389

394-
resolve _ _ Nothing = throwE MissingSelectionSet
395-
resolve schema mHandler (Just selectionSet) =
390+
resolve _ Nothing = throwE MissingSelectionSet
391+
resolve mHandler (Just selectionSet) =
396392
case getSelectionSet of
397393
Left err -> throwE err
398394
Right ss -> do
399395
-- Run the handler so the field resolvers have access to the object.
400396
-- This (and other places, including field resolvers) is where user
401397
-- code can do things like look up something in a database.
402398
handler <- mHandler
403-
r <- traverse (runFields @m @(RunFieldsType m fields) schema handler) ss
399+
r <- traverse (runFields @m @(RunFieldsType m fields) handler) ss
404400
let (Result errs obj) = GValue.objectFromOrderedMap . OrderedMap.catMaybes <$> sequenceA r
405401
pure (Result errs (GValue.ValueObject obj))
406402

@@ -411,7 +407,7 @@ instance forall typeName interfaces fields m.
411407
-- inline fragments or the use of fragment spreads. These type
412408
-- conditions are represented in the schema by the name of a type
413409
-- (e.g. "Dog"). To determine which type conditions (and thus which
414-
-- fields) are relevant for this selection set, we need to look up the
410+
-- fields) are relevant for this 1selection set, we need to look up the
415411
-- actual types they refer to, as interfaces (say) match objects
416412
-- differently than unions.
417413
--
@@ -456,7 +452,7 @@ type role DynamicUnionValue representational representational
456452
data DynamicUnionValue (union :: Type) (m :: Type -> Type) = DynamicUnionValue { _label :: Text, _value :: GHC.Exts.Any }
457453

458454
class RunUnion m union objects where
459-
runUnion :: Schema -> DynamicUnionValue union m -> SelectionSetByType Value -> m (Result Value)
455+
runUnion :: DynamicUnionValue union m -> SelectionSetByType Value -> m (Result Value)
460456

461457
instance forall m union objects name interfaces fields.
462458
( Monad m
@@ -466,10 +462,10 @@ instance forall m union objects name interfaces fields.
466462
, API.HasObjectDefinition (API.Object name interfaces fields)
467463
, RunUnion m union objects
468464
) => RunUnion m union (API.Object name interfaces fields:objects) where
469-
runUnion schema duv selectionSet =
465+
runUnion duv selectionSet =
470466
case extractUnionValue @(API.Object name interfaces fields) @union @m duv of
471-
Just handler -> resolve @m @(API.Object name interfaces fields) schema handler (Just selectionSet)
472-
Nothing -> runUnion @m @union @objects schema duv selectionSet
467+
Just handler -> resolve @m @(API.Object name interfaces fields) handler (Just selectionSet)
468+
Nothing -> runUnion @m @union @objects duv selectionSet
473469

474470
-- AFAICT it should not be possible to ever hit the empty case because
475471
-- the compiler doesn't allow constructing a unionValue that's not in
@@ -479,7 +475,7 @@ instance forall m union objects name interfaces fields.
479475
-- We still need to implement this instance for the compiler because
480476
-- it exhaustively checks all cases when deconstructs the Union.
481477
instance forall m union. RunUnion m union '[] where
482-
runUnion _ (DynamicUnionValue label _) selection =
478+
runUnion (DynamicUnionValue label _) selection =
483479
panic ("Unexpected branch in runUnion, got " <> show selection <> " for label " <> label <> ". Please file a bug.")
484480

485481
instance forall m unionName objects.
@@ -488,10 +484,10 @@ instance forall m unionName objects.
488484
, RunUnion m (API.Union unionName objects) objects
489485
) => HasResolver m (API.Union unionName objects) where
490486
type Handler m (API.Union unionName objects) = m (DynamicUnionValue (API.Union unionName objects) m)
491-
resolve _ _ Nothing = throwE MissingSelectionSet
492-
resolve schema mHandler (Just selectionSet) = do
487+
resolve _ Nothing = throwE MissingSelectionSet
488+
resolve mHandler (Just selectionSet) = do
493489
duv <- mHandler
494-
runUnion @m @(API.Union unionName objects) @objects schema duv selectionSet
490+
runUnion @m @(API.Union unionName objects) @objects duv selectionSet
495491

496492
symbolText :: forall ks. KnownSymbol ks => Text
497493
symbolText = toS (symbolVal @ks Proxy)

0 commit comments

Comments
 (0)