Coder Social home page Coder Social logo

elm-graphql's Introduction

A fork from ghivert/elm-graphql with RemoteData support

Elm GraphQL

Opinion

Just import GraphQL, and write queries! This package suppose your decoders are already written, and do not write decoders. It only provide a nice syntax to do GraphQL queries, and decode the "data" at the root of standard GraphQL for you. Just think on your schema, and don't bother with everything else. By not writing custom decoders, you can make multiple queries on the same data, with different schemas each times. They will always be converted to the same type, avoiding you to rewrote a type for each request like others can do. Moreover, it is purely written in Elm, avoiding you to think to recompile .graphql files.

This package try to be similar to Json.Encode and Http. This allow to write things more easily if you're often involved with Elm!

How to use?

Basically, creates an object with object, add some fields with a list of field, and you're done! You can add some arguments, selectors or alias to the fields, by using the corresponding functions. Otherwise, a (huge) example:

Types.elm

module Types exposing (..)

import Json.Decode as Decode exposing (Decoder, field, maybe, int, string)
import RemoteData


type Msg
  = GraphQlMsg (RemoteData.WebData NameAndAddress)


type alias User =
  { id : Maybe Int
  , name : Maybe Name
  }


type alias Name =
  { firstName : Maybe String
  , lastName : Maybe String
  }


type alias Address =
  { street : Maybe String
  , town : Maybe String
  }

type alias NameAndAddress =
  { user : User
  , address : Address
  }

decodeName : Decoder Name
decodeName =
  Decode.map2 Name
    (maybe (field "first_name" string))
    (maybe (field "last_name" string))


decodeUser : Decoder User
decodeUser =
  Decode.map2 User
    (maybe (field "id" int))
    (maybe (field "name" decodeName))


decodeAddress : Decoder Address
decodeAddress =
  Decode.map2 Address
    (maybe (field "street" string))
    (maybe (field "town" string))


decodeNameAndAddress =
  Decode.map2 NameAndAddress
    (field "user" decodeUser)
    (field "address" decodeAddress)

Requests.elm

module Requests exposing (..)

import GraphQl
import Json.Decode as Decode exposing (Decoder, field)
import Types exposing (User, Address, Msg(..))


userRequest : GraphQl.Value Root
userRequest =
  GraphQl.object
    [ GraphQl.field "user"
      |> GraphQl.withArgument "id" (GraphQl.variable "id")
      |> GraphQl.withAlias "current_user"
      |> GraphQl.withSelectors
        [ GraphQl.field "id"
        , GraphQl.field "name"
          |> GraphQl.withSelectors
            [ GraphQl.field "first_name"
            , GraphQl.field "last_name"
            ]
        ]
    , GraphQl.field "address"
      |> GraphQl.withArgument "city" (GraphQl.string "Paris")
      |> GraphQl.withArgument "id" (GraphQl.int 12)
      |> GraphQl.withArgument "type" (GraphQl.type_ "LOFT")
      |> GraphQl.withSelectors
        [ GraphQl.field "street"
        , GraphQl.field "town"
        ]
    ]
    |> GraphQl.withVariable "id" "INT!"


baseRequest : GraphQl.Value -> Decoder a -> GraphQl.Request a
baseRequest =
  GraphQl.query "/example_endpoint"


sendRequest : Int -> Cmd Msg
sendRequest id =
  baseRequest userRequest decodeNameAndAddress
    |> GraphQl.addVariables [ ("id", Encode.int id) ]
    |> GraphQl.send
    |> Cmd.map GraphQlMsg

Licenced BSD3, enjoy the work! GraphQL is amazingly awesome!

elm-graphql's People

Contributors

ghivert avatar guillaumeboudon avatar

Watchers

Paulo Ahagon avatar Diego Souza avatar James Cloos avatar Bezerra avatar  avatar Guilherme Teodoro avatar Nicholas Reise avatar x3rp4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.