GraphQL queries and mutations made easy in Elm! This package provides easier way to deal with GraphQL queries and mutations. 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 exposing (Mutation, Query, Variables, Named, Operation)
import Http
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
-- Define the request.
userRequest : Operation Query Variables
userRequest =
GraphQl.named "MySuperQuery"
[ 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.withArgument "id" (GraphQl.type_ "INT!")
]
, 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.withVariables [ ("id", "INT!") ]
userModifying : Operation Mutation Named
userModifying =
GraphQl.named "MySuperMutation"
[ GraphQl.field "CreateUser"
|> GraphQl.withArgument "user"
(GraphQl.input
[ ("first", GraphQl.string "John")
, ("last", GraphQl.string "Doe")
]
)
]
-- And Send It!
sendRequest : Int -> (Result Http.Error a -> msg) -> Decoder a -> Cmd msg
sendRequest id msg decoder =
GraphQl.query "/example_endpoint" userRequest decoder
|> GraphQl.addVariables [ ("id", Encode.int id) ]
|> GraphQl.send msgHandle GraphQL values.
Generates a Value, from a list of fields.
object
[ field "user" ]Turns into:
{
user
}Generates a Value with a name.
named "MySuperRequest"
[ field "user" ]Turns into:
MySuperRequest {
user
}Generates a field.
Adds 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
}Adds variables to an Operation.
"UserRequest"
[ field "user" ]
|> withVariables [ ("id", "id") ]Turns into:
query UserRequest(id: $id) {
user
}Adds 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
}Adds 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.
Generates an argument, to use with withArgument.
field "user"
|> withArgument "id" (GraphQl.int 12)Turns into:
user(id: 12)Generates an argument, to use with withArgument.
field "user"
|> withArgument "admin" (GraphQl.bool False)Turns into:
user(id: false)Generates an argument, to use with withArgument.
field "user"
|> withArgument "score" (GraphQl.float 12.1)Turns into:
user(id: 12)Generates an argument, to use with withArgument.
field "user"
|> withArgument "id" (GraphQl.string "12")Turns into:
user(id: "12")Generates an argument, to use with withArgument.
Generate a type in GraphQL.
field "user"
|> withArgument "id" (GraphQl.type_ "INT")Turns into:
user(id: INT)Generates an argument, to use with withArgument.
You don't have to handle the $ sign.
field "user"
|> withArgument "id" (GraphQl.variable "id")Turns into:
user(id: $id)Generates an argument, to use with 'withArgument'.
field "CreateUser"
|> withArgument "user"
(GraphQl.input
[ ("first", (GraphQl.string "John"))
, ("last", (GraphQl.string "Doe"))
]
)Turns into:
CreateUser(user: {first: "John", last: "Doe"})Generates an argument, to use with 'withArgument'.
field "CreateUser"
|> withArgument "users"
(GraphQl.nestedInput
[ [ ("first", (GraphQl.string "John"))
, ("last", (GraphQl.string "Doe"))
]
, [ ("first", (GraphQl.string "Jane"))
, ("last", (GraphQl.string "Smith"))
]
]
)Turns into:
CreateUsers(users: [
{first: "John", last: "Doe"},
{first: "Jane", last: "Smith"}
])Requests contains the query or the mutation and the variables of each GraphQL requests. The variables can't be used with an anonymous Request, due to the nature of GraphQL.
Entry of every GraphQL query to turn them into requests, which can be launched!
object []
|> flip (query "https://example.com") decoder
|> send msgEntry of every GraphQL mutation to turn them into requests, which can be launched!
object []
|> flip (mutation "https://example.com") decoder
|> send msgAdd variables to a request. Useful when defining variables in your GraphQL request. If no variables has been defined in your operation, no variables can be added: the compiler will reject it.
named [ field "user" |> withArgument "id" (variable "id") ]
|> withVariables [ ("id", "INT") ]
|> flip (query "https://example.com") decoder
|> addVariables [ ("id", Encode.int 12) ]
|> send msgSends the GraphQL request! Generates a Cmd, to feed the runtime in your update.
Handle GraphQL operations