【问题标题】:Enable POST method to Laravel Folklore\GraphQL or enable GET method in react-apollo为 Laravel Folklore\GraphQL 启用 POST 方法或在 react-apollo 中启用 GET 方法
【发布时间】:2018-03-20 00:41:23
【问题描述】:

我尝试将 React 与 Laravel 的 Folklore\GraphQL 连接起来。

在 ReactJS 我有这个代码 ind index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker';

import { ApolloClient, ApolloProvider, createNetworkInterface } from 'react-apollo';
import Devices from './components/Devices.js'

const networkInterface = createNetworkInterface({
    uri: 'http://127.0.0.1:8000/graphiql',
    opts: {
        credentials: 'same-origin',
        mode: 'no-cors',
    },
});
const client = new ApolloClient({ networkInterface });

ReactDOM.render(
  <ApolloProvider client={client}>
    <Devices />
  </ApolloProvider>,
  document.getElementById('root')
)

Devices.js

import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

class Devices extends Component {
    render() {
        console.log(this.props);
        return(
            <div>Hello</div>
        );
    }
}

const query = gql`
{
    devices {
        id_device,
        name,
        type,
    }
}
`;
export default graphql(query)(Devices);

但显示错误:

POST http://127.0.0.1:8000/graphiql405(方法不允许)

如果我在opts 中添加到method:'GET',则显示以下错误:

未处理(在 react-apollo 中)错误:网络错误:执行失败 'Window' 上的'fetch':使用 GET/HEAD 方法的请求不能有正文。

如何将 POST 方法添加到 Folklore\GraphQL? 如何将方法设置为 GET 到 ApolloClient?

【问题讨论】:

    标签: reactjs laravel-5 graphql react-apollo


    【解决方案1】:

    错误是我的,因为你放了 GraphQL 接口的 URL。正确的 URL 是 http://127.0.0.1:8000/graphql

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 2018-11-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多