我正在使用apollo-fetch
来获取graphql
数据。
我结束了一个脚本错误,请求没有通过。我不确定在这样一个相当简单的请求中出了什么问题。请帮忙。
这是错误:
Uncaught (in promise) Error: Network request failed to return valid JSON
at throwHttpError (apollo-fetch.js:41)
at apollo-fetch.js:141
at <anonymous>
throwHttpError @ apollo-fetch.js:41
(anonymous) @ apollo-fetch.js:141
Promise rejected (async)
以下是代码:
const { createApolloFetch } = require('apollo-fetch');
PlayService.prototype.getPlays = function(playID) {
const fetch = createApolloFetch({
uri: 'graphql'
});
const playQuery = `query ($playid: String) {
plays(id: $playid) {
id
items {
nodes {
id
name
}
}
}
}`;
fetch({
query: playQuery,
variables: { playid: playID }
}).then((res) => {
console.log(res.data);
});
};
答案 0 :(得分:0)
我最好的猜测是你的uri值不正确。你应该提供像这样的完全合格的uri
PlayService.prototype.getPlays = function(playID) {
const fetch = createApolloFetch({
uri: 'http://localhost:3000/graphql'
});