Advertisement
Guest User

Untitled

a guest
May 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. object ExampleViewerSchema {
  2. case class User(id: String)
  3. class FriendsService {
  4. def list(user: User): List[User] = List() // Actually filter based on your data model
  5. }
  6. class UserService {
  7. def authenticate: User = User("1") // Actually get a user
  8. }
  9. case class ServiceManager(friendsService: FriendsService, userService: UserService)
  10. case class MyContext(user: Option[User], serviceManager: ServiceManager)
  11.  
  12. val UserType = ObjectType[MyContext, User](
  13. "User",
  14. fields = fields[MyContext, User](
  15. Field(
  16. "id",
  17. StringType,
  18. resolve = ctx => ctx.value.id
  19. )
  20. )
  21. )
  22.  
  23. val ViewerType: ObjectType[MyContext, Unit] = ObjectType(
  24. "Viewer",
  25. "The current, authenticated user.",
  26. fields = fields[MyContext, Unit](
  27. Field(
  28. "tastings",
  29. ListType(UserType),
  30. Some("List all of the friends for a user."),
  31. resolve = ctx => ctx.ctx.serviceManager.friendsService.list(ctx.ctx.user.get)
  32. )
  33. )
  34. )
  35.  
  36. val ViewQuery = fields[MyContext, Unit](
  37. Field(
  38. "viewer",
  39. ViewerType,
  40. Some("Information in the app scoped to the requesting user."),
  41. resolve = ctx => UpdateCtx(ctx.ctx.serviceManager.userService.authenticate) { user =>
  42. ctx.ctx.copy(user = Some(user))
  43. }
  44. )
  45. )
  46.  
  47. val QueryType: ObjectType[MyContext, Unit] = ObjectType("Query",
  48. ViewQuery
  49. )
  50.  
  51. val schema = Schema(QueryType)
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement