1

I want to return a single object(not array).The graphql server is returning null when i try to return single object.But seems to work fine while trying to return an array.

Here is my schema example:

type Author{
     authorID: String
     label: String 
     posts: [Post]
   }

 type Post {
      postID: String
      label: String
      author: Author
 }

type Query {
       getAuthorById (authorID: String!): Author #-> this does not work
       getAuthorById (authorID: String!): [Author] #-> this works
 }

But when I try to run this query "getAuthorById (authorID: String!)",I am getting the following result:

 {
  "data": {
    "getAuthorById": {
            "label": null,
            "authorID": null
   }
 }

However,it seems to work only if i try to return an array (when I try to change the schema for type query like this):

type Query {
      getAuthorById (authorID: String!): [Author]
 }

Here is my resolver.js:

Query: {
 getAuthorById(_, params) {

   let session = driver.session();

   let query = `MATCH (a:Author{ authorID: $authorID})  RETURN a ;`

      return session.run(query, params)

     .then( result => {
       return result.records.map( record => {
         return record.get("a").properties
       }
     )
   }
  )
 },

}

What I need is to return a single object like this: getAuthorById (authorID: String!): Author

// Instead of array like this-> getAuthorById (authorID: String!): [Author]

so,could someone let me know what am i doing wrong here ? All I need is to return single object and not array .... Thanks in Advance

1 Answer 1

1

The problem is in your resolver, specifically you are returning the result of result.records.map() from the resolver. map() evaluates to an Array (applying the inner function to each element of result in this case.

Instead you can just grab the first Record in the Result stream:

.then( result => {
   return result.records[0].get("a").properties
   }
 )
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @William, it worked...Lastly, could you please point out any specific link where I could learn more regarding these properties and neo4j documentation cause I have read most of the neo4j blogs, but still never got a hold of useful insight as you provided. Thanks again !
Great! Docs for Neo4j JavaScript driver are here: neo4j.com/docs/api/javascript-driver/current

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.