GraphQL查询返回空白列表

时间:2019-05-16 16:27:52

标签: javascript graphql sequelize.js express-graphql

我正在使用sequelize在与乘员组关联的表中查询零件编号,然后使用这些编号查找该零件的显示名称。这些值将放入对象中并添加到零件列表中。我正在尝试将此列表返回给客户端。

我已经在console.log中记录了“ end”变量,以根据我的graphql类型查看输出是否正确,并且对我来说看起来正确。它是一个对象列表,每个对象都有part_num,名称和类别键,值对。

PartsListType GraphQL类型

const PartsListType = new GraphQLObjectType({
  name:"PartsListItem",
  fields: () => ({
    part_num: {
      type: GraphQLString
    },
    name: {
      type: GraphQLString
    },
    category: {
      type: GraphQLString
    }
  })
})

查询

crew_parts: {
  type: new GraphQLList(PartsListType),
  args: {
    crew: {
      type: new GraphQLNonNull(GraphQLString)
    }
  },
  async resolve(parent, args){
    const end = await partsCrewModel.findAll({
      raw: true,
      attributes: ['part_num', 'category'],
      where: {
        crew: args.crew
      }
    }).then(async partList => {
      let queryresults = []
      await partList.forEach(async part => {
         await partsModel.findAll({
           raw:true,
           attributes: ['sap_name'],
           where: {
             part_num: part.part_num
           }
         }).then(results => {
           queryresults.push({
             part_num: part.part_num,
             name: results[0].sap_name,
             category: part.category
           })
         })
         })
         return queryresults
    })
    console.log(end)
    return end
  }
}

Chrome日志输出

[]
0: {part_num: "4500344", name: "Packing (w/Brass Ring)", category: "Packing"}
1: {part_num: "2018298", name: "Discharge Seat", category: "Discharge Seat"}
2: {part_num: "2017692", name: "Clamp Plunger", category: "Plunger"}
3: {part_num: "2018299", name: "Discharge Valve ", category: "Discharge Valve"}
4: {part_num: "2018320", name: "Suction Seat", category: "Suction Seat"}
5: {part_num: "2018321", name: "Suction Valve", category: "Suction Valve"}

GraphiQL查询

query crewParts($crew: String!){
  crew_parts(crew: $crew){
    category
    name
    part_num
  }
}

GraphiQL输出

{
  "data": {
    "crew_parts": []
  }
}

我希望得到一个对象列表,但是从我的graphql查询中得到一个空列表。

0 个答案:

没有答案