A
A
Aleksandr2018-07-10 12:49:55
Node.js
Aleksandr, 2018-07-10 12:49:55

Change graphQL mutation code?

I'm just studying this bundle and ran into some misunderstandings?

import {
    GraphQLNonNull,
    GraphQLID,
    GraphQLString
} from 'graphql';

import { clubType } from '../../types/club';
import ClubModel from '../../../models/club';

export default {
    type: clubType,
    args: {
        id: {
            name: 'id',
            type: new GraphQLNonNull(GraphQLID)
        },
        name: {
            type: new GraphQLNonNull(GraphQLString)
        },
        position: {
            type: new GraphQLNonNull(GraphQLString)
        },
        league: {
            type: new GraphQLNonNull(GraphQLString)
        }
    },
    resolve(root, params) {
        const updateClub = ClubModel.findByIdAndUpdate(params.id, {
            $set: {
                name: params.name,
                position: params.position,
                league: params.league
            }
        }, {
            new: true
        })
        if (!updateClub) {
            throw new Error('error update club')
        }
        return updateClub;
    }
}

There is such a code for changing data, now it is done in such a way that only certain data cannot be changed, you need to transfer everything
mutation {
  UpdateClub(id: "5b447f4d0f2f3b03e20001e4", name: "Zenit FC", position: "1", league : "Rus") {
    _id
    name
    position
    league
    createdAp
    updateAt
  }
}

But how to rewrite in such a way that if I need to change only position, I would transfer only this data?
mutation {
  UpdateClub(id: "5b447f4d0f2f3b03e20001e4", position: "1") {
    _id
    name
    position
    league
    createdAp
    updateAt
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-07-10
@Sashjkeee

Use the Relay approach to solve this problem, instead of describing all the arguments in a mutation, just make an input and pass the desired data into it. Here is a good article to read.

input UpdateClubInput {
    id: ID!
    name: String
    position: String
    league: String
}

mutation {
    UpdateClub(input: UpdateClubInput!): Club
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question