package com.remy.graphql.resolvers.mutation;

import com.remy.common.module.GoodsDTO;
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 GoodsMutation implements GraphQLMutationResolver {

    @Autowired
    RestAPIEnv apiEnv;

    CompletableFuture<GoodsDTO> createGoods(GoodsDTO goodsDTO) {
        log.info("start create goods: " + BeanHelper.toJSON(goodsDTO));
        return supplyAsync(() ->
                new RestTemplate().postForObject(apiEnv.getGoodsEndpoint(),
                        goodsDTO, GoodsDTO.class));
    }

}
