Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
How to create an array in the GraphQL's argumment as GraphQL Code First Approach

In GraphQL Code First Approach I am trying to pass the same argumment for createUser in createManyUser but I want to pass it as an array to create many users at once. I searched a lot but couldn't find it in GraphQL Code First Approach.

enter image description here

The Code

export const createUser = {
    type: userType,
    args: { // This work fine
        email: { type: string },
        username: { type: string },
        firstName: { type: string },
        lastName: { type: string }
    },
    resolve: async (_, args, { userAuth }) => {
        try {
            const user = await db.models.userModel.create(args);
            return user;
        } catch (error) {
            throw Error(`${error.message}`)
        }
    }
}

export const createManyUser = {
    type: new GraphQLList(userType),
    args: [{ // Here I made an array [] but it dose not work so here is my problem
        email: { type: string },
        username: { type: string },
        firstName: { type: string },
        lastName: { type: string }
    }],
    resolve: async (_, args, { userAuth }) => {
        try {
            const user = await db.models.userModel.bulkCreate(args);
            return user;
        } catch (error) {
            throw Error(`${error.message}`)
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't just put the args options in an array, to tell GraphQL that you expect a list of things you explicitly need to construct a GraphQLList type.

And you can't make a mutation field take a list of named things either - you must give the mutation one named argument that expects a list of input objects. So it'll be

export const createManyUser = {
    type: new GraphQLList(userType),
    args: {
        inputs: { type: new GraphQLList(new GraphQLNonNull(new GraphQLInputObjectType({
//                          ^^^^^^^^^^^                        ^^^^^^^^^^^^^^^^^^^^^^
            name: 'CreateUserInput',
            description: 'Input payload for creating user',
            fields: {
                email: { type: string },
                username: { type: string },
                firstName: { type: string },
                lastName: { type: string }
            }
        })))
    },
    resolve: async (_, args, { userAuth }) => {
        const user = await db.models.userModel.bulkCreate(args.inputs);
//                             ^^^^^^^
        return user;
    }
}

See also this article.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda