I've created a model that has a field which takes multiple values of an ENUM. How can I write a mutation which will allow me to add multiple values at once?
For example, I have a sign up mutation:
export const signUp = gql`
mutation signUp(
$email: String!,
$name: String!,
$password: String!,
$attended: [USER_ATTENDED!]!,
$role: [USER_ROLE!]!,
) {
createUser(
authProvider: {
email: {
email: $email,
password: $password,
},
},
name: $name,
attended: $attended,
role: $role,
) {
id,
createdAt,
name,
email,
}
}
`;
I would expect $attended and $role to accept multiple values.
You can pass multiple values by using brackets: []. In your case, the variables in GraphiQL need to like like this:
{
"email": "name#email.com",
"name": "Name",
"password": "secret",
"attended": ["A", "B"],
"role": ["ROLE_A", "ROLE_B"]
}
In Apollo, this should work:
const variables = {
email: "name#email.com",
name": "Name",
password: "secret",
attended: ["A", "B"],
role: ["ROLE_A", "ROLE_B"]
}
More information about GraphQL variables is available here and here.
Related
I have a user.gql - file with some queries, mutations, inputs etc.
I get content of user.gql, using gql(), then filter an document.definition to find needed a query. After filter
document.definitions
.filter(item => item.kind === 'OperationDefinition')
.filter(item => item.operation === 'query')
, one request object remains. smth like
{
kind: "OperationDefinition",
name: {kind: 'Name', value: 'authenticate'},
operation: "query",
selectionSet: {...}
variableDefinitions: [
...
]
}
How to convert it back to string?
In the file this code is written like this
query authenticate($login: String!, $password: Password!) {
user(login: $login, password: $password) {
token,
name,
additional
}
}
I have a GraphQL schema like this:
type User {
id: ID
name: String
email: String
addresses: [UserAddress]
}
type UserAddress {
id: ID
city: String
country: String
}
I always have doubts about how to make the best design for mutations. (I'm using apollo + prisma)
These are my options:
1) One single mutation
I need to create this mutation and input type:
input userAddressInput {
id: ID
city: String
country: String
}
mutation updateUser (
id: ID
name: String
email: String
addresses: UserAddressInput
): User
Then I execute mutations like this:
mutation updateUserData($id: ID, $name: String, $email: String) {
updateUser(id: $id, name: $name, email: $email) {
id
name
email
}
}
mutation updateUserAddress($id: ID, $userAddress: UserAddressInput) {
updateUser(id: $id, userAddress: $userAddress) {
id
addresses {
id
city
country
}
}
}
And resolvers like this:
Mutation: {
updateUser: (_, args) => {
if (args.name || args.email) {
// update model User by args.userData.id
}
if (args.userAddress) {
// update model UserAddress by args.userAddress.id
}
}
}
2) One mutation per type
I don't need to create any input type but I need two mutations:
mutation updateUser (
id: ID
name: String
email: String
): User
mutation updateUserAddress (
id: ID
city: String
country: String
): UserAddress
Then mutations like this:
mutation updateUser($id: ID, $name: String, $email: String) {
updateUser(id: $id, name: $name, email: $email) {
id
name
email
}
}
mutation updateUserAddress($id: ID, $city: String, $country: String) {
updateUserAddress(id: $id, city: $city, country: $country) {
id
city
country
}
}
And resolvers like this:
Mutation: {
updateUserAddress: (_, args) => {
// update model UserAddress by args.id
}
updateUser: (_, args) => {
// update model User by args.id
}
}
What is the best way to deal with such cases?
It depends what your use case is.
Does your GUI allow for update of a user's addresses without also updating the user info? If so you will likely need a separate mutation for updating only the addresses.
If you are allowing user and addresses to be edited and saved as one operation then individual mutations would require you to send multiple HTTP requests (one per mutation).
Do you need to update the user and addresses as an atomic transaction (i.e. all or nothing)? If so then you should use a single mutation.
I'm using strapi as a headless CMS
I've created a Customer content type and I've installed the graphql plugin.
I'm trying to create a mutation in the graphql playground to create a new Customer
mutation createCustomer($username: String!, $email: String, $password: String){
createCustomer(username: $username, email: $email, password: $password){
_id username email password
}
}
I'm passing the username etc in the query variables
{ "username": "user1" }
{ "email": "User1#test.com" }
{"password":"123456"}
I'm getting the error
"message": "Unknown argument \"username\" on field \"createCustomer\" of type \"Mutation\".",
If I look in the Docs the it looks like.
How can I write a mutation to create a new Customer
That error indicates that are missing the reference to Customer type in the Query component of the Mutation.
Also, I believe you are missing "input:" & "data:". I know that these are required when using createUser.
By example:
mutation createCustomer($username: String!, $email: String, $password: String) {
createCustomer(
input: { data: { username: $username, email: $email, password: $password } }
) {
customer {
id
username
email
password
}
}
}
I want to alter the mutation using Laravel Creating event. I want to fetch task ID from key that is coming from front end. And then i want to add this ID in replace of key so that my task will be create automatically using lighthouse structure. Here is sample mutation
mutation
{
createUser(input: {
firstname: "last"
email: "abc#gmaiol.com"
task:
{
create: {
key: 'reminder'
}
}
})
{
id
}
}
My recommendation is to create a resolver for your specific situation:
mutation
{
createUser(input: {firstname: "last", email: "abc#gmaiol.com", key: "reminder"})
{
id
}
}
Remember to always use double quotes " ", never use single quotes ' '
In your schema.graphql
input newUser {
firstname: String!
email: String!
key: String!
}
type newUserResponse {
ID: ID!
}
createUser(data: newUser): newUserResponse #field(resolver: "App\\GraphQL\\Mutations\\createUser")
Here's an example of the resolver: Resolver example
Also check the docs: https://lighthouse-php.com/4.9/api-reference/resolvers.html
I would like to send an array of object in a graphQL queries. But I don't have any idea how to type the pointer in the query $gallery: where Type will be a simple datastructure like a class or dictionnary.
apollo_client.mutate({mutation: gql`
mutation m(
$title: String!, $gallery:<Type?>){
mutatePmaGallery(pmaData:
{title: $title, gallery: $gallery}) {
pma{
id
}
}
}`,
variables: {
title: _this.state.title,
gallery: {<Type?>}
})
You first need to define an input type according to your gallery structure :
input GalleryType {
id:ID!
name: String!
}
Then you can simply do this:
apollo_client.mutate({mutation: gql`
mutation m(
$title: String!, $gallery:[GalleryType!]!){ //changed part
mutatePmaGallery(pmaData:
{title: $title, gallery: $gallery}) {
pma{
id
}
}
}`,
variables: {
title: _this.state.title,
gallery: {<Type?>}
})