graphql-request
graphql-request copied to clipboard
[Question] Insert multiple objects at once with variables
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!
Maybe we need to specify in mutation that it's an array ?
what's specify in mutation how to create that ?
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"}])
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
}
}
`;
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