仅返回GraphQLList对象

时间:2018-02-20 19:25:49

标签: graphql graphql-js express-graphql

我的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查询我用来查询上面JSON中的数据 -

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

我想知道为什么查询返回“title”,即使它在第二个对象中为null,同样在第一个对象中也有描述。有没有办法只返回“标题”或“描述”,如果它不为空?

查询的当前结果 -

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

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

       }
    ]
  }

}

必填结果 -

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

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

       }
    ]
  }

}

感谢您的任何帮助!,谢谢。

0 个答案:

没有答案