Apollo/GraphQL 突变在 GraphIQL 中有效,但在客户端代码中无效?
Apollo/GraphQL Mutation Works in GraphIQL, but Not in Client Code?
我在 http://localhost:8080/graphiql:
的 GraphIQL 中使这个突变正常工作
查询
mutation($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
...和查询变量
{
"fromID": "1",
"toID": "2",
"msgText": "Test from GraphIQL #3. It's working!!!"
}
现在我需要在代码中实现它。
客户端代码
sendInstantMsg(){
const {textToSend} = this.refs;
const {toID} = this.props;
const fromID = Meteor.userId();
const msgText = trimInput(textToSend.getValue());
client.query({
query: gql`
query mutation($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`,
variables: {
fromID: fromID,
toID: toID,
msgText: msgText
},
forceFetch: false,
}).then(({ data }) => {
console.log('got data', data);
}).catch((error) => {
console.log('there was an error sending the query', error);
});
}
查询变量(fromID、toID 和 msgText)按预期进入函数,但 Apollo 抛出错误:
message: "Network error: Unexpected token < in JSON at position 0"
我错过了什么?
请试试这个..
你应该对突变而不是查询使用 mutate..
client.mutate({
mutation: gql`
mutation createIM($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`,
variables: {
fromID: fromID,
toID: toID,
msgText: msgText
},
forceFetch: false,
}).then(({ data }) => {
我在 http://localhost:8080/graphiql:
的 GraphIQL 中使这个突变正常工作查询
mutation($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
...和查询变量
{
"fromID": "1",
"toID": "2",
"msgText": "Test from GraphIQL #3. It's working!!!"
}
现在我需要在代码中实现它。
客户端代码
sendInstantMsg(){
const {textToSend} = this.refs;
const {toID} = this.props;
const fromID = Meteor.userId();
const msgText = trimInput(textToSend.getValue());
client.query({
query: gql`
query mutation($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`,
variables: {
fromID: fromID,
toID: toID,
msgText: msgText
},
forceFetch: false,
}).then(({ data }) => {
console.log('got data', data);
}).catch((error) => {
console.log('there was an error sending the query', error);
});
}
查询变量(fromID、toID 和 msgText)按预期进入函数,但 Apollo 抛出错误:
message: "Network error: Unexpected token < in JSON at position 0"
我错过了什么?
请试试这个.. 你应该对突变而不是查询使用 mutate..
client.mutate({
mutation: gql`
mutation createIM($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`,
variables: {
fromID: fromID,
toID: toID,
msgText: msgText
},
forceFetch: false,
}).then(({ data }) => {