Building and sending GraphQL.
Building of HTTP request has been based on elm-http-builder.
Build a GraphQL named query.
GraphQL.Selector.succeed identity
|> GraphQL.Selector.field "me"
[]
(GraphQL.Selector.succeed User
|> GraphQL.Selector.field "id" [] GraphQL.Selector.string
|> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
|> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
)
|> query "Me"It collects a GraphQL which is equal to:
"""
query Me {
me {
id
firstName
lastName
}
}
"""and decoder is equal to:
Decode.field "me"
(Decode.map3
(Decode.field "id" Decode.string)
(Decode.field "firstName" Decode.string)
(Decode.field "lastName" Decode.string)
)Build a GraphQL named mutation.
GraphQL.Selector.succeed identity
|> GraphQL.Selector.field "updateMe"
[ ( "firstName", GraphQL.Argument.string "Tom" )
]
(GraphQL.Selector.succeed User
|> GraphQL.Selector.field "id" [] GraphQL.Selector.string
|> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
|> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
)
|> mutation "RenameUser"It collects a GraphQL which is equal to:
"""
mutation RenameUser {
updateMe("firstName": "Tom") {
id
firstName
lastName
}
}
"""and decoder is equal to:
Decode.field "updateMe"
(Decode.map3
(Decode.field "id" Decode.string)
(Decode.field "firstName" Decode.string)
(Decode.field "lastName" Decode.string)
)Build a GraphQL named subscription.
GraphQL.Selector.succeed identity
|> GraphQL.Selector.field "onUpdateMe"
[]
(GraphQL.Selector.succeed User
|> GraphQL.Selector.field "id" [] GraphQL.Selector.string
|> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
|> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
)
|> subscription "OnUpdateUser"It collects a GraphQL which is equal to:
"""
subscription OnUpdateUser {
onUpdateMe {
id
firstName
lastName
}
}
"""and decoder is equal to:
Decode.field "onUpdateMe"
(Decode.map3
(Decode.field "id" Decode.string)
(Decode.field "firstName" Decode.string)
(Decode.field "lastName" Decode.string)
)Render GraphQL representation from a GraphQL.
Build a Decoder from a GraphQL.
A type for chaining request configuration.
Start building a GET request with a given URL.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"Start building a POST request with a given URL.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"Add a single header to a request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withHeader "Content-Type" "application/json"
|> withHeader "Accept" "application/json"Add many headers to a request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withHeaders
[ ( "Content-Type", "application/json" )
, ( "Accept", "application/json" )
]Add a bearer token to a request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withBearerToken "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"Add a query param to the url for the request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withQueryParams "hello" "world"
|> withQueryParams "baz" "qux"
-- sends a request to https://example.com/graphql?hello=world&baz=quxAdd some query params to the url for the request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withQueryParams [ ( "hello", "world" ), ( "foo", "bar" ) ]
|> withQueryParams [ ( "baz", "qux" ) ]
-- sends a request to https://example.com/graphql?hello=world&foo=bar&baz=quxSet the timeout setting on the request.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withTimeout (10 * Time.second)Set the withCredentials flag on the request to True. Works via
XMLHttpRequest#withCredentials.
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withCredentials TrueSend the request with a Time based cache buster added to the URL. You provide a key for an extra query param, and when the request is sent that query param will be given a value with the current timestamp.
type Msg
= InitialData (Result Http.Error (User, List Article))
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withCacheBuster "cache_buster"
|> send InitialData
-- makes a request to https://example.com/graphql?cache_buster=1481633217383Set a decoder of data container. By default it set as Decode.field "data".
GraphQL.Selector.succeed (,)
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withDataDecoder (Decode.at [ "my", "custom", "data", "path" ])A Request can fail in a couple ways:
Extract the Http.Request component of the builder in case you want to use it directly.
This function is lossy and will discard some of the extra stuff that HttpBuilder allows you to do.
Things that will be lost:
withCacheBusterConvert the Request to a Task with all options applied.
toTask differs from toRequest in that it retains all extra behavior allowed by
HttpBuilder, including:
withCacheBusterSend the request.