Send requests to your GraphQL endpoint. See this live code demo
or the examples/
folder for some end-to-end examples.
The builder syntax is inspired by Luke Westby's
elm-http-builder package.
Represents the two types of errors you can get, an Http error or a GraphQL error.
See the Graphql.Http.GraphqlError module docs for more details.
Request PipelineInitialize a basic request from a Query. You can add on options with withHeader,
withTimeout, withCredentials, and send it with Graphql.Http.send.
Initialize a basic request from a Mutation. You can add on options with withHeader,
withTimeout, withCredentials, and send it with Graphql.Http.send.
Exactly like queryRequest, but with an option to use the HTTP GET request
method. You will probably want to use GetIfShortEnough, which uses GET if the
full URL ends up being under 2000 characters, or POST otherwise, since some browsers
don't support URLs over a certain length.
GetIfShortEnough will typically do what you need. If you must use GET no matter
what when hitting your endpoint, you can use AlwaysGet.
queryRequest always uses POST since some GraphQL API's don't support GET
requests (for example, the Github API assumes that you are doing an introspection
query if you make a GET request). But for semantic reasons, GET requests
are sometimes useful for sending GraphQL Query requests. That is, a GraphQL Query
does not perform side-effects on the server like a Mutation does, so a GET
indicates this and allows some servers to cache requests. See
this github thread from the Apollo project
for more details.
Union type to pass in to queryRequestWithHttpGet. Only applies to queries.
Mutations don't accept this configuration option and will always use POST.
Request OptionsAdd a header.
makeRequest : Cmd Msg
makeRequest =
query
|> Graphql.Http.queryRequest "https://api.github.com/graphql"
|> Graphql.Http.withHeader "authorization" "Bearer <my token>"
|> Graphql.Http.send (RemoteData.fromResult >> GotResponse)Add a timeout.
Set with credentials to true.
Add query params. The values will be Uri encoded.
makeRequest : Cmd Msg
makeRequest =
query
|> Graphql.Http.queryRequest "https://api.github.com/graphql"
|> Graphql.Http.withQueryParams [ ( "version", "1.2.3" ) ]
|> Graphql.Http.send (RemoteData.fromResult >> GotResponse)RequestSend the Graphql.Request
You can use it on its own, or with a library like
RemoteData.
import Graphql.Http
import Graphql.OptionalArgument exposing (OptionalArgument(..))
import RemoteData exposing (RemoteData)
type Msg
= GotResponse RemoteData (Graphql.Http.Error Response) Response
makeRequest : Cmd Msg
makeRequest =
query
|> Graphql.Http.queryRequest "https://elm-graphql.herokuapp.com/"
|> Graphql.Http.withHeader "authorization" "Bearer abcdefgh12345678"
-- If you're not using remote data, it's just
-- |> Graphql.Http.send GotResponse
-- With remote data, it's as below
|> Graphql.Http.send (RemoteData.fromResult >> GotResponse)If any errors are present, you will get a GraphqlError that includes the details.
GraphQL allows for partial data to be returned in the case of errors so you can
inspect the data returned in the GraphqlError if you would like to try to recover
any data that made it through in the response.
Convert a Request to a Task. See Graphql.Http.send for an example of
how to build up a Request.
ErrorsUseful when you don't want to deal with the recovered data if there is ParsedData.
Just a shorthand for mapError that will turn any ParsedData into ().
An internal request as it's built up. Once it's built up, send the request with
Graphql.Http.send.