传递变量以使用复杂的输入类型进行查询 [英] Passing variables to query with complex input types

查看:12
本文介绍了传递变量以使用复杂的输入类型进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 graphQl 文档:

According to graphQl docs:

在代码中传递参数时,通常最好避免自己构建整个查询字符串.相反,您可以使用 $在查询中定义变量的语法,并将变量作为单独的地图.

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map.

https://graphql.org/graphql-js/passing-arguments/

在这种情况下,我试图查询以下架构:

In this context, I'm trying to query the following schema:

const typeDefs = "
input OrderInputData {    
  date: String!
  time: String!
  frequency: String!
  extras: [Int!]
  mailAlarm: String
  phoneAlarm: String
  rating: Float
  comments: String
}

type Mutation {
  updateIntent(
    paymentIntentId: String, 
    setupIntentId: String, 
    pairIntentId: String,
    orderInput: OrderInputData): Boolean
}"

我在构建符合 graphQl 更好实践的查询时遇到了一些困难,因为这种情况比文档中描述的情况更复杂,因为存在嵌套文档 (OrderInputData).这就是我所知道的:

I'm having a bit of difficulty to build the query respecting graphQl's better practices, since this case is more complex than the one depicted on the documentation, due to the presence of a nested document(OrderInputData). This is what I have up to know:

  const dummyData = {
    date: "11/11/2020",
    frequency: "One time",
    time: "6:17 PM",
    mailAlarm: "teste",
    phoneAlarm: "teste",
    extras: [1, 3, 7],
  };

const graphqlQuery = {
      query: `
      mutation updateIntent (
        $paymentIntentId: String, 
        $pairIntentId: String, 
        $orderInput: { 
          date: String!, 
          time: String!, 
          frequency: String!, 
          extras: [Int!], 
          mailAlarm: String, 
          phoneAlarm: String
        }) {
      updateIntent (
        paymentIntentId: $paymentIntentId, 
        pairIntentId: $pairIntentId, 
        orderInput: { 
          date: $date, 
          time: $time, 
          frequency: $frequency, 
          extras: $extras, 
          mailAlarm: $mailAlarm, 
          phoneAlarm: $phoneAlarm
        })
      }`,
      variables: {
        paymentIntentId: paymentIntentId, 
        pairIntentId: setupIntentId,
        "orderInput.date": dummyData.date, 
        "orderInput.time": dummyData.time, 
        "orderInput.frequency": dummyData.frequency, 
        "orderInput.extras": dummyData.extras, 
        "orderInput.mailAlarm": dummyData.mailAlarm, 
        "orderInput.phoneAlarm": dummyData.phoneAlarm
      }

我的错误是什么?

推荐答案

通常,为 [API, BE] 突变规范中定义的任何复杂输入类型准备 [并作为变量传递] 对象.

variables: {
    paymentIntentId: paymentIntentId, 
    pairIntentId: setupIntentId,
    orderInput: { date: orderInput.date,
    ...

... 或者在这种情况下(orderInput 之前定义)只是

... or in this case (orderInput defined earlier) simply

orderInput: orderInput

...或者当然(如果名称匹配)只是:

... or of course (if name matches) just:

variables: {
    paymentIntentId: paymentIntentId, 
    pairIntentId: setupIntentId,
    orderInput,
    ...

简单地在查询/变异中:

In query/mutation simply:

$orderInput: OrderInputData

关注

orderInput: $orderInput` 

这篇关于传递变量以使用复杂的输入类型进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆