【问题标题】:Graphql query arrayGraphql 查询数组
【发布时间】:2016-12-28 17:14:10
【问题描述】:

我正在尝试使用 graphql 获取坐标数组。我正在使用 relay、mongoose 和 graffiti-mongoose 从这些 mongoose 模型生成 graphql 类型:

// Geometry
const GeometrySchema = new mongoose.Schema({
type:         { type: String }, 
coordinates:  [ Number ]
}, { _id: false } );

// Country 
const CountrySchema = new mongoose.Schema({
code:           String,
name:           String,
dictionary:     [ { _id: false, value: String } ],
language:       [ { _id: false, code: String, name: String } ],
loc:            { type: { type: String }, geometries: [ GeometrySchema ] },
creationDate:   Date,
modifiedDate:   Date
});

const Country = mongoose.model('Country', CountrySchema);

这是生成的graphql(graphql/utilities):

type Country implements Node {
  code: String
  name: String
  dictionary: [CountryDictionary]
  language: [CountryLanguage]
  loc: [CountryLoc]
  creationDate: Date
  modifiedDate: Date
  _id: ID
  id: ID!
}

type CountryConnection {
  pageInfo: PageInfo!
  edges: [CountryEdge]
  count: Float
}

type CountryDictionary {
  _id: Generic
  value: String
}

input CountryDictionaryInput {
  _id: Generic
  value: String
}

type CountryEdge {
  node: Country
  cursor: String!
}

type CountryLanguage {
  _id: Generic
  code: String
  name: String
}

input CountryLanguageInput {
  _id: Generic
  code: String
  name: String
}

type CountryLoc {
  type: String
  coordinates: [Float]
}

input CountryLocInput {
  type: String
  coordinates: [Float]
}

使用 graphiql 我可以通过以下方式获取国家/地区名称:

{
  country(id:"57b48b73f6d183802aa06fe8"){
    name

  } 
}

我如何检索 loc 信息?

【问题讨论】:

  • 你试过{ country(id:"57b48b73f6d183802aa06fe8"){ name loc { coordinates } } }吗?
  • 是的,我试试,我得到了这样的响应:{ "statusCode": 400, "error": "Bad Request", "message": "Expected Iterable, but didn't find one for field Country.洛克。” }
  • 这意味着,问题与服务器端实现有关。检查您是否在服务器端正确返回坐标数组。

标签: mongodb mongoose graphql relay


【解决方案1】:

根据您的架构,查询将如下所示

{
  country(id:"57b48b73f6d183802aa06fe8") {
    name
    loc {
      type
      coordinates
    } 
  } 
}

【讨论】:

  • 您好,感谢您的回复。通过该查询,我得到了以下结果: { "statusCode": 400, "error": "Bad Request", "message": "Expected Iterable, but didn't find one for field Country.loc." }
  • 您的猫鼬CountrySchemaloc 属性定义为单个对象,而不是数组。并且您的类型架构具有定义为列表[CountryLoc] 的 loc 字段。您的模型根本不返回数组
  • 是的,你是对的。猫鼬模型没问题,但用于生成 graphql 类型的实用程序 graffiti-mongoose 对子文档有限制。谢谢。
【解决方案2】:

这似乎是模式创建中的一个可能的错误,我可以让它与这个模型一起工作(参见示例文件夹):https://github.com/tothandras/graffiti-mongoose/tree/bug/ric_castro_rada

【讨论】:

  • 谢谢安德拉斯。我将尝试使用该模型。
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2020-09-09
  • 2019-07-14
  • 2018-10-18
  • 2020-11-16
  • 2019-06-27
  • 2021-07-10
相关资源
最近更新 更多