使用node和express-graphql从pgPromise返回db.any?

时间:2019-07-05 01:40:24

标签: node.js postgresql graphql

寻求有关从Postgresql返回结果集的帮助。我知道返回结果集是一个记录数组。我不确定如何从pgPromise中的db.Any返回该结果。以下代码(db.then(res => res [0]))返回一条记录。当然,我需要所有与查询过滤器匹配的参数。

// Query Def

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  type: 'Query',
  fields: {
    matches: {
      type: MatchesAccountType,
      args: { trisyn_gl_combo: { type: GraphQLString } },
      resolve(parentValue, args) {
        const query = (`SELECT * FROM matches WHERE trisyn_gl_combo=$1`);
        const values = [args.trisyn_gl_combo];

        return db
          .any(query, values)
          .then(res => {return res[0]}) // <--Need all query results
          .catch(err => err);

      }
    }
  }
});

//Type Def

const MatchesAccountType = new GraphQLObjectType({
  name: "Matches",
  type: "Query",
  fields: {
    matched_id: { type: GraphQLString },
    created_at: { type: GraphQLString },
    batch_number: { type: GraphQLString },
    trans_code: { type: GraphQLString },
    orig_account_number: { type: GraphQLString },
    orig_cost_center: { type: GraphQLString },
    orig_combo: { type: GraphQLString },
    trans_date: { type: GraphQLString },
    tran_amount: { type: GraphQLString },
    report_date: { type: GraphQLString },
    trisyn_gl_combo: { type: GraphQLString },
    new_account: { type: GraphQLString },
    new_cc: { type: GraphQLString },
    new_combo: { type: GraphQLString },
    entity: { type: GraphQLString },
    affiliate: { type: GraphQLString },
    product_category: { type: GraphQLString },
    description: { type: GraphQLString },
    load_status: { type: GraphQLString }
  }
});

0 个答案:

没有答案