cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Custom resolve function returned undefined in grandstack starter app

I have type definition for A and B defined in the schema.graphql file. Resolvers for these are auto-generated and work well.

To create a scoring mechanism that ranks nodes with label B in relation to the node with label A, I am writing a CustomResolver query that executes a cypher query and returns a collection of bs and a computed score as defined in the ScoredBs type.

The schema.graphql file looks like this.

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
}

This is where the custom resolver is defined:

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

When I run the query in the apollo-server graphql playground i am receiving an error:

"message": "Resolve function for "B.id" returned undefined",

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

I solved this by rewriting part of the code:

  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