Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Add value to Array field in mutation #267

Closed
kbrandwijk opened this issue Jun 22, 2017 · 5 comments
Closed

Add value to Array field in mutation #267

kbrandwijk opened this issue Jun 22, 2017 · 5 comments

Comments

@kbrandwijk
Copy link
Contributor

If you have a Type with a [String!] field, it might be useful to get a mutation syntax that supports adding to the existing Array.

@sorenbs
Copy link
Member

sorenbs commented Nov 14, 2017

Please see this proposal for our suggested implementation: https://github.com/graphcool/framework/issues/1275

This feature requires a fundamental change to the data structure for scalar lists, so it would be very valuable to get feedback on the suggested functionality as soon as possible, so we can make sure the new data structure supports all required features.

@sorenbs sorenbs closed this as completed Nov 14, 2017
@gustawdaniel
Copy link

gustawdaniel commented Jun 23, 2019

Is this solved?

link https://github.com/graphcool/framework/issues/1275 gives 404

Update.

For further readers you can add value to array of relation in this way:

await context.prisma.updateUser({
     data: {takenOffers: {connect: [{id}]}},
     where: {id: userId}
});

@onyegood
Copy link

onyegood commented Aug 6, 2019

@gustawdaniel please help, I still face this problem, can I see your data model? and what your id look like

//Here is my createRole mutation
async createRole(parent, args, { prisma, request }, info) {
getUserId(request);
const { permissions, name } = args.data;
return prisma.mutation.createRole({
data: {
name,
permissions: {
connect: [{ id: permissions }]
}
}
});
}

//Here is my createRoleInput
input CreateRoleInput {
name: String!
permissions: [ID!]!
}

//Here is what I'm sending from GraphQL Playground
mutation {
createRole(data:{
name: "Super Admin"
permissions: ["cjyviw68f160l0785xgy61evq", "cjywrqnab1hwt0785t6ztxj02"]
})
{
id
}
}

@gustawdaniel
Copy link

@onyegood It is because of you have array of ID in your model of input but you assigning this array to single id in mutation.

Of course in your QraphQL Playground example everything should work.

In backend code you can try

async createRole(parent, args, { prisma, request }, info) {
   getUserId(request);
   const { permissions, name } = args.data;
   return prisma.mutation.createRole({
      data: {
         name,
         permissions
      }
   });
}

or

async createRole(parent, args, { prisma, request }, info) {
   getUserId(request);
   const { permissions, name } = args.data;
   return prisma.mutation.createRole({
      data: {
         name,
         permissions: {connect: permissions.map( p => ( {id: p} ) ) }
      }
   });
}

I assuming that you have

permissions =  ["cjyviw68f160l0785xgy61evq", "cjywrqnab1hwt0785t6ztxj02"]

For next comments please use syntax with

\ ```

to mark code.

https://en.support.wordpress.com/markdown-quick-reference/

@onyegood
Copy link

onyegood commented Aug 7, 2019

@gustawdaniel Much thanks, option 2 solved my problem. I am very grateful

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants