Apollo Query在GraphIQL中工作但不在调用graphql()中?

时间:2016-10-16 19:55:55

标签: graphql apollo apollostack

我有一个在localhost中正常工作的Apollo查询:3010 / graphiql:

QUERY

query getIMs($fromID: String!, $toID: String!){
  instant_message(fromID:$fromID, toID: $toID){
    fromID,
    toID,
    msgText
  }
}  

QUERY VARIABLES

{
  "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”查询:“查询getIMs($ fromID:String!,$ toID:   字符串!){↵instant_message(fromID:$ fromID,toID:$ toID){↵
  fromID↵到ID↵msgText↵__typename↵}↵}↵“   变量:{fromID:“DsmkoaYPeAumREsqC”,toID:“572bddac4ecbbac0ffe37fdd”}   fromID: “DsmkoaYPeAumREsqC”   TOID: “572bddac4ecbbac0ffe37fdd”

data对象带有ApolloError:

  

“网络错误:在位置0的JSON中出现意外的令牌<”

我该如何纠正?

更新

以下是“网络”标签的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

在Marc Greenstock和@neophi的帮助下,我找到了答案。我有这个代码设置Apollo Client:

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,
});