0

Is it possible to pass an existing resolver into an Extension of an object method, so I can reuse the resolver? I am getting "Unexpected Execution Error : "No service for type 'UserPreferenceResolver' has been registered.", when executing a query., so I assume I either need todo some di or mark the resolver differently on the method signature.

"Users" and "UserPreferences" are stored separately, its a one to one relationship. I don't always want both, I have created a resolver for both objects. A new requirement has now been asked for, to pull both objects at the same time, so I created a UserModelExtention object to combine them. the individual Resolvers work fine and return the data. However when trying to use the Object ExtentionMethod "Test" I get the error.

Example Code

      public class UserModel
        {
         public Guid Id { get; set; }
         public string Name { get;set; }
        }
        
        public class UserPreferenceModel
        {
         public Guid UserId { get;set; }
         public bool AllowSms { get; set; }
         public bool AllowEmail { get; set; }
        }
        
        
        [ExtendObjectType(typeof(UserModel))]
        public class UserModelExtention
        {
         public async Task<UserPreferenceModel> Test([Parent] UserModel parent, [Service] 
         UserPreferenceResolver userPreferenceResolver)
                {
                    return await 
          userPreferenceResolver.GetUserPreferenceByIdAsync(default,parent.Id);
        
                }
        }
    
        [ExtendObjectType(typeof(Query))]
        public class UserResolver
        {
            private readonly IMapper _mapper;
    
            public UserResolver(IMapper mapper)
            {
                _mapper = mapper;
            }
    
    
            public async Task<UserModel> GetUserByIdAsync([Service] IUnifiedPortalRequestService uPService, Guid userId)
            {
                return _mapper.Map<UserModel>(await uPService.GetUser(userId.ToString()));
    
            }
        }
    
       [ExtendObjectType(typeof(Query))]
        public class UserPreferenceResolver
        {
            private readonly IMapper _mapper;
    
            public UserPreferenceResolver(IMapper mapper)
            {
                _mapper = mapper;
            }
    
            public async Task<UserPreferenceModel> GetUserPreferenceByIdAsync([Service] INotificationAPIService notificationAPIService, Guid userId)
            {
                return _mapper.Map<UserPreferenceModel>(await notificationAPIService.GetUserPreference(userId));
    
            }
    
        }

    Query {
              UserById(userId:""){
                  name
                 Test {
                 AllowEmail
               }
             } 
          }

it would be great to know if what I am trying todo is possible and if so how?

Mike Ross
  • 155
  • 1
  • 1
  • 9

1 Answers1

0

So one solution I have found that I can pass the IResolverContext context into the method and then use context.Resolver<UserPreferenceResolver>();

public async Task<UserPreferenceModel> Test(
    [Parent] UserModel parent, 
    IResolverContext context, 
    [Service] INotificationAPIService notificationAPIService
)
{
    var resolver = context.Resolver<UserPreferenceResolver>();
    
    return _mapper.Map<UserPreferenceModel>(
        await resolver.GetUserPreferenceByIdAsync(notificationAPIService, parent.Id)
    );
}
Joundill
  • 6,828
  • 12
  • 36
  • 50
Mike Ross
  • 155
  • 1
  • 1
  • 9