Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL: Updating an array

Tags:

I'm having some issues updating an array in the resolver. I'm building with typescript.

Description

I have in the datamodel.graphql for Prisma:

type Service @model {
    id: ID! @unique
    title: String
    content: String
    createdAt: DateTime!
    updatedAt: DateTime!
    comments: [Comment!]! // Line to be seen here
    author: User!
    offer: Offer
    isPublished: Boolean! @default(value: "false")
    type: [ServiceType!]!
}

type Comment @model {
    id: ID! @unique
    author: User! @relation(name: "WRITER")
    service: Service!
    message: String!
}

The Prisma is connected to the GraphQl server and in this one, I defined the mutation :

commentService(id: String!, comment: String!): Service!

So comes the time for implementing the resolver for the given mutation and I'm doing this :

async commentService(parent, {id, comment}, ctx: Context, info) {
    const userId = getUserId(ctx);
    const service = await ctx.db.query.service({
        where: {id}
    });
    if (!service) {
        throw new Error(`Service not found or you're not the author`)
    }

    const userComment = await ctx.db.mutation.createComment({
        data: {
            message: comment,
            service: {
                connect: {id}
            },
            author: {
                connect: {id:userId}
            },
        }
    });

    return ctx.db.mutation.updateService({
        where: {id},
        data: {
            comments: {
               connect: {id: userComment.id}
            }
        }
    })
}

The problem :

The only thing I'm receiving when querying the playground is null instead of the comment I've given.

Thanks for reading till so far.

like image 365
Selast Lambou Avatar asked Aug 19 '18 08:08

Selast Lambou


1 Answers

Can you please share code where you expose mutation resolvers? You might get null in response in case you forgot to include commentService resolver in mutation resolver object.

Apart from this, I see one more issue in the code. Since you have relation between Service and Comment, you can use single mutation to create comment and add it in service. You don't need to write two separate mutations in order to achieve that. Your resolver can be changed to be as simple as below:

async commentService(parent, {id, comment}, ctx: Context, info) {
    const userId = getUserId(ctx);

    return ctx.db.mutation.updateService({
        where: {id},
        data: {
            comments: {
               create: {
                   message: comment,
                   author: {
                      connect: {id:userId}
                   }
               }
            }
        }
    })
}

Note that I also removed query to check if service exists before performing update. Reason being, updateService binding call will throw error in case it does not exist, we don't need to explicitly check for that.

like image 186
Raeesaa Avatar answered Sep 28 '22 17:09

Raeesaa