daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // mongoose schema
  2. const userSchema = new Schema({
  3.   name: String,
  4.   friendIds: [String],
  5. })
  6.  
  7. userSchema.methods.friends = function() {
  8.   return User.where("_id").in(this.friendIds)
  9. }
  10.  
  11. const User = mongoose.model("User", userSchema)
  12.  
  13. // GraphQL schema
  14. const graphqlSchema = `
  15.   type User {
  16.     id: ID!
  17.     name: String
  18.     friends: [User]
  19.   }
  20.  
  21.   type Query {
  22.     users: [User]
  23.   }
  24. `
  25.  
  26. // GraphQL resolver
  27. const resolver = {
  28.   Query: {
  29.     users: () => User.find()
  30.   }
  31. }
  32.    
  33. [
  34.   { id: 1, name: "Alice", friendIds: [2, 3] },
  35.   { id: 2, name: "Bob", friendIds: [1, 3] },
  36.   { id: 3, name: "Charlie", friendIds: [2, 4, 5] },
  37.   { id: 4, name: "David", friendIds: [1, 5] },
  38.   { id: 5, name: "Ethan", friendIds: [1, 4, 2] },
  39. ]
  40.    
  41. {
  42.   users {
  43.     name
  44.     friends {
  45.       name
  46.     }
  47.   }
  48. }
  49.    
  50. // mongoose schema
  51. const userSchema = new Schema({
  52.   name: String,
  53.   friendIds: [String]
  54. })
  55.  
  56. userSchema.methods.friends = function() {
  57.   return userLoader.load(this.friendIds)
  58. }
  59.  
  60. const User = mongoose.model("User", userSchema)
  61.  
  62. const userLoader = new Dataloader(userIds => {
  63.   const users = await User.where("_id").in(userIds)
  64.   const usersMap = new Map(users.map(user => [user.id, user]))
  65.   return userIds.map(userId => usersMap.get(userId))
  66. })
  67.    
  68. // GraphQL resolver
  69. const resolver = {
  70.   Query: {
  71.     users: async () => {
  72.       const userLoader = new Dataloader(userIds => {
  73.         const users = await User.where("_id").in(userIds)
  74.         const usersMap = new Map(users.map(user => [user.id, user]))
  75.         return userIds.map(userId => usersMap.get(userId))
  76.       })
  77.       const userIds = await User.find().distinct("_id")
  78.       return userLoader.load(userIds)
  79.     }
  80.   }
  81. }
  82.    
  83. // GraphQL resolver
  84. const resolver = {
  85.   Query: {
  86.     users: async () => {
  87.       const userLoader = new Dataloader(userIds => {
  88.         const users = await User.where("_id").in(userIds)
  89.         const usersMap = new Map(users.map(user => [user.id, user]))
  90.         return userIds.map(userId => usersMap.get(userId))
  91.       })
  92.       const userSchema = new Schema({
  93.         name: String,
  94.         friendIds: [String]
  95.       })
  96.       userSchema.methods.friends = function() {
  97.         return userLoader.load(this.friendIds)
  98.       }
  99.       const User = mongoose.model("User", userSchema)
  100.       const userIds = await User.find().distinct("_id")
  101.       return userLoader.load(userIds)
  102.     }
  103.   }
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top