1
votes

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

1 Answers

1
votes

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