27

我需要能够在一个请求中创建一个用户并添加它最喜欢的电影(一个引用电影集合的对象数组和他对每部电影的个人评分)。

可能看起来像这样的东西(伪代码)

var exSchema = `
 type Mutation {
    addUser(
        name: String!
        favMovies: [{ movie: String! #ref to movies coll
                      personal_rating: Int! # this is different for every movie
                   }]
    ) : User
 }
...
`

在单个请求中执行此操作的 graphql 方式是什么?我知道我可以通过多个突变/请求来实现结果,但我想一次性完成。

4

6 回答 6

55

你可以像这样传递一个数组

var MovieSchema = `
  type Movie {
   name: String
  }
  input MovieInput {
   name: String
  }
  mutation {
   addMovies(movies: [MovieInput]): [Movie]
  }
`

然后在你的突变中,你可以传递一个像

mutation {
  addMovies(movies: [{name: 'name1'}, {name: 'name2'}]) {
    name
  }
}

没有测试过代码,但你明白了

于 2017-01-05T09:15:02.850 回答
11

我想出了这个简单的解决方案——不使用 JSON。仅使用一个输入。希望它会帮助别人。

我必须添加到这种类型:

type Option {
    id: ID!
    status: String!
    products: [Product!]!
}

我们可以添加到突变类型并添加输入,如下所示:

type Mutation {
    createOption(data: [createProductInput!]!): Option!
    // other mutation definitions
}

input createProductInput {
    id: ID!
    name: String!
    price: Float!
    producer: ID!
    status: String
}

然后可以使用以下解析器:

const resolvers = {
    Mutation: {
      createOption(parent, args, ctx, info) {

        const status = args.data[0].status;

        // Below code removes 'status' from all array items not to pollute DB.
        // if you query for 'status' after adding option 'null' will be shown. 
        // But 'status': null should not be added to DB. See result of log below.
        args.data.forEach((item) => {
            delete item.status
        });

        console.log('args.data - ', args.data);

        const option = {
            id: uuidv4(),
            status: status,  // or if using babel    status,
            products: args.data
        }

        options.push(option)

        return option
      },
    // other mutation resolvers
    }

现在您可以使用它来添加一个选项(STATUS 取自数组中的第一项 - 它可以为空):

mutation{
  createOption(data:
    [{
            id: "prodB",
            name: "componentB",
            price: 20,
            producer: "e4",
            status: "CANCELLED"
        },
        {
            id: "prodD",
            name: "componentD",
            price: 15,
            producer: "e5"
        }
    ]
  ) {
    id
    status
    products{
      name
      price
    }
  }
}

产生:

{
  "data": {
    "createOption": {
      "id": "d12ef60f-21a8-41f3-825d-5762630acdb4",
      "status": "CANCELLED",
      "products": [
        {
          "name": "componentB",
          "price": 20,
        },
        {
          "name": "componentD",
          "price": 15,
        }
      ]
    }
  }
}

无需说要获得上述结果,您需要添加:

type Query {
    products(query: String): [Product!]!
    // others
}

type Product {
    id: ID!
    name: String!
    price: Float!
    producer: Company!
    status: String
}

我知道这不是最好的方法,但我没有在文档中找到这样做的方法。

于 2019-01-01T09:15:32.600 回答
7

我最终手动解析了正确的模式,因为 JavaScript 数组和 JSON.stringify 字符串不被接受为 graphQL 模式格式。

const id = 5;
const title = 'Title test';

let formattedAttachments = '';
attachments.map(attachment => {
  formattedAttachments += `{ id: ${attachment.id}, short_id: "${attachment.shortid}" }`;      
  // { id: 1, short_id: "abcxyz" }{ id: 2, short_id: "bcdqrs" }
});

// Query
const query = `
  mutation {
    addChallengeReply(
      challengeId: ${id}, 
      title: "${title}", 
      attachments: [${formattedAttachments}]
    ) {
      id
      title
      description
    }
  }
`;
于 2018-06-08T13:24:21.887 回答
1

我对您的要求的理解是,如果您有以下代码

const user = {
    name:"Rohit", 
    age:27, 
    marks: [10,15], 
    subjects:[
        {name:"maths"},
        {name:"science"}
    ]
};

const query = `mutation {
        createUser(user:${user}) {
            name
        }
}`

你一定会得到类似的东西

"mutation {
        createUser(user:[object Object]) {
            name
        }
}"

而不是预期的

"mutation {
        createUser(user:{
            name: "Rohit" ,
            age: 27 ,
            marks: [10 ,15 ] ,
            subjects: [
                {name: "maths" } ,
                {name: "science" } 
                ] 
            }) {
            name
        }
}"

如果这是您想要实现的,那么gqlast是一个不错的标签功能,您可以使用它来获得预期的结果

只需从此处获取 js 文件并将其用作:

const user = {
    name:"Rohit", 
    age:27, 
    marks: [10,15], 
    subjects:[
        {name:"maths"},
        {name:"science"}
    ]
};

const query = gqlast`mutation {
        createUser(user:${user}) {
            name
        }
}`

存储在变量中的结果query将是:

"mutation {
        createUser(user:{
            name: "Rohit" ,
            age: 27 ,
            marks: [10 ,15 ] ,
            subjects: [
                {name: "maths" } ,
                {name: "science" } 
                ] 
            }) {
            name
        }
}"
于 2019-05-10T18:17:08.903 回答
0

将它们作为 JSON 字符串传递。我就是做这个的。

于 2016-11-19T20:33:51.027 回答
0

对于那些不需要为一个请求传递数组并且愿意为每个突变发出请求的想法的人。(我用的是 Vue3,compisition Api,但是 React 和 Angular 的开发者还是可以理解的)。

不能像这样循环突变:

function createProject() {
    for (let i = 0; i < state.arrOfItems.length; i++) {
      const { mutate: addImplementation } = useMutation(
        post_dataToServer,
        () => ({
          variables: {
            implementation_type_id: state.arrOfItems[i],
            sow_id: state.newSowId,
          },
        })
      );

      addImplementation();
    }
}

这会给你一个错误,因为突变必须在 setup() 中。(这是您将收到的错误:https ://github.com/vuejs/vue-apollo/issues/888 )

而是创建一个子组件,并将数组映射到父组件中。

在 Parent.vue 中

<div v-for="(card, id) in state.arrOfItems">
  <ChildComponent
    :id="id"
    :card="card"
  />
</div>

在 ChildComponent.vue 中

收到道具和:

 const { mutate: addImplementation } = useMutation(
    post_dataToServer,
    () => ({
      variables: {
        implementation_id: props.arrOfItems,
        id: props.id,
      },
    })
  );
于 2021-10-11T14:47:17.107 回答