graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

[Question] Insert multiple objects at once with variables

Open divramod opened this issue 4 years ago • 5 comments

Hey, thx for the awesome project!

I'm a graphql-request beginner and like to ask, if it is possible to insert multiple objects at once with graphql-request using variables and how it should look like.

I was able to successful insert a single entity like that:

  import { request } from 'graphql-request'
  const variables = { id: 123, name: "test" }
  const gql = `mutation insert_test(
    $id: Int!
    $name: String! 
  ) {
    insert_test(
      objects: {
        id: $id,
        name: $name
      }
    ) {
      returning {
        id
      }
    }
  }`
  const res = await request(HASURA_URL_GRAPHQL, gql, variables)

But when changing the variables to an array i got an error.

  import { request } from 'graphql-request'
  const variables = [{ id: 123, name: "test" }, { id: 456, name: "test1" }]
  const gql = `mutation insert_test(
    $id: Int!
    $name: String! 
  ) {
    insert_test(
      objects: {
        id: $id,
        name: $name
      }
    ) {
      returning {
        id
      }
    }
  }`
  const res = await request(HASURA_URL_GRAPHQL, gql, variables)

Can someone give me a hint into the right direction here? Thx in advance!

divramod avatar Oct 27 '19 06:10 divramod

Maybe we need to specify in mutation that it's an array ?

umr55766 avatar Nov 25 '19 07:11 umr55766

what's specify in mutation how to create that ?

zeing avatar Jun 10 '20 09:06 zeing

Your definition should be same as server\s definition.

E.g., in server

input UserInput {
  username: String!
}

mutations {
  createUsers(input: [UserInput!]): boolean
}

Request must be something like this:

const query = gql`
    mutation createUsers(
        $users: [UserInput!]
    ) {
        createUsers(
            input: $users
        )
    }

request("http://localhost:8080", query, [{username: "sample1"}, {username: "sample2"}])

randomprofilename avatar May 21 '21 11:05 randomprofilename

In case you want to pass an array of objects this is how I did it.

In your server


export const OptionInputType = new GraphQLInputObjectType({
  name: 'OptionInput',
  fields: () => ({
    option_uid: { type: GraphQLID },
    attribute_uid: { type: GraphQLID },
    option_name: { type: GraphQLString },
    additional_price: { type: GraphQLFloat },
    color_hex: { type: GraphQLString },
  }),
});

// -------------
const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
      CreateAttribute: {
            type: AttributeType,
            args: {
              product_uid: { type: GraphQLID },
              attribute_name: { type: GraphQLString },
              options: { type: new GraphQLList(OptionInputType) },
            },
            async resolve(
              parent,
              { product_uid, attribute_name, options }
            ) {
              console.log(`====>`, { product_uid, attribute_name, options })
              return []
            },
          }}
})

In Client

import { gql } from 'graphql-request';

export const CreateAttributeMutation = gql`

  mutation CreateAttribute(
    $product_uid: ID!
    $attribute_name: String!
    $options: [OptionInput!]!
  ) {
    CreateAttribute(
        product_uid: $product_uid
        attribute_name: $attribute_name
        options: $options
    ) {
        attribute_name
    }
  }
`;

larbisahli avatar Aug 15 '21 20:08 larbisahli

In case you want to pass an array of objects this is how I did it.

In your server

export const OptionInputType = new GraphQLInputObjectType({
  name: 'OptionInput',
  fields: () => ({
    option_uid: { type: GraphQLID },
    attribute_uid: { type: GraphQLID },
    option_name: { type: GraphQLString },
    additional_price: { type: GraphQLFloat },
    color_hex: { type: GraphQLString },
  }),
});

// -------------
const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
      CreateAttribute: {
            type: AttributeType,
            args: {
              product_uid: { type: GraphQLID },
              attribute_name: { type: GraphQLString },
              options: { type: new GraphQLList(OptionInputType) },
            },
            async resolve(
              parent,
              { product_uid, attribute_name, options }
            ) {
              console.log(`====>`, { product_uid, attribute_name, options })
              return []
            },
          }}
})

In Client

import { gql } from 'graphql-request';

export const CreateAttributeMutation = gql`

  mutation CreateAttribute(
    $product_uid: ID!
    $attribute_name: String!
    $options: [OptionInput!]!
  ) {
    CreateAttribute(
        product_uid: $product_uid
        attribute_name: $attribute_name
        options: $options
    ) {
        attribute_name
    }
  }
`;

Thank you very much, this successfully solved my problem

frmachao avatar Aug 23 '21 05:08 frmachao