简体   繁体   English

Graphql - 使用 Apollo 客户端 Angular 的突变操作不起作用

[英]Graphql - Mutation operation with Apollo Client Angular not working

I am working on a angular app with Graphql, The mutation operation on postman works fine, but when i tried to invoke the same in angular it throws an error as I am working on a angular app with Graphql, The mutation operation on postman works fine, but when i tried to invoke the same in angular it throws an error as

GraphQL request:2:21 1 | GraphQL 请求:2:21 1 | 2 | 2 | mutation createHero(item: {$id: ID,: $publisher, String:, $characters: String!, $superhero :String!}){突变 createHero(item: {$id: ID,: $publisher, String:, $characters: String!, $superhero :String!}){
| | ^ 3 | ^ 3 | createHero(item: {id: $id, publisher: $publisher, characters: $characters, superhero: $superhero} ) GraphQLError: Syntax Error: Expected "$", found Name "item". createHero(item: {id: $id, publisher: $publisher, characters: $characters, superhero: $superhero} ) GraphQLError:语法错误:预期为“$”,找到名称“item”。

Here is the working request这是工作请求

mutation{
  createHero (item:{ id:"test1", alter_ego:"hero", first_appearance:"cosmos", publisher:"azure", superhero:"test"}) {
     id
     superhero
      publisher
      characters
      alter_ego
      first_appearance
  }
}

and here is the code on Angular app,这是 Angular 应用程序上的代码,

const post_SaveHero = gql`
mutation createHero(item: {$id : ID!, $publisher : String!, $characters : String!, $superhero :String!}){
  createHero(item: {id: $id, publisher : $publisher , characters : $characters , superhero : $superhero} )
 {
  superhero
}
}`;

  addHero(hero: Hero) {
    return this.apollo.mutate({
      mutation: post_SaveHero,
      variables: {
        $publisher: hero.publisher,
        $characters: hero.characters,
        $superhero: hero.superhero,
        $id : hero.id,
        $alter_ego : hero.alter_ego,
        $first_appearance : hero.first_appearance,
        
      }
    });
  }

What is wrong here?这里有什么问题? Please help.请帮忙。

try to set id: String!尝试设置id: String! also I recommend you to add variables in your postman validations我还建议您在 postman 验证中添加变量

邮递员示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM