【问题标题】:Is it possible to contain different keys in object schema?是否可以在对象模式中包含不同的键?
【发布时间】:2021-04-15 02:10:43
【问题描述】:

我有类型,例如“动物”,这个对象的键可以是任何类型,但值始终是 String 类型,例如我可以获得的一个响应:

   type Animal {
    breed: String
    eyes: String
    }

其他回复:

type Animal {
something: String
somethingElse: String
someOtherField: String
}

是否有可能以某种方式描述这个对象?

type Animal {
  // how i can describe this object?
}

type General {
  name: String
  example: Animal
}

【问题讨论】:

  • 这两个响应描述了两种不同的类型——它们都不能是 Animal 类型。这就是为什么您无法将它们“描述”为一组属性。
  • 重点是“Animal”对象可以有任意键
  • 对,我明白了,但是拥有一个具有随机键(如“wheels”、“controls”、“modelNumber”、“gearRatio”等)的 Animal“type”是没有意义的。在 Typescript 中描述了一组一致的属性,这些属性依赖于静态检查旨在采用类型“形状”的变量。如果没有你用你的想法破坏的“一致”部分,“类型检查”的整个想法就会被抛到窗外。但是,祝你好运。

标签: javascript graphql apollo graphql-js


【解决方案1】:

我不熟悉'graphql-js',所以不确定这是否有帮助,但可以列举一个普通的java对象如下:

var animal = {
    something: "one",
    somethingelse: "two",
    somethingother: "three"
}

for (var propName in animal)
{
    console.log(propName, animal[propName]);
}

输出:

"something", "one"
"somethingelse", "two"
"somethingother", "three"

【讨论】:

  • 不幸的是这不是我的情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
相关资源
最近更新 更多