import {
  ApolloClient,
  InMemoryCache,
  gql,
  useQuery,
  createHttpLink,
} from "@apollo/client";
import { createUploadLink } from "apollo-upload-client";
import ApolloConfig from "../config/apollo-config";
import fetch from "cross-fetch";

function initApollo(token) {
  const httpLink = createUploadLink({
    uri: ApolloConfig.graphql_uri,
    fetch: fetch,
    headers:
      token != ""
        ? {
            Authorization: `Bearer ${token}`,
          }
        : null,
  });
  return new ApolloClient({
    ssrMode: false,
    cache: new InMemoryCache({
      addTypename: false,
    }),
    link: httpLink,
  });
}

async function query(query, token = "", variables = {}, cache = false) {
  const client = initApollo(token);
  var res;
  try {
    var sql = await client.query({
      query: gql`
        ${query}
      `,
      variables: variables,
      fetchPolicy: cache ? "cache-first" : "no-cache",
    });
    res = { STATUS: 1, DATA: sql.data };
  } catch (e) {
    res = { STATUS: 0, DATA: e };
  }
  return res;
}

async function mutation(mutation, token = "", variables = {}) {
  const client = initApollo(token);
  var res;
  try {
    var sql = await client.mutate({
      mutation: gql`
        ${mutation}
      `,
      variables: variables,
    });
    res = { STATUS: 1, DATA: sql.data };
  } catch (e) {
    res = { STATUS: 0, DATA: e };
  }
  return res;
}

module.exports = {
  query: query,
  mutation: mutation,
};
