|
| 1 | +--- |
| 2 | +name: Mu-Haskell with Mu-GraphQL |
| 3 | +description: A Haskell library for building microservices (gRPC, HTTP) and GraphQL APIs. |
| 4 | +url: https://higherkindness.io/mu-haskell/ |
| 5 | +github: higherkindness/mu-haskell |
| 6 | +--- |
| 7 | + |
| 8 | +Example implementation of a GraphQL server with type-level representation of the schema auto-generated: |
| 9 | + |
| 10 | +```haskell |
| 11 | +{-# LANGUAGE DataKinds #-} |
| 12 | +{-# LANGUAGE NamedFieldPuns #-} |
| 13 | +{-# LANGUAGE OverloadedStrings #-} |
| 14 | +{-# LANGUAGE PartialTypeSignatures #-} |
| 15 | +{-# LANGUAGE TypeApplications #-} |
| 16 | +{-# LANGUAGE TypeFamilies #-} |
| 17 | +{-# LANGUAGE TypeOperators #-} |
| 18 | + |
| 19 | +-- imports omitted for brevity... |
| 20 | + |
| 21 | +graphql "Library" "library.graphql" -- all the magic happens here! 🪄🎩 |
| 22 | + |
| 23 | +-- ... a bit more code... |
| 24 | + |
| 25 | +libraryServer :: SqlBackend -> ServerT ObjectMapping i Library ServerErrorIO _ |
| 26 | +libraryServer conn = |
| 27 | + resolver |
| 28 | + ( object @"Book" |
| 29 | + ( field @"id" bookId, |
| 30 | + field @"title" bookTitle, |
| 31 | + field @"author" bookAuthor, |
| 32 | + field @"imageUrl" bookImage |
| 33 | + ), |
| 34 | + object @"Author" |
| 35 | + ( field @"id" authorId, |
| 36 | + field @"name" authorName, |
| 37 | + field @"books" authorBooks |
| 38 | + ), |
| 39 | + object @"Query" |
| 40 | + ( method @"authors" allAuthors, |
| 41 | + method @"books" allBooks |
| 42 | + ), |
| 43 | + object @"Mutation" |
| 44 | + ( method @"newAuthor" newAuthor, |
| 45 | + method @"newBook" newBook |
| 46 | + ), |
| 47 | + object @"Subscription" |
| 48 | + (method @"allBooks" allBooksConduit) |
| 49 | + ) |
| 50 | + where |
| 51 | + bookId :: Entity Book -> ServerErrorIO Integer |
| 52 | + bookId (Entity (BookKey k) _) = pure $ toInteger k |
| 53 | + -- ... more resolvers... |
| 54 | +``` |
| 55 | + |
| 56 | +See [our docs](https://higherkindness.io/mu-haskell/graphql/) for more information about how to build your own GraphQL server and [the library example](https://github.com/higherkindness/mu-graphql-example-elm) for a more end-to-end example that includes a client written in Elm! |
0 commit comments