简体   繁体   中英

Node js GraphQL Root Mutation pass an array

I have the following rootmutation function which accepts leads data as part of an argumant. The function works very well .

const RootMutationType = new GraphQLObjectType({
    name: 'Mutation',
    description: 'Root Mutation',
    fields: () => ({
        addLead: {
            type: lead_obj,
            description: 'Add a lead',
            args: {
                fname: {type: GraphQLNonNull(GraphQLString)},
                sname: {type: GraphQLNonNull(GraphQLString)},
                mobileno: {type: GraphQLNonNull(GraphQLString)},
                call_time: {type: GraphQLNonNull(GraphQLString)},
                productIdentifier: {type: GraphQLNonNull(GraphQLString)},
                card_id: {type: GraphQLNonNull(GraphQLString)},
                country: {type: GraphQLNonNull(GraphQLString)}
            },
            resolve: (parent, args) => {
                const lead = {
                    id: leads.length + 1,
                    fname: args.fname,
                    sname: args.sname,
                    mobileno: args.mobileno,
                    call_time: args.call_time,
                    productIdentifier: args.productIdentifier,
                    card_id: args.card_id,
                    country: args.country
                }
                leads.push(lead)
                return lead
            }
        }
    })
})

However , I would like to modify it so that it can allow array of productidentifier and cardid in the following manner :

products: [{productidentifier: 'product_1', cardid:'card_1'}, {productidentifier: 'product_2', cardid:'card_2'}]

I tried the following arguments on my mutation but it worked but I'm not sure if there any other better way to handle this :

products : {type: GraphQLNonNull(GraphQLString)},

And below is how I pushed my data :

mutation{
  addLead(fname:"dsfd",sname:"sfd",mobileno:"gdfs",call_time:"6453" , productIdentifier:"453",
    ,products:"[{productidentifier: 'product_1', cardid:'card_1'}, {productidentifier: 'product_2', cardid:'card_2'}]" ,card_id:"3243", country:"US"){
    
    fname,
    sname,
    mobileno,
    call_time,
    card_id,
    country
    
  }
}

It there a better way how to handle an array post in mutation apart from passing it as a string ?

Please define another GraphQLInputObjectType for products which will have fields like productidentifier and cardid. You will have to pass this fields in args object of mutation like below:

 products: {type: GraphQLNonNull(NewInputTypeName)},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM