Skip to content

[Question] How use NamedResolvers with custum execution context #818

@u900x600

Description

@u900x600

I am trying to create an GQL app following the »NamedResolver« Pattern, as shown in the docs and in this example.

I'd like to access the database Connection within a resolver function from a Reader Context. I cannot find anything the docs and I would be really glad to get some Details on how it is meant to work. I confess to find the types pretty complicated, so I am seeking for help.

Some code:

-- That should be the Context for the resolver
type MyApp = ReaderT Text IO

-- straight from the docs
data User m = User {
      id :: m Text
    , name :: m Text
  } deriving (Generic, GQLType)


-- added MonadReader Text m here
getUser :: (Monad m, MonadReader Text m) => ID -> m (Maybe (User (NamedResolverT m)))
getUser uid = pure $ 
  Just $ 
    User { 
      id = resolve (pure (unpackID uid)), 
      name = resolve (pure ("Test User for: " <> unpackID uid  ))
    }

-- how it should be
-- getUser uid = do
--    db <- get
--    …

instance (MonadReader Text m) => ResolveNamed m (User (NamedResolverT m)) where
  type Dep (User (NamedResolverT m)) = ID
  resolveBatched = traverse getUser

-- straight from the docs
data Query m = Query {
      users :: m [User m]
    , user :: Arg "id" ID -> m (Maybe (User m))
  } deriving (Generic, GQLType)

-- again, added MonadReader m
instance (MonadError GQLError m, MonadReader m) => ResolveNamed m (Query (NamedResolverT m)) where
  type Dep (Query (NamedResolverT m)) = ()
  resolveBatched _ =
    pure
      [ Just
          Query
            { users = resolve (pure ["1325", "2525"]),
              user = \(Arg arg) -> resolve (pure arg)
            }
      ]

rootResolver' :: NamedResolvers MyApp () Query Undefined Undefined
rootResolver' = NamedResolvers

results in that error:

Couldn't match type ‘morpheus-graphql-app-0.27.0:Data.Morpheus.App.Internal.Resolving.ResolverState.ResolverContext’
                 with ‘Text’
    arising from a functional dependency between:
      constraint ‘MonadReader
                    Text
                    (morpheus-graphql-app-0.27.0:Data.Morpheus.App.Internal.Resolving.Resolver.Resolver
                       morpheus-graphql-core-0.27.0:Data.Morpheus.Types.Internal.AST.OperationType.QUERY
                       ()
                       MyApp)’
        arising from a use of ‘NamedResolvers’
      instance ‘MonadReader
                  morpheus-graphql-app-0.27.0:Data.Morpheus.App.Internal.Resolving.ResolverState.ResolverContext
                  (morpheus-graphql-app-0.27.0:Data.Morpheus.App.Internal.Resolving.Resolver.Resolver
                     o e m)’
        at <no location info>
• In the expression: NamedResolvers
  In an equation for ‘rootResolver'’: rootResolver' = NamedResolverstypecheck(-Wdeferred-type-errors)

How should one change the Resolver Context, such that it is possible to access some parameters within the Resolver functions?

I have spent a long time going through the source, the docs and the examples but I was not able to figure out how this is meant to work and I would be happy for any sort of direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions