package com.remy.graphql.resolvers.mutation;

import com.remy.common.module.UserDTO;
import com.remy.graphql.evn.RestAPIEnv;
import com.remy.graphql.helper.BeanHelper;
import graphql.kickstart.tools.GraphQLMutationResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.supplyAsync;

@Component
@Slf4j
public class UserMutation implements GraphQLMutationResolver {

    @Autowired
    RestAPIEnv apiEnv;

    CompletableFuture<UserDTO> createUser(UserDTO userDTO) {
        log.info("start create user: " + BeanHelper.toJSON(userDTO));
        return supplyAsync(() ->
                new RestTemplate().postForObject(apiEnv.getUsersEndpoint(),
                        userDTO, UserDTO.class));
    }
}
