import React from "react";
import { render } from "react-dom";

import { ApolloProvider } from "@apollo/react-hooks";
import ApolloClient, { gql } from "apollo-boost";
import { InMemoryCache } from "apollo-cache-inmemory";

import App from "./src/App";

const domain =
  process.env.NODE_ENV === "development"
    ? "http://localhost:8080"
    : location.origin;

const cache = new InMemoryCache();
const client = new ApolloClient({
  cache,
  resolvers: {
    Mutation: {
      gotoChat: (_root, currentChat, { cache }) => {
        const query = gql`
          query GotoChatQuery {
            currentChat {
              workspace
              channel
            }
          }
        `;
        currentChat.__typename = "ChatInfo";
        cache.writeQuery({
          query,
          data: {
            currentChat
          }
        });
        return null;
      }
    }
  },
  uri: `${domain}/graphql`
});
cache.writeData({
  data: {
    currentChat: null
  }
});

render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.querySelector("main")
);
