Scala GraphQL implementation
Sangria - Scala GraphQL Implementation sangria - scala graphql implementation
I'm testing out sangria to build a graphql/relay server. I have a very simple User class:
case class User(
id: Int,
username: String,
gender: Gender.Value)
I want to allow queries by either ID or username. I have created a schema that allows this, but the fields have different names:
val Query = ObjectType(
"Query", fields[UserRepo, Unit](
Field("user", OptionType(User),
arguments = ID :: Nil,
resolve = ctx => ctx.ctx.getUser(ctx arg ID)),
Field("userByUsername", OptionType(User),
arguments = Username :: Nil,
resolve = ctx => ctx.ctx.getUserByUsername(ctx arg Username))
))
Unfortunately, I need to query these with different fields names, user
and userByUsername
, e.g.:
curl -G localhost:8080/graphql
--data-urlencode 'query={userByUsername(username: "Leia Skywalker") {id, username, gender}}'
or
curl -G localhost:8080/graph
--data-urlencode "query={user(id: 1025) {id, username, gender}}"
How can I create a schema that allows a single field called user
to be queried on either ID or username? E.g. both of the following should return the same user object:
curl -G localhost:8080/graphql
--data-urlencode 'query={user(username: "Leia Skywalker") {id, username, gender}}'
or
curl -G localhost:8080/graph
--data-urlencode "query={user(id: 1025) {id, username, gender}}"
Source: (StackOverflow)