Last active
November 22, 2019 22:49
-
-
Save duanebester/0f4fa986717158a9ca412ed2849aaf4b to your computer and use it in GitHub Desktop.
Example GraphQLSchema definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sangria.schema._ | |
import sangria.macros.derive._ | |
import sangria.marshalling.sprayJson._ | |
import models.responses._ | |
import models.variables._ | |
import models.common._ | |
object GraphQLSchema { | |
// Responses | |
implicit val LocationOutputType = deriveObjectType[Unit, Location]() | |
implicit val UserType = deriveObjectType[Unit, User]() | |
implicit val CoffeeShopType = deriveObjectType[Unit, CoffeeShop]() | |
val UsersResponseType = deriveObjectType[Unit, UsersResponse]() | |
val CoffeeShopsResponseType = deriveObjectType[Unit, CoffeeShopsResponse]() | |
// Variables | |
implicit val LocationInputType = deriveInputObjectType[Location]( | |
InputObjectTypeName("LocationVariable") // Give name to resolve name conflict | |
) | |
implicit val BBoxType = deriveInputObjectType[BBox]() | |
// Arguments | |
val bboxArg = Argument("bbox", OptionInputType(BBoxType)) | |
val nameArg = Argument("name", OptionInputType(StringType)) | |
val SearchType = ObjectType( | |
"Search", | |
fields[MyContext, Unit]( | |
Field( | |
"users", | |
UsersResponseType, | |
arguments = nameArg :: Nil, | |
resolve = (c) => { | |
val name = c arg nameArg | |
val filter = Filter(name, c.ctx.bbox) | |
c.ctx.elastic.searchUsers(filter) | |
} | |
), | |
Field( | |
"coffeeShops", | |
CoffeeShopsResponseType, | |
arguments = nameArg :: Nil, | |
resolve = (c) => { | |
val name = c arg nameArg | |
val filter = Filter(name, c.ctx.bbox) | |
c.ctx.elastic.searchCoffeeShops(filter) | |
} | |
) | |
) | |
) | |
val QueryType = ObjectType( | |
"Query", | |
fields[MyContext, Unit]( | |
Field( | |
"geoSearch", | |
SearchType, | |
arguments = bboxArg :: Nil, | |
resolve = c => { | |
val bbox = c arg bboxArg | |
UpdateCtx(())(_ => c.ctx.copy(bbox = bbox)) | |
} | |
) | |
) | |
) | |
val SchemaDefinition = Schema(QueryType) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment