如何编写带有自定义对象的graphql查询 [英] How to write graphql query wiith custom objects

查看:299
本文介绍了如何编写带有自定义对象的graphql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

graphql的服务器端带有nodejs和express.这是graphql的架构.它具有一个查询,该查询接受具有日期和日期的DateT对象.

The server side of graphql is with nodejs and express. This is the schema for graphql. It has one query which accepts DateT object having from and to dates.

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

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

这就是我获得课程的方式

and this is how I am getting courses

我可以使用此网址运行应用程序

I am able to run the application with this 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"
}

我收到的异常消息与我要传递的输入类型有关.

Exception message I get is related to the input type I am trying to pass.

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

推荐答案

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

I'm not sure, but probably this style looks more like best practice

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
}

并且在客户端的查询应该是:

And Query on client side should be:

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

这篇关于如何编写带有自定义对象的graphql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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