Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql:如何映射以数字开头的字段_Graphql - Fatal编程技术网

Graphql:如何映射以数字开头的字段

Graphql:如何映射以数字开头的字段,graphql,Graphql,现在正在学习GraphQL,在“映射”对象属性方面存在一些问题 我正在尝试映射这个对象 (注意:您可能会将时间戳更改为将来的时间戳,请在此处找到) 以下是JSON格式: { "0": { "arrets": [], "trips": [], "prevTime": 1544217780000, "nextTime": 1544229600000 }, "1": {} } 我的代码: const SheetT

现在正在学习GraphQL,在“映射”对象属性方面存在一些问题

我正在尝试映射这个对象

(注意:您可能会将时间戳更改为将来的时间戳,请在此处找到)

以下是JSON格式:

{
    "0": {
        "arrets": [],
        "trips": [],
        "prevTime": 1544217780000,
        "nextTime": 1544229600000
    },
    "1": {}
}
我的代码:

const SheetType = new GraphQLObjectType({
  name: "Sheet",
  fields: () => ({
    arrets: { type: ArretsType },
    trips: { type: TripsType },
    prevTime: { type: GraphQLString },
    nextTime: { type: GraphQLInt }
  })
});

const TimeSheetType = new GraphQLObjectType({
  name: "TimeSheet",
  fields: () => ({
    0: { type: SheetType },
    1: { type: SheetType }
  })
});
但是,我不知道如何映射对象,因为键是数字,不可能按数字映射字段()

我能做什么


谢谢。

如果API总是只返回键“0”和“1”,那么您可以有两个任意命名的字段映射回API返回的键

const TimeSheetType = new GraphQLObjectType({
  name: "TimeSheet",
  fields: () => ({
    zero: {
      type: SheetType,
      resolve: (parent) => parent['0'],
    },
    one: {
      type: SheetType,
      resolve: (parent) => parent['1'],
    },
  })
})
另一方面,如果API可以返回名为“2”、“3”、“4”等的键,那么更好的选择是更改数据结构的结构以使用数组。在这种情况下,您将完全忽略
时间表类型
,而只返回
SheetType
列表

// An example of how you can iterate over keys and reduce an object to an array
const convertToArray = (obj) => Object.keys(obj).reduce((arr, key) => {
  arr[Number.parseInt(key, 10)] = obj[key]
  return arr
}, [])

// Example query returning the array of SheetType objects
const QueryType = new GraphQLObjectType({
  name: "Query",
  fields: () => ({
    getTimeSheets: {
      type: new GraphQLList(SheetType),
      resolve: async () => {
        const apiResponse = await getAPIResponse()
        return convertToArray(apiResponse)
      },
    },
  })
})

如果API总是只返回键“0”和“1”,那么您可以有两个任意命名的字段映射回API返回的键

const TimeSheetType = new GraphQLObjectType({
  name: "TimeSheet",
  fields: () => ({
    zero: {
      type: SheetType,
      resolve: (parent) => parent['0'],
    },
    one: {
      type: SheetType,
      resolve: (parent) => parent['1'],
    },
  })
})
另一方面,如果API可以返回名为“2”、“3”、“4”等的键,那么更好的选择是更改数据结构的结构以使用数组。在这种情况下,您将完全忽略
时间表类型
,而只返回
SheetType
列表

// An example of how you can iterate over keys and reduce an object to an array
const convertToArray = (obj) => Object.keys(obj).reduce((arr, key) => {
  arr[Number.parseInt(key, 10)] = obj[key]
  return arr
}, [])

// Example query returning the array of SheetType objects
const QueryType = new GraphQLObjectType({
  name: "Query",
  fields: () => ({
    getTimeSheets: {
      type: new GraphQLList(SheetType),
      resolve: async () => {
        const apiResponse = await getAPIResponse()
        return convertToArray(apiResponse)
      },
    },
  })
})

什么是字段名是
7days
24hours
?GraphQL中的名称可以包括数字,但不能以数字开头。您必须将这些字段映射到类似于
sevenDays
等的内容。好吧,编写自定义解析程序:
{u 7days:{u=>{u['7days']}
…谢谢字段名是什么
7days
24hours
?GraphQL中的名称可以包含数字,但不能以数字开头。您必须将这些字段映射到类似于
sevenDays
等的内容。好吧,编写自定义解析程序:
{u7days:\u=>{'7days']}
。谢谢