gpt4 book ai didi

javascript - GraphQL 对象属性应该是一个字符串列表

转载 作者:数据小太阳 更新时间:2023-10-29 04:29:46 26 4
gpt4 key购买 nike

如何为 GraphQL 中的字符串数组对象属性创建架构?我希望响应看起来像这样:

{
name: "colors",
keys: ["red", "blue"]
}

这是我的架构

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)
};
}
});

当我运行此查询时出现错误且没有数据,错误只是[{}]

查询{颜色{名称、键}}

但是,当我运行查询以仅返回名称时,我得到了成功的响应。

查询{颜色{名称}}

当我查询键时,如何创建一个返回字符串数组的模式?

最佳答案

我想出了答案。关键是将 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)
};
}
});

使用这个查询:

查询{颜色{名称、键}}

我得到了想要的结果:

{
name: "colors",
keys: ["red", "blue"]
}

关于javascript - GraphQL 对象属性应该是一个字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com