【问题标题】:Batch-mode Graphene responds with status 400 "Batch requests should receive a list" to a React-Apollo request批处理模式 Graphene 以状态 400“批处理请求应该收到一个列表”响应 React-Apollo 请求
【发布时间】:2018-05-31 00:51:21
【问题描述】:

React-Apollo 客户端(详见下文)向 Graphene-Django GraphQL 服务器发出的 GraphQL 请求失败为 Status Code 400 Bad Request,并显示错误消息:

Batch requests should receive a list, but received {...

这是为什么呢?

设置

有一个 GraphQL 服务器(使用 graphene-django)和一个正常运行的 GraphiQL;例如

query AllMsgsApp {
    allMessages {
        id
        message
    }
}

将产生:

{
  "data": {
    "allMessages": [
      {
        "id": "TWVzc2FnZVR5cGU6MQ=="
        "message": "Some message..."
      }
    ]
  }
}

前端是一个 React 应用程序(使用 create-react-app)和 apollo-client 等。

我在App.js 中的测试 sn-p 是:

import React, { Component } from 'react'
import { ApolloClient, InMemoryCache } from 'apollo-client-preset';
import { ApolloProvider } from 'react-apollo';
import { createHttpLink } from 'apollo-link-http';
import gql from 'graphql-tag'

const client = new ApolloClient({
  link: createHttpLink({ 
    uri: 'http://localhost:8000/gql/' }),
  cache: new InMemoryCache(),
});

client.query({
  query: gql`
    query AllMsgsApp {
      allMessages {
        id
        message
      }
    }
  `
}).then(response => console.log(response.data.allMessages))

一旦yarn start,得到的响应是400 Bad Request

{"errors":[{"message":"Batch requests should receive a list, but received {'operationName': 'AllMsgsApp', 'variables': {}, 'query': 'query AllMsgsApp {
  allMessages {
    id
    message
    __typename
  }
}
'}."}]}

依赖关系

"dependencies": {
    "apollo-cache-inmemory": "^1.1.4",
    "apollo-client": "^2.0.4",
    "apollo-client-preset": "^1.0.5",
    "apollo-link-http": "^1.3.1",
    "graphql": "^0.12.0",
    "graphql-tag": "^2.6.0",
    "react": "^16.2.0",
    "react-apollo": "^2.0.4",
    "react-dom": "^16.2.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17"
  }

额外

顺便说一下,以下内容:

curl -X POST -H "Content-Type: application/json" -d '{"query": "{ allMessages { id, message } }"}' http://localhost:8000/gql

基本上返回同样的错误:

{"errors":[{"message":"Batch requests should receive a list, but received {'query': '{ allMessages { id, message } }'}."}]}

然而,这个会返回预期的结果(注意封闭的[ ]):

curl -X POST -H "Content-Type: application/json" -d '[{"query": "{ allMessages { id, message } }"}]' http://localhost:8000/gql

这是:

[
  {
    "data":{
      "allMessages":[
        {
          "id":"TWVzc2FnZVR5cGU6MQ==",
          "message":"Some message..."
        }
      ]
    },
    "id":null,
    "status":200
  }
]

【问题讨论】:

    标签: graphql apollo react-apollo apollo-client


    【解决方案1】:

    不要使用createHttpLink,而是使用较新的BatchHttpLink

    像这样导入:

    import { BatchHttpLink } from "apollo-link-batch-http";

    const client = new ApolloClient({
      link: new BatchHttpLink({ 
        uri: 'http://localhost:8000/gql/' }),
      cache: new InMemoryCache(),
    });
    

    我希望这会有所帮助。这将发送来自 Apollo 的批处理请求,这是您期望从您的服务器发送的。

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 2019-01-20
      • 1970-01-01
      • 2018-08-10
      • 2023-02-09
      • 2014-08-19
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多