GraphQL queries made easy in Elm! This package provides easier way to deal with GraphQL queries. This package aims to partially mimic the Json Encoders and the HTTP API. Every user of Elm should not be lost using this package.
import GraphQl
-- Define the request.
userRequest : GraphQl.Value
userRequest =
GraphQl.object
[ GraphQl.field "user"
|> GraphQl.withArgument "id" (GraphQl.variable "id")
|> GraphQl.withSelectors
[ GraphQl.field "id"
, GraphQl.field "name"
|> GraphQl.withSelectors
[ GraphQl.field "first_name"
, GraphQl.field "last_name"
]
]
, GraphQl.field "address"
|> GraphQl.withArgument "city" (GraphQl.string "Paris")
|> GraphQl.withArgument "id" (GraphQl.int 12)
|> GraphQl.withArgument "type" (GraphQl.type_ "LOFT")
|> GraphQl.withSelectors
[ GraphQl.field "street"
, GraphQl.field "neighborhood"
]
]
|> GraphQl.withVariable "id" "INT!"
-- And Send It!
sendRequest : Int -> Decoder a -> Cmd msg
sendRequest id decoder =
GraphQl.query "/example_endpoint" userRequest decoder
|> GraphQl.addVariables [ ("id", Encode.int id) ]
|> GraphQl.sendGenerate a Value, from a list of fields.
object
[ field "user" ]Turns into:
query {
user
}Generate a Value with a name.
named "MySuperRequest"
[ field "user" ]Turns into:
query MySuperRequest {
user
}Generate a field.
Add an argument to a Field.
field "user"
|> withArgument "id" (GraphQl.string "12")
|> withSelectors
[ field "id"
, field "first_name"
, field "last_name"
]Turns into:
user(id: "12") {
id
first_name
last_name
}Add a variable to a Field.
field "user"
|> withVariable "id" "id"Turns into:
user(id: $id)Add an alias to a Field.
field "user"
|> withAlias "currentUser"
|> withSelectors
[ field "id"
, field "first_name"
, field "last_name"
]Turns into:
currentUser: user {
id
first_name
last_name
}Add selectors to a Field.
field "user"
|> withSelectors
[ field "id"
, field "first_name"
, field "last_name"
]Turns into:
user {
id
first_name
last_name
}Handle arguments on GraphQL fields.
Generate an argument, to use with withArgument.
fied "user"
|> withArgument "id" (GraphQl.int 12)Turns into:
user(id: 12)Generate an argument, to use with withArgument.
fied "user"
|> withArgument "id" (GraphQl.string "12")Turns into:
user(id: "12")Generate an argument, to use with withArgument.
Generate a type in GraphQL.
fied "user"
|> withArgument "id" (GraphQl.type_ "INT")Turns into:
user(id: INT)Generate an argument, to use with withArgument.
You don't have to handle the $ sign.
fied "user"
|> withArgument "id" (GraphQl.variable "id")Turns into:
user(id: $id)Requests contains the query and the variables of each GraphQl requests.
Entry of every GraphQL values to turn them into requests, which can be launched!
object []
|> flip (query "https://example.com") decoder
|> send msgAdd variables to a requests. Useful when defining variables in your GraphQL request.
object []
|> flip (query "https://example.com") decoder
|> addVariables []
|> send msgActually send the GraphQL request! Generates a Cmd, to give to Elm in your update.
Handle GraphQL values.