使用数组的元素设置新对象的字段

时间:2019-07-17 14:49:36

标签: typescript

我正在尝试创建一个NewInput类型的新对象(下面的接口):

interface NewInput {
  title: String;
  tags?: String[];
  description?: String;
  queries: QueryInput[];
  meta: MetaDataInput[];
  variables: VariableInput[];
}

现在,我正在使用GraphQL自省功能从类型数据库中查找NewInput的字段,因此我不必对其进行硬编码。我正在将自省的输出放入看起来像这样的string[]

 [ 'title: String',
   'tags?: String[]',
   'description?: String',
   'queries: QueryInput[]',
   'meta: MetaDataInput[]',
   'variables: VariableInput[]' ]`

是否有办法将此数组(称为fields)转换为类型为NewInput的新对象的字段?另外,最好为每个字段提供值(例如title:“这是标题”,依此类推)。

这也是JSON文件的一个片段,我正在从其中获取fields数组:

{
    "kind": "INPUT_OBJECT",
    "name": "NewInput",
    "description": "",
    "fields": null,
    "inputFields": [
      {
        "name": "title",
        "description": "",
        "type": {
          "kind": "NON_NULL",
          "name": null,
          "ofType": {
            "kind": "SCALAR",
            "name": "String",
            "ofType": null
          }
        },
        "defaultValue": null
      },
      {
        "name": "tags",
        "description": "",
        "type": {
          "kind": "LIST",
          "name": null,
          "ofType": {
            "kind": "SCALAR",
            "name": "String",
            "ofType": null
          }
        },
        "defaultValue": null
      },
      {
        "name": "description",
        "description": "",
        "type": {
          "kind": "SCALAR",
          "name": "String",
          "ofType": null
        },
        "defaultValue": null
      },
      {
        "name": "queries",
        "description": "",
        "type": {
          "kind": "NON_NULL",
          "name": null,
          "ofType": {
            "kind": "LIST",
            "name": null,
            "ofType": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "INPUT_OBJECT",
                "name": "QueryInput",
                "ofType": null
              }
            }
          }
        },
        "defaultValue": null
      },
      {
        "name": "meta",
        "description": "",
        "type": {
          "kind": "LIST",
          "name": null,
          "ofType": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "INPUT_OBJECT",
              "name": "MetaDataInput",
              "ofType": null
            }
          }
        },
        "defaultValue": null
      },
      {
        "name": "variables",
        "description": "",
        "type": {
          "kind": "LIST",
          "name": null,
          "ofType": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "INPUT_OBJECT",
              "name": "VariableInput",
              "ofType": null
            }
          }
        },
        "defaultValue": null
      }
    ],
    "interfaces": null,
    "enumValues": null,
    "possibleTypes": null
  },

0 个答案:

没有答案