Gerd
Gerd

Reputation: 2603

How can I select a part of a array of objects in a GraphQL query?

My resolver get

{ adminMsg: 
   [ 
     {active: “y”, text1: “blah1" } , 
     {active: “n”, text1: “blah2" } 
   ] };

My query:

{
  adminWarn {
    adminMsg {
      active, text1
    }
  }
}

I want only array-elements with condition: active = 'y'

I find in GQL Dokumentation no way to write this condition im my query. Is there any solution in GQL?

Upvotes: 0

Views: 825

Answers (1)

Gerd
Gerd

Reputation: 2603

Use of resolve args can solve the problem:

const adminWarnList = new GraphQLObjectType({
    name: 'adminWarnReportList',
    fields: () => ({
        adminMsg: {
            type: new GraphQLList(adminWarnFields),
        },
    }),
});

const adminWarn = {
    type: adminWarnList,
    args: {
        active: { type: GraphQLString },
    },
    resolve: (parent, args, context) => {
       ...
       let reportdata = context.loadData();

       if (args.active == 'y') {
                    let filteredItems = reportdata.filter(function(item) {
                        return item.active != null && item.active != 'y';
                    });

                    reportdata = filteredItems;
        }

        return { adminMsg: reportdata };
    },
};

Upvotes: 1

Related Questions