【问题标题】:How to write graphql query wiith custom objects如何使用自定义对象编写 graphql 查询
【发布时间】:2019-06-12 23:21:12
【问题描述】:

graphql的服务端是nodejs和express。这是 graphql 的架构。它有一个查询,它接受具有 from 和 to 日期的 DateT 对象。

var schema = buildSchema(`
    type Query {
        courseWithDate(
            timeFilter: DateT
        ): Course

    },
    type Course {
        ...
        from: String
        to: String
    },
    type DateT{
        from : String
        to : String
    }
`);

这就是我获得课程的方式

我可以使用这个 url 运行应用程序

localhost:4000/graphql

这是我正在使用的查询

query courseWithDate($from: dateFrom, $to: dateTo) {
    courseWithDate(timeFilter: {
      from: "${dateFrom}"
      to: "${dateTo}"
    })  {
        title
        ...
    }
}

使用这些参数

{ 
   "from": "2019-10-10","to":"2019-10-10"
}

我收到的异常消息与我尝试传递的输入类型有关。

{
  "errors": [
    {
      "message": "The type of Query.courseWithDate(timeFilter:) must be Input Type but got: DateT.",
      "locations": [
        {
          "line": 6,
          "column": 25
        }
      ]
    }
  ]
}

【问题讨论】:

    标签: graphql express-graphql


    【解决方案1】:

    我不确定,但可能这种风格看起来更像是最佳实践

    type Course {
      id: Int
      title: String
      author: String
      from: String
      to: String
      description: String
      topic: String
      url: String
    }
    
    input DateInput {
      dateFrom: String!
      dateTo: String!
    }
    
    type Query {
      courseWithDate(input: DateInput!, name: String!): Course
    }
    

    客户端的查询应该是:

      {
        courseWithDate(input: {
          dateFrom: "${dateFrom}"
          dateTo: "${dateTo}"
        }
        name: "${name}") 
        {
          id
          name
        }
      }
    

    【讨论】:

    • 我已经尝试过了,但在客户端我收到了一个异常“消息”:“Query.courseWithDate(timeFilter:) 的类型必须是输入类型,但得到:DateInput。”,。我的查询是 courseWithDate(id: $courseID, from : $from, to : $to) { id,... }
    • 还是同样的问题。我也用代码编辑了我的问题。这可能有助于回答这个问题。
    • 在您的架构中,尝试将type DateT 更改为input DateT
    • 错误 "message": "Unknown type \"dateFrom\". 你的意思是 \"DateT\"?", "message": "Unknown type \"dateTo\". 你的意思是 \ "DateT\"?", "message": "变量\"$from\" 从未用于操作\"courseWithDate\".", "message": "变量\"$to\" 从未用于操作\ "courseWithDate\".",
    猜你喜欢
    • 2017-12-23
    • 2019-06-27
    • 2020-07-04
    • 1970-01-01
    • 2021-02-14
    • 2018-05-31
    • 2017-01-31
    • 2019-06-12
    • 1970-01-01
    相关资源
    最近更新 更多