2

My sampleJSON -

{
 "entries": [
    {
      "fields":{
        "title":"My test title"
      }
    },
    {
      "fields":{
        "description":"My test description"
    }
    }
 ]
}

Schema.js -

const rootQuery = new GraphQLObjectType({
   name: 'testQuery',
   fields: {
     Articles: {
       type: articleItem,
       resolve(parentValue) {
          return axios.get(`/getArticles`).then(resp => resp.data);
       }
     }
   }
});

const articleItem = new GraphQLObjectType({
   name: 'articleItem',
   fields: () => ({
    entries: {type: new GraphQLList(entry)}
   })
});

const entry = new GraphQLObjectType({
   name: 'entry',
   fields: () => ({
      fields: {type: fields}
   })
});

const fields = new GraphQLObjectType({
  name: 'fields',
  fields: () => ({
    title: {type: GraphQLString},
    description: {type: GraphQLString}
  })
});

GraphQL query i am using to query the data in the above JSON -

query articles{
    Articles {
        entries{
          fields{
            title,
            description
          }
        }
     }
}

I am wondering why the query returns "title" even though it is null in the second object and likewise with description in the first object. Is there a way to just return " title " or " description " only if it not null?

Current result of the query -

{
  "data" : {
    "entries" [
       {
         "fields": {
             "title": "My test title",
             "description": null
         }
       },

       {
          "fields": {
             "title": null,
             "description" : "My test description"
          }

       }
    ]
  }

}

Required result -

{
  "data" : {
    "entries" [
       {
         "fields": {
             "title": "My test title"
         }
       },

       {
          "fields": {
             "description" : "My test description"
          }

       }
    ]
  }

}

Appreciate any help with this !, thanks.

arjary
  • 169
  • 1
  • 12

1 Answers1

0

Way too late to answer, but if you stumble upon this, you can make non-nullable (!) by using GraphQLNonNull().

Here is the example for non-nullable integer

random: {
  type: new GraphQLNonNull(GraphQLInt)
}