Mdd*_*Mdd 7 javascript node.js graphql graphql-js
如何为GraphQL中的字符串数组创建对象属性的模式?我希望响应看起来像这样:
{
name: "colors",
keys: ["red", "blue"]
}
Run Code Online (Sandbox Code Playgroud)
这是我的架构
var keysType = new graphql.GraphQLObjectType({
name: 'keys',
fields: function() {
key: { type: graphql.GraphQLString }
}
});
var ColorType = new graphql.GraphQLObjectType({
name: 'colors',
fields: function() {
return {
name: { type: graphql.GraphQLString },
keys: { type: new graphql.GraphQLList(keysType)
};
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此查询时,我得到一个错误,没有数据,错误就是 [{}]
查询{colors {name,keys}}
但是,当我运行查询只返回名称时,我得到了成功的响应.
查询{colors {name}}
如何在查询密钥时创建一个返回字符串数组的模式?
Mdd*_*Mdd 14
我想出了答案.关键是要graphql.GraphQLString进入graphql.GraphQLList()
架构变为:
var ColorType = new graphql.GraphQLObjectType({
name: 'colors',
fields: function() {
return {
name: { type: graphql.GraphQLString },
keys: { type: new graphql.GraphQLList(graphql.GraphQLString)
};
}
});
Run Code Online (Sandbox Code Playgroud)
使用此查询:
查询{colors {name,keys}}
我得到了预期的结果:
{
name: "colors",
keys: ["red", "blue"]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4908 次 |
| 最近记录: |