【问题标题】:How to return an array of strings in GraphQL如何在 GraphQL 中返回字符串数组
【发布时间】:2018-07-11 02:55:05
【问题描述】:

所以我想弄清楚如何在 GraphQL 中返回一个字符串数组。基本上我有一个包含依赖项的待办事项列表,因此对于每个项目,都有一个其他项目依赖 ID 的事物列表。我对这一切有点陌生,但我已经在 GraphQL 文档和此处进行了很多搜索,但还没有找到任何东西,所以如果这是一个愚蠢的问题,请提前道歉。我在解析函数中使用了 lodash。

// dummy data
var todos = [
  { name: 'Repair Driveway', dependencies: ['2' ,'3'], id: '1' },
  { name: 'Research driveway repair', dependencies: [], id: '2' },
  { name: 'Buy driveway repair tools', dependencies: ['2'], id: '3' },
  { name: 'Get groceries', dependencies: [], id: '4'}
];

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    deadline: { type: GraphQLDateTime },
    dependencies: { type:   } // This is where I'm not sure what to do.
  })
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    todo: {
      type: TodoType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args){
        console.log("Queried:" );
        return _.find(todos, { id: args.id });
      }
    }
  }
})

【问题讨论】:

    标签: javascript node.js lodash graphql


    【解决方案1】:

    GraphQL 提供了一个类,用于生成给定对象类型new GraphQLList(objectType) 的列表。

    你可以像这样使用它:

    const TodoType = new GraphQLObjectType({
      name: 'Todo',
      fields: () => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        deadline: { type: GraphQLDateTime },
        dependencies: { type: new GraphQLList(GraphQLString)  }
      })
    });
    

    【讨论】:

      【解决方案2】:

      试试这个:

      const TodoType = new GraphQLObjectType({
          name: 'Todo',
          fields: () => ({
              id: { type: GraphQLID },
              name: { type: GraphQLString },
              deadline: { type: GraphQLDateTime },
              dependencies: { type: [GraphQLString!] } // This is where I'm not sure what to do.
          })
      });
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2011-07-27
        • 2020-08-26
        • 1970-01-01
        • 1970-01-01
        • 2013-08-19
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        相关资源
        最近更新 更多