How to insert values from function into graphql mutation?

by tressie.damore , in category: Javascript , 5 months ago

How to insert values from function into graphql mutation?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 5 months ago

@tressie.damore 

To insert values from a function into a GraphQL mutation, you can define a resolver function that will handle the input values from the function and pass them to the mutation. Here is an example of how you can achieve this:

  1. Define a resolver function that takes in the values you want to insert into the mutation:
1
2
3
4
const insertValues = (value1, value2, value3) {
  // do any necessary processing here
  return { value1, value2, value3 };
}


  1. Create a GraphQL mutation that will use the values from the resolver function:
1
2
3
4
5
mutation InsertValues($value1: String!, $value2: String!, $value3: String!) {
  insertValues(value1: $value1, value2: $value2, value3: $value3) {
    // specify the fields you want to return after insertion
  }
}


  1. In your resolver function, call the insertValues mutation with the values from the function:
1
2
3
4
5
6
7
8
9
const { value1, value2, value3 } = insertValues('value1', 'value2', 'value3');
const result = await graphqlClient.mutate({
  mutation: INSERT_VALUES_MUTATION,
  variables: {
    value1,
    value2,
    value3
  }
});


This way, you can easily insert values from a function into a GraphQL mutation by passing the values as variables in the mutation query.