graphql required fields approach

Viewed 7986

This is my graphql schema, query and mutations.

I marked required fields in my schema with "!"

How I can create mutation to add new client?

Do I really need to write the same required fields again?

Like createClient(contactMethod: String!, hearAbout: String! ......... ): Client


const typeShard = `
  type ClientProfile {
    name: String!
    surname: String!
    address: String
    language: String!
  }

  type Client {
    _id: String
    isEdit: Boolean
    createdAt: String
    shortId: Int
    profile: ClientProfile
    comments: String
    contactMethod: String!
    hearAbout: String!
    leadAgentId: String
    branchId: String!
  }
`;

const queryShard = `
  getAllClients: [Client]
`;

const mutationShard = `
  removeClient(shortId : Int!): Client
  createClient(contactMethod: String!, hearAbout: String!   .........  ): Client
`;

const resolvers = {
  Query: {
    getAllClients: () => MongoClients.find().fetch(),
  },
  Mutation: {
    removeClient(root, { shortId }) {
      const client = MongoClients.findOne({ shortId });
      if (!client) throw new Error(`Couldn't find client with id ${shortId}`);
      MongoClients.remove({ shortId });
      return client;
    },
    createClient: (_, args) => {
      return MongoClients.insert(args);
    },
  },
};
1 Answers
Related