Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few changes in GraphQL part: #32

Merged
merged 8 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ This sample app has several key components:

Additional libraries used:

* `spray-json` for JSON processing.
* `play-json` for JSON processing.
* `zio-logging` for logging.
* `zio-config` for typesafe configuration.
* `zio-test` for testing.

###GraphQL

During initialization step, there is a possibility to add GraphQL endpoint managed by Caliban library.
It adds two endpoints to the api: `/api/graphql` which is responsible for GraphQL queries and `/graphiql` with simple
GraphiQL console to play with the API.

To try out this feature open the browser http://localhost:8080/graphiql

Template license
----------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@
// use fetch, and could instead implement graphQLFetcher however you like,
// as long as it returns a Promise or Observable.
function graphQLFetcher(graphQLParams) {
// When working locally, the example expects a GraphQL server at the path /graphql.
// In a PR preview, it connects to the Star Wars API externally.
// Change this to point wherever you host your GraphQL server.
const isDev = !window.location.hostname.match(/(^|\.)netlify\.com\$|(^|\.)graphql\.org\$/)
const api = isDev ? '/api/graphql' : 'https://swapi.graph.cool/'
const api = '/api/graphql'
const token = localStorage.getItem('Authorization')
return fetch(api, {
method: 'post',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,40 @@ object GraphQLApi extends GenericSchema[Any] {

case class ItemName(name: String)
case class ItemPrice(price: BigDecimal)
case class ItemInput(name: String, price: BigDecimal)

case class Queries(
@GQLDescription("Find items by name")
item: ItemName => Task[List[Item]],
itemByName: ItemName => Task[List[Item]],
@GQLDescription("Find items cheaper than specified price")
cheaperThan: ItemPrice => Task[List[Item]]
cheaperThan: ItemPrice => Task[List[Item]],
@GQLDescription("Get all items")
allItems: Task[List[Item]],
@GQLDescription("Get item by ID")
item: ItemId => Task[Option[Item]]
)

def adaptError(e: DomainError): Throwable = e match {
case RepositoryError(cause) => cause
case ValidationError(msg) => new Exception(msg)
}

case class Mutations(deleteItem: ItemId => ZIO[Any, Throwable, Unit])
case class Mutations(
addItem: ItemInput => Task[ItemId],
deleteItem: ItemId => Task[Unit])

def api(repository: ItemRepository): GraphQL[Console with Clock] =
graphQL(
RootResolver(
Queries(
args => ApplicationService.getItemByName(args.name).mapError(adaptError).provide(repository),
args => ApplicationService.getItemsCheaperThan(args.price).mapError(adaptError).provide(repository)
args => ApplicationService.getItemsCheaperThan(args.price).mapError(adaptError).provide(repository),
ApplicationService.getItems.mapError(adaptError).provide(repository),
id => ApplicationService.getItem(id).mapError(adaptError).provide(repository)
),
Mutations(id => ApplicationService.deleteItem(id).provide(repository).mapError(e => new Throwable("")))
Mutations(
item => ApplicationService.addItem(item.name, item.price).mapError(_.asThrowable).provide(repository),
id => ApplicationService.deleteItem(id).mapError(e => new Throwable(""))).provide(repository)
)
) @@
maxFields(200) @@ // query analyzer that limit query fields
Expand Down