0

我有类型定义ABschema.graphql文件中定义。这些解析器是自动生成的并且工作良好(在其他地方)。

为了创建一个评分机制,将标签 B 的节点相对于标签 A 的节点进行排名,我正在编写一个CustomResolver查询,该查询执行密码查询并返回 bs 的集合和类型中定义的计算分数ScoredBs

schema.graphql文件看起来像这样。

type Query {
  CustomResolver(idA: ID!, idsB: [ID!]!): [ScoredBs]
}
type A {
  id: ID! @id
  # and some more stuff of course
}
type B {
  id: ID! @id
  # more fields that use the @relation or @cypher decorator
}
type ScoredBs {
  bs: [B]
  score: Float
}

这是定义自定义解析器的地方:

const resolvers = {
  Query: {
    CustomResolver: async (
      parent,
      { idA, idsB },
      context,
      info
    ) => {
      const cypher = `
      MATCH (a:A {id: $idA})
      MATCH (a)<--(b:B) WHERE b.id IN $idsB

      WITH
        b

      RETURN DISTINCT collect(b) AS bs, rand() AS score
      ORDER BY score DESC
      `
      const session = context.driver.session()
      const results = await session
        .run(cypher, { idA, idsB })
        .then((result) => {
          return result.records.map((record) => {
            return {
              bs: record.get('bs'),
              score: record.get('score')?.low || null,
            }
          })
        })
        .catch(console.log)
        .then((results) => {
          session.close()
          return results
        })
      return results
    },
  },
}

当我在 apollo-server graphql 操场上运行查询时,我收到一个错误:

"message": ""B.id" 的解析函数返回未定义",

query {
  CustomResolver(
    idA:"2560886f-654b-4047-8d7a-386cd7d9f670",
    idsB: ["01ec8367-a8ae-4600-9f88-ec141b2cae2c", "032f9a88-98c3-4968-8388-659ae26d63b3"]
  ) {
    bs {
      id
    }
    score
  }
}
4

1 回答 1

0

我通过重写部分代码解决了这个问题:

  const results = await session
    .run(cypher, { idA, idsB })
    .then((result) => {
      return result.records.map((record) => {
        // diff start
        const obj = record.toObject()
        const bs = obj.bs.map((b) => b.properties)
        return {
          bs, // diff end
          score: record.get('score')?.low || null,
        }
      })
    })
    .catch(console.log)
    .then((results) => {
      session.close()
      console.log(results)
      return results
    })
  return results
于 2020-10-13T05:52:31.037 回答