Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphql is only resolving _id field, other fields are null

GraphQL is resolving my title and content properties as null, though the data is definitely being retrieved from the server - the console log in the confirms it. Only the _id property is returned from graphql, this is what I get back from the query json { listings: [ { _id: '56e6c94f1cf94a7c0a4e22ba', title: null, content: null } ] } As far as I can tell everything is set up correctly, I also tried giving title and content a GraphQLIDType to rule out the differences it types.

I have a graphql query:

query(`
  query findListings {
    listings(offset: 1) {
      _id,
      title,
      content
    }
  }
`).then((json) => {
  console.log('json', json.data)
})

my root query type:

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    listings: {
      name: 'listings',
      type: new GraphQLList(ListingType),
      args: {
        limit: {
          type: GraphQLInt
        },
        offset: {
          type: GraphQLInt
        }
      },
      resolve(source, args, info) {
        const { fieldASTs } = info
        const projection = getProjection(fieldASTs[0])

        return ListingModel.find({}, projection).then(listing => {
          console.log(listing)
          return listing
        })
      }
    }
  }
})

and my "listing type":

const ListingType = new GraphQLObjectType({
  name: 'Listing',
  fields: {
    _id: {
      type: GraphQLID
    },
    title: {
      type: GraphQLString
    },
    content: {
      type: GraphQLString
    }
  }
})
like image 267
Melbourne2991 Avatar asked Mar 15 '16 11:03

Melbourne2991


1 Answers

I know this is old but had similar problem, when querying for list returns null.

When querying for arrays you'll get an object {key: yourArray}, so it should be:

return ListingModel.find({}, projection).then(listing => {
 console.log(listing)
 return {listing: listing}
like image 174
Enes L Avatar answered Oct 18 '22 03:10

Enes L