【问题标题】:Custom resolve function returned undefined in grandstack自定义解析函数在大堆栈中返回未定义
【发布时间】:2021-01-27 08:55:43
【问题描述】:

我在schema.graphql 文件中定义了AB 的类型定义。这些解析器是自动生成的并且运行良好(在其他地方)。

为了创建一个评分机制,将标签 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 Playground 中运行查询时,我收到一个错误:

"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
  }
}

【问题讨论】:

    标签: javascript neo4j graphql apollo-server grandstack


    【解决方案1】:

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

      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
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多