// Generated by moongql from shop.mbt. Edit those files, not this one.

///|
pub fn register(schema : @moongql.Schema, resolvers : @moongql.Resolvers) -> Unit {
  try {
    schema.add_sdl(shop_sdl + "\n" + mutations_sdl())
  } catch{
    e => abort(e.to_string())
  }
  resolvers.field(
    "Author", "books", info => {
      @moongql.ToGql::to_gql(
        try {
          Author::books(
            @moongql.FromGql::from_gql(info.parent),
            first?=info.read("first"),
          )
        } catch{
          e => raise  @moongql.field_error(e)
        },
      )
    },
  )
  resolvers.field(
    "Book", "author", info => {
      @moongql.ToGql::to_gql(
        try {
          Book::author(@moongql.FromGql::from_gql(info.parent))
        } catch{
          e => raise  @moongql.field_error(e)
        },
      )
    },
  )
  resolvers.field(
    "Query", "book", info => {
      @moongql.ToGql::to_gql(
        try {
          Query::book(info.read("id"))
        } catch{
          e => raise  @moongql.field_error(e)
        },
      )
    },
  )
  resolvers.field(
    "Query", "search", info => {
      @moongql.ToGql::to_gql(
        try {
          Query::search(info.read("text"))
        } catch{
          e => raise  @moongql.field_error(e)
        },
      )
    },
  )
  resolvers.field(
    "Mutation", "addBook", info => {
      @moongql.ToGql::to_gql(
        try {
          Mutation::add_book(info.read("input"))
        } catch{
          e => raise  @moongql.field_error(e)
        },
      )
    },
  )
}
///|
pub fn register_subscriptions(
  schema : @moongql.Schema,
  resolvers : @moongql.Resolvers,
  subscribers : @moongql.Subscribers,
) -> Unit {
  let gql_subscription = match schema.subscription {
    Some(n) => {
      match schema.type_by_name(n) {
        Some(t) => t
        None => schema.object(n)
      }
    }
    None => {
      schema.set_subscription("Subscription")
      schema.object("Subscription")
    }
  }
  subscribers.field(
    gql_subscription.name, "bookAdded", info => {
      (
        try {
          Subscription::book_added(author_id?=info.read("authorId"))
        } catch{
          e => raise  @moongql.field_error(e)
        }
      ).map(e => @moongql.ToGql::to_gql(e))
    },
  )
  resolvers.field(gql_subscription.name, "bookAdded", info => info.parent)
}
///|
fn mutations_sdl() -> String {
  "type Mutation {\n  addBook(input: NewBook!): Book!\n}\n"
}
///|
pub(all) struct Author {
  id : String
  name : String
}
///|
pub impl @moongql.ToGql for Author with fn to_gql(self) {
  {
    "__typename": "Author",
    "id": @moongql.ToGql::to_gql(self.id),
    "name": @moongql.ToGql::to_gql(self.name),
  }
}
///|
pub impl @moongql.FromGql for Author with fn from_gql(v) {
  Author::{ id: @moongql.read(v, "id"), name: @moongql.read(v, "name") }
}
///|
pub extend Author with @moongql.ToGql::{ to_gql }
///|
pub extend Author with @moongql.FromGql::{ from_gql }
///|
declare fn Author::books(self : Author, first? : Int32) -> Array[Book] raise

///|
pub impl @moongql.ToGql for Book with fn to_gql(self) {
  {
    "__typename": "Book",
    "id": @moongql.ToGql::to_gql(self.id),
    "title": @moongql.ToGql::to_gql(self.title),
    "status": @moongql.ToGql::to_gql(self.status),
    "authorId": @moongql.ToGql::to_gql(self.author_id),
  }
}
///|
pub impl @moongql.FromGql for Book with fn from_gql(v) {
  Book::{
    id: @moongql.read(v, "id"),
    title: @moongql.read(v, "title"),
    status: @moongql.read(v, "status"),
    author_id: @moongql.read(v, "authorId"),
  }
}
///|
pub extend Book with @moongql.ToGql::{ to_gql }
///|
pub extend Book with @moongql.FromGql::{ from_gql }
///|
declare fn Book::author(self : Book) -> Author raise

///|
pub(all) struct NewBook {
  title : String
  author_id : String
}
///|
pub impl @moongql.ToGql for NewBook with fn to_gql(self) {
  {
    "title": @moongql.ToGql::to_gql(self.title),
    "authorId": @moongql.ToGql::to_gql(self.author_id),
  }
}
///|
pub impl @moongql.FromGql for NewBook with fn from_gql(v) {
  NewBook::{
    title: @moongql.read(v, "title"),
    author_id: @moongql.read(v, "authorId"),
  }
}
///|
pub extend NewBook with @moongql.ToGql::{ to_gql }
///|
pub extend NewBook with @moongql.FromGql::{ from_gql }
///|
pub(all) struct Query {
  
}
///|
declare fn Query::book(id : String) -> Book? raise

///|
declare fn Query::search(text : String) -> Array[Hit] raise

///|
pub(all) struct Subscription {
  
}
///|
declare fn Subscription::book_added(author_id? : String) -> Array[Book] raise

///|
pub(all) struct Mutation {
  
}
///|
declare fn Mutation::add_book(input : NewBook) -> Book raise

///|
#derive(Eq, Debug)
pub(all) enum Status {
  DRAFT
  PUBLISHED
}
///|
pub impl @moongql.ToGql for Status with fn to_gql(self) {
  match self {
    DRAFT => "DRAFT"
    PUBLISHED => "PUBLISHED"
  }
}
///|
pub impl @moongql.FromGql for Status with fn from_gql(v) {
  match v {
    String("DRAFT") => DRAFT
    String("PUBLISHED") => PUBLISHED
    other => raise  @moongql.ResolverError::expected("Status", other)
  }
}
///|
pub extend Status with @moongql.ToGql::{ to_gql }
///|
pub extend Status with @moongql.FromGql::{ from_gql }
///|
pub(all) enum Hit {
  Author(Author)
  Book(Book)
}
///|
pub impl @moongql.ToGql for Hit with fn to_gql(self) {
  match self {
    Author(v) => @moongql.ToGql::to_gql(v)
    Book(v) => @moongql.ToGql::to_gql(v)
  }
}
///|
pub impl @moongql.FromGql for Hit with fn from_gql(v) {
  match v {
    { "__typename": String("Author"), .. } => 
      Author(@moongql.FromGql::from_gql(v))
    { "__typename": String("Book"), .. } => Book(@moongql.FromGql::from_gql(v))
    other => raise  @moongql.ResolverError::expected("Hit", other)
  }
}
///|
pub extend Hit with @moongql.ToGql::{ to_gql }
///|
pub extend Hit with @moongql.FromGql::{ from_gql }