GraphiQL return 'null' on query

1.6k views Asked by At

I am actually experimenting with GraphQL and GraphiQL using a Neo4j DB and using an example from the neo4j_movie_recommendations code. I have a very strange problem which seems to be either javascript- or GraphQL-related. I do a simple query and whereas I can see that the data is available to be returned by the resolver (using console.log) GraphQL return a 'null' value. The following is the code segment:

const resolveFunctions = {
    Query: {
        allVoters(_, params) {
            let session = driver.session();
            let query = "MATCH (voter:Voter) RETURN voter;"
            return session.run(query, params)
                .then(result => {
                    return result.records.map(record => {
                        console.log("Voter: " + JSON.stringify(record))
                        return record.get("voter").properties
                    })
                })
        },

    },
    Voter: {
        memberOf(voter) {
            let session = driver.session(),
                params = { voterid: voter.voterid },
                query = `
                        MATCH (v:Voter)-[:MEMBER_OF]->(g:Group)
                        WHERE v.voterid = $voterid
                        RETURN g.name AS groupname;
                        `
            return session
                .run(query, params)
                .then(result => {
                    return result.records.map(record => {
                        console.log("Group Name " + record.get("groupname")) 
                        return record.get("groupname")
                    })
                })
        },

The console.log show the correct value but GraphiQL shows the name, email..correctly but the memberof field it show 'null'. Can anyone see what I am doing wrong here?

This is the Schema....

export default `
type Voter {
  voterid     : String!
  email       : String!
  name        : String!
  nickname    : [String]
  address     : String
  altAddress  : String
  dob         : String
  profession  : String
  telephone   : String
  gender      : String
  skills      : [String]
  votesIn     : PD
  livesIn     : PD 
  memberOf    : [Group]
}
type Group {
  name        : String
  location    : String
  created     : String
  partof      : Division
}
type PD {
  number      : String
  location    : String
  created     : String
  description : String
  partof      : Division 
}
type Division {
  name        : String
  created     : String
  belongsto   : Constituency
 }
 type Constituency {
  name        : String
  created     : String
  reportsto   : Region
 }
 type Region {
  name        : String
  created     : String
  reportsto   : Exec
 }
 type Exec {
  name        : String
  created     : String
 }

type Query {
  allVoters: [Voter]!
  getVoter(voterid: String!): Voter
  }

  schema {
    query: Query
  }
`;

EDIT...add Resolver Resolver code for getvoter:

  getVoter(_, params) {
            let session = driver.session();
            let query = "MATCH (voter:Voter {voterid: $voterid}) RETURN voter;"
            return session.run(query, params)
                .then(result => {
                    return result.records.map(record => {
                        return record.get("voter").properties
                    })
                })
        },
1

There are 1 answers

11
Daniel Rearden On BEST ANSWER

Whatever your resolver returns has to be the correct type as defined by your schema. If you return the wrong type (a number instead of a string), GraphQL may try to coerce it into the correct type, but in most cases it will just return null.

Your memberOf field is supposed to be an array of Group objects. However, in your resolver, it looks like you return an array of strings instead:

return result.records.map(record => {
  return record.get("groupname")
})

You'll need to utilize the toObject method or otherwise convert the record into an Object with fields matching those specified in your schema for Group.