GraphQL source #246
-
Hi, GraphQL is getting very popular in the CMS world, is it possible (out of the box) to pull inn content with GraphQL at buildtime and "Browsertime"? Should it be / Could it be? |
Beta Was this translation helpful? Give feedback.
Answered by
dillonkearns
Nov 2, 2021
Replies: 1 comment 1 reply
-
Hello! This is the perfect spot for this question 👍 I do this quite often with module Request exposing (staticGraphqlRequest)
import DataSource exposing (DataSource)
import DataSource.Http
import Graphql.Document
import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet exposing (SelectionSet)
import Json.Encode as Encode
import Pages.Secrets as Secrets
staticGraphqlRequest : SelectionSet value RootQuery -> DataSource value
staticGraphqlRequest selectionSet =
DataSource.Http.unoptimizedRequest
(Secrets.succeed
{ url = graphqlEndpointUrl
, method = "POST"
, headers = []
, body =
DataSource.Http.jsonBody
(Encode.object
[ ( "query"
, selectionSet
|> Graphql.Document.serializeQuery
|> Encode.string
)
]
)
}
)
(selectionSet
|> Graphql.Document.decoder
|> DataSource.Http.expectUnoptimizedJson
)
graphqlEndpointUrl : String
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
prange
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! This is the perfect spot for this question 👍
I do this quite often with
dillonkearns/elm-graphql
and I would definitely recommend it! Here's a code snippet you can use for that: https://github.com/dillonkearns/incremental-elm-web/blob/1d6248dafbb9c8918fb47ebe9322ad55f1c8b2da/src/Request.elm.