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)
)A type for chaining request configuration.
Start building a GET request with a given URL.
GraphQL.Selector.succeed Tuple.pair
|> 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 Tuple.pair
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> post "https://example.com/graphql"Add a single header to a request.
GraphQL.Selector.succeed Tuple.pair
|> 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 Tuple.pair
|> 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 Tuple.pair
|> 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 Tuple.pair
|> 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 Tuple.pair
|> 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 Tuple.pair
|> 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 Tuple.pair
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withCredentials TrueSet a path of graphql data. Default value is [ "data" ].
GraphQL.Selector.succeed Tuple.pair
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> withDataPath [ "my", "custom", "data", "path" ]A Request can fail in a couple ways:
Send the cancelable Http request.
Try to cancel an ongoing cancelable Http request.
Convert the Request to a Task with all options applied.
GraphQL.Selector.succeed Tuple.pair
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> toTask
-- makes a task of request to https://example.com/graphqlConvert the Request to a Task 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.
GraphQL.Selector.succeed Tuple.pair
|> GraphQL.Selector.field "me" [] userSelector
|> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
|> query "InitialData"
|> get "https://example.com/graphql"
|> toTaskWithCacheBuster "cache_buster"
-- makes a task of request to https://example.com/graphql?cache_buster=1481633217383