Apollo Query 在 GraphIQL 中有效但在调用 graphql() 时无效?
Apollo Query Works in GraphIQL but Not in Call to graphql()?
我有一个在 localhost:3010/graphiql:
中正常工作的 Apollo 查询
查询
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
fromID,
toID,
msgText
}
}
查询变量
{
"fromID": "1",
"toID": "2"
}
这是我通过调用 graphql() 来 运行 查询的代码:
const GETIMS_QUERY = gql`
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
fromID,
toID,
msgText
}
} `;
const CreateIMPageWithDataAndMutations = graphql(GETIMS_QUERY, {
options({ toID, userID }) {
return {
variables: { fromID: `${userID}`, toID: `${toID}`}
};
}
})(CreateIMPageWithMutations);
Chrome 网络选项卡显示预期的请求负载:
operationName:"getIMs" query: "query getIMs($fromID: String!, $toID:
String!) {↵ instant_message(fromID: $fromID, toID: $toID) {↵
fromID↵ toID↵ msgText↵ __typename↵ }↵}↵"
variables:{fromID: "DsmkoaYPeAumREsqC", toID: "572bddac4ecbbac0ffe37fdd"}
fromID:"DsmkoaYPeAumREsqC"
toID:"572bddac4ecbbac0ffe37fdd"
但是 data
对象返回时出现 ApolloError:
"Network error: Unexpected token < in JSON at position 0"
我该如何纠正?
更新
这是“网络”选项卡的屏幕截图:
在 Marc Greenstock 和@neophi 的帮助下,我找到了答案。我用这段代码设置了 Apollo 客户端:
const networkInterface = createNetworkInterface({
uri: '/graphql',
opts: {
credentials: 'same-origin',
},
transportBatching: true,
});
这是相对定义 uri 并使用与 Meteor 运行 相同的端口 (3000)。但是 GraphQL 服务器当然 运行 在不同的端口上,在我的例子中是 3010。这修复了它:
const networkInterface = createNetworkInterface({
uri: 'http://localhost:3010/graphql',
opts: {
credentials: 'same-origin',
},
transportBatching: true,
});
我有一个在 localhost:3010/graphiql:
中正常工作的 Apollo 查询查询
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
fromID,
toID,
msgText
}
}
查询变量
{
"fromID": "1",
"toID": "2"
}
这是我通过调用 graphql() 来 运行 查询的代码:
const GETIMS_QUERY = gql`
query getIMs($fromID: String!, $toID: String!){
instant_message(fromID:$fromID, toID: $toID){
fromID,
toID,
msgText
}
} `;
const CreateIMPageWithDataAndMutations = graphql(GETIMS_QUERY, {
options({ toID, userID }) {
return {
variables: { fromID: `${userID}`, toID: `${toID}`}
};
}
})(CreateIMPageWithMutations);
Chrome 网络选项卡显示预期的请求负载:
operationName:"getIMs" query: "query getIMs($fromID: String!, $toID: String!) {↵ instant_message(fromID: $fromID, toID: $toID) {↵
fromID↵ toID↵ msgText↵ __typename↵ }↵}↵" variables:{fromID: "DsmkoaYPeAumREsqC", toID: "572bddac4ecbbac0ffe37fdd"} fromID:"DsmkoaYPeAumREsqC" toID:"572bddac4ecbbac0ffe37fdd"
但是 data
对象返回时出现 ApolloError:
"Network error: Unexpected token < in JSON at position 0"
我该如何纠正?
更新
这是“网络”选项卡的屏幕截图:
在 Marc Greenstock 和@neophi 的帮助下,我找到了答案。我用这段代码设置了 Apollo 客户端:
const networkInterface = createNetworkInterface({
uri: '/graphql',
opts: {
credentials: 'same-origin',
},
transportBatching: true,
});
这是相对定义 uri 并使用与 Meteor 运行 相同的端口 (3000)。但是 GraphQL 服务器当然 运行 在不同的端口上,在我的例子中是 3010。这修复了它:
const networkInterface = createNetworkInterface({
uri: 'http://localhost:3010/graphql',
opts: {
credentials: 'same-origin',
},
transportBatching: true,
});