将变量传递给具有复杂输入类型的查询 [英] Passing variables to query with complex input types

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

问题描述

根据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,
    ...

只需查询/变异:

$orderInput: OrderInputData

之后

orderInput: $orderInput` 

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

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