Issues returning nested result with Graphql

Hello again.

I'm trying to return a (Post ) node using Graphql.
the Post type looks like this.

type Post {
  id: ID
  post_id: String
  user_id: String
  text: String
  post_type: String
  user_liked: Boolean
  author_info: User
  text_post: TextPost
}

and the User type looks like this.

type User {
  id: ID
  created_at: String
  display_name: String
  bio: String
  avatar: String
  default_profile_image: Boolean
  cover: String
  favourite_posts_count: Int @cypher(statement: "RETURN SIZE((this)-[:FAVOURITES]-())")
  user_id: ID
}

and my query looks like this.

fetchPosts(user_id: String, limit: Int, skip: Int): [Post]
  @cypher(
    statement"""MATCH (u:User)-[f:FOLLOWS]->(u2:User)-[c:CREATED]-(p:Post)
    WHERE u.user_id = $user_id
    
    WITH u, p, EXISTS((u)-[:LIKES]->(p)) AS isLiked,
    
    RETURN p {
        .created_at,
        .user_id,
        .post_id,
        .post_type,
        user_liked: isLiked,
        author_info: [ (p)-[CREATED]-(User) | author_info]
      } ORDER BY p.created_at DESC Skip $skip LIMIT $limit
  )

when I try to return the result without mentioning author_info the result are returned as expected but once I specify author_info like this for ex:

  author_info{
      username
    }

it gives me this error message

"message": "Variable `undefined` not defined (line 34, column 177 (offset: 1073))\n\"} ORDER BY p.created_at DESC Skip $skip LIMIT $limit\", {offset:$offset, first:$first, user_id:$user_id, limit:$limit, skip:$skip}, True) AS x UNWIND x AS `post` RETURN `post` {undefined} AS `post`\"\n     

and if I try to change author_info in the post type from

author_info: User

to

author_info: User @relation(name: "CREATED", direction: IN)

so my Post type looks like this (also I would prefer it to be like this, it's more convenient this way)

type Post {
  id: ID
  post_id: String
  user_id: String
  text: String
  post_type: String
  user_liked: Boolean
  author_info: User @relation(name: "CREATED", direction: IN)
  text_post: TextPost
}

then once I specify author_info on the query, it gives me this error.

"message": "Expected to find a node at ref slot 1 but found Map{post_id -> String(\"93ac89c0335f11eb9c33eb106b4a2825\"), created_at -> String(\"2020-12-01 00:03:14.223361\"), post_type -> String(\"text_post\"), user_id -> String(\"MI9B8M4AYkZgLXFNoKHkGAIUv4H2\")} instead"

like I've mentioned before I would prefer to specify the author_info's cypher query on the Post type but it gives me the latter problem and even when I change author_info's type to User and specify its cypher query on fetchpost() it gives me the first error.

Is there a way to mix the returned result to give me the result from isLiked while also calling author_info from the post type?. And if not, then how can I fix the first error? please.

Thank you for the help. And best regards.

@MuddyBootsCode, @William_Lyon Any help with this one?

As long as you include the @relation directives to map to the property graph model in the database your fetchposts Query field can just return the Post nodes and the nested selections should be handled automagically in the generated Cypher.

type Query {
  fetchPosts(user_id: String, limit: Int, skip: Int): [Post]
    @cypher(
      statement"""MATCH (u:User)-[f:FOLLOWS]->(u2:User)-[c:CREATED]-(p:Post)
        WHERE u.user_id = $user_id
        RETURN p ORDER BY p.created_at DESC Skip $skip LIMIT $limit"""
  )
}

You'd need to add a Cypher schema directive to the user_liked field to compute that value.

Note that this is using the older neo4j-graphql-js library, you might be interested in the new official @neo4j/graphql library which offers many new features and enhancements.

As for the author_info it does return the expected result with @relation directive. But when I want to add the user_liked to the Query function like this

RETURN {
user_liked: userLiked
}

this way it gives me the second error. As for userLiked I can't add a Cypher directive to its field, because it requires the user_id and the post_id and the user_id is passed to the fetchPost() function hence why userLiked is returned inside the function.