import React, {Component} from 'react';
import { Input, Alert} from 'antd';
import GraphiQL from "graphiql";
import fetch from "isomorphic-fetch";


class Graphql extends Component {

  constructor() {
    super();
    this.state = {
      //api: 'http://service-ci2tk8iu-1254337200.ap-guangzhou.apigateway.myqcloud.com/release/graphql',
      // 便于开发调试，请删除并换成上面
      api: 'http://service-eucrnpse-1254337200.ap-guangzhou.apigateway.myqcloud.com/release/graphql'
    }
  };

  graphQLFetcher = (graphQLParams) => {
    // 已经存在的 magazine api， 设想: 用户生成 schema，返回 api 自动替换这里
    return fetch(this.state.api, {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify(graphQLParams),
    }).then(response => response.json());
  };

  render() {
    return (
      <div>
        <Alert style={{marginTop: 10}} message="下面的 graphql api 对应的是是 online 的数据库"
               type="warning" banner closable/>
        <Input style={{marginTop: 10}} addonBefore="POST" defaultValue={this.state.api}
               onChange={(e) => {
                 this.setState({api: e.target.value})
               }}/>
        <GraphiQL fetcher={this.graphQLFetcher}/>
      </div>
    )
  }

}

export default Graphql;