Skip to content

Instantly share code, notes, and snippets.

init : Maybe Token -> Location -> ( Model, Cmd Msg )
init token location =
let model =
{ initialModel
| token = Maybe.map RemoteData.succeed token |> Maybe.withDefault RemoteData.NotAsked
, route = parseLocation location
}
in
fetchPosts model
|> andThen fetchUser
landing : Model -> Html Msg
landing model =
case model.posts of
NotAsked ->
div [] []
Loading ->
withLoader (div [] [])
Success posts ->
RemoteData.map userHeader model.user
|> RemoteData.withDefault authHeader
graphcool: String
graphcool = "https://api.graph.cool/simple/v1/cjbm8w2980rge0186pld48aan"
fetchPosts: Cmd Msg
fetchPosts =
Http.post graphcool (jsonBody <| Encoders.postsQuery) decodePosts
|> RemoteData.sendRequest
|> Cmd.map Messages.OnFetchPosts
createRequest : Form -> String -> Http.Request Post
decodePosts : Decoder.Decoder (List Post)
decodePosts =
Decoder.list decodePost
|> Decoder.field "allPosts"
|> Decoder.field "data"
decodeGraphcoolToken : Decoder.Decoder String
decodeGraphcoolToken =
Decoder.string
|> Decoder.field "token"
postsQuery : Encoder.Value
postsQuery =
Encoder.object
[ ( "query", Encoder.string "query {allPosts(orderBy: createdAt_DESC){id title body}}" ) ]
authenticate : Token -> Encoder.Value
authenticate token =
let
query =
"mutation {authenticate (accessToken: \"" ++ token.accessToken ++ "\"){token}}"
{
"name": "elm-blog",
"version": "1.0.0",
"description": "An example of Elm blog.",
"devDependencies": {
"graphcool-lib": "^0.1.4",
"isomorphic-fetch": "^2.2.1",
"jsonwebtoken": "^8.1.0",
"jwks-rsa": "^1.2.1"
}
type AuthenticationPayload {
id: String!
token: String!
}
extend type Mutation {
authenticate(accessToken: String!) : AuthenticationPayload
}
types: ./types.graphql
functions:
authenticate:
type: resolver
schema: ./src/authentication.graphql
handler:
code:
src: ./src/resolvers/authenticate.js
environment:
AUTH0_DOMAIN: nookit.eu.auth0.com
type User @model {
id: ID! @isUnique
createdAt: DateTime!
updatedAt: DateTime!
auth0UserId: String! @isUnique
email: String! @isUnique
}
type Post @model {
id: ID! @isUnique
...
-- graphcool/
-+ node_modules/
-- src/
| -- resolvers/
| | -- authenticate.js
| -- authentication.graphql
-- .graphcoolrc
-- graphcool.yml
-- package.json