graphql - 具有动态键的对象的 GraphQL 模式类型
问题描述
我使用外部 API 作为数据源,因此我无法控制返回记录中的键。我有一个这样的对象的记录:
images: {
'50': 'https://mir-s3-cdncf.behance.net.jpg',
'100': 'https://mir-s3-cdn-cf.behance.net/user/100/579c455d13419.jpg',
'115': 'https://mir-s3-cdn-cf.behance.net/user/115/.jpg',
'138': 'https://mir-s3-cdn-cf.behance.net/user/138/.jpg'
}
我无法预先知道这个对象中的键是什么。有没有办法在 GraphQL 模式中处理这个问题?
解决方案
我得到这个使用包“graphql-type-json”。这是使用该包的代码:
// Define new JSON types.
const AppTypes = `
scalar JSON
scalar JSONObject
`;
// Define resolvers for the new types which point to the types from the library.
const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json');
const AppResolvers = {
JSON: GraphQLJSON,
JSONObject: GraphQLJSONObject
};
// Use the types.
entityTree: [JSONObject]!
推荐阅读
- mysql - \n 不能发布到 MYSQL 服务器
- c++ - dijkstra 算法打印路径会跳过一些数字并且不适用于某些数字
- javascript - 元素的 css 样式完成转换后是否有回调?
- r - 如何使用多种模式在 R 中执行 filter_all
- javascript - 如何在反应离子中将道具从一个组件传递到另一个组件
- command-line - 使用命令行开关启动 Chromium 无效
- javascript - 在 IE11 的子窗口中打开新组件不起作用
- python - python中不和谐机器人的计划任务无法完成其工作
- django - 使用if语句过滤django模板中的外键内容
- swiftui - 为什么我的 UI 不能反映 @State 属性的变化?