Guest User

Untitled

a guest
Sep 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import User from './user.model';
  2.  
  3. /**
  4. * Export a string which contains our GraphQL schema.
  5. */
  6. export const userTypeDefs = `
  7.  
  8. type User {
  9. id: ID!
  10. email: String!
  11. password: String!
  12. firstName: String!
  13. # Last name is not a required field so it does not need a "!" at the end.
  14. lastName: String
  15. }
  16.  
  17. input UserFilterInput {
  18. limit: Int
  19. }
  20.  
  21. # Extending the root Query type.
  22. extend type Query {
  23. users(filter: UserFilterInput): [User]
  24. user(id: String!): User
  25. }
  26.  
  27. # We do not need to check if any of the input parameters exist with a "!" character.
  28. # This is because mongoose will do this for us, and it also means we can use the same
  29. # input on both the "addUser" and "editUser" methods.
  30. input UserInput {
  31. email: String
  32. password: String
  33. firstName: String
  34. lastName: String
  35. }
  36.  
  37. # Extending the root Mutation type.
  38. extend type Mutation {
  39. addUser(input: UserInput!): User
  40. editUser(id: String!, input: UserInput!): User
  41. deleteUser(id: String!): User
  42. }
  43.  
  44. `;
  45.  
  46. /**
  47. * Exporting our resolver functions. Note that:
  48. * 1. They can use async/await or return a Promise which Apollo will resolve for us.
  49. * 2. The resolver property names match exactly with the schema types.
  50. */
  51. export const userResolvers = {
  52. Query: {
  53. users: async (_, { filter = {} }) => {
  54. const users: any[] = await User.find({}, null, filter);
  55. // notice that I have ": any[]" after the "users" variable? That is because I am using TypeScript
  56. // but you can remove this and it will work normally with pure JavaScript
  57. return users.map(user => user.toGraph());
  58. },
  59. user: async (_, { id }) => {
  60. const user: any = await User.findById(id);
  61. return user.toGraph();
  62. },
  63. },
  64. Mutation: {
  65. addUser: (_, { input }) => {
  66. const user: any = await User.create(input);
  67. return user.toGraph();
  68. },
  69. editUser: (_, { id, input }) => {
  70. const user: any = await User.findByIdAndUpdate(id, input);
  71. return user.toGraph();
  72. },
  73. deleteUser: (_, { id }) => {
  74. const user: any = await User.findByIdAndRemove(id);
  75. return user ? user.toGraph() : null;
  76. },
  77. },
  78. };
Add Comment
Please, Sign In to add comment