jamesmacaulay / elm-graphql

A GraphQL library for Elm
http://package.elm-lang.org/packages/jamesmacaulay/elm-graphql/latest
BSD 3-Clause "New" or "Revised" License
313 stars 19 forks source link

Mutation - extra '!' #11

Closed MartinKavik closed 7 years ago

MartinKavik commented 7 years ago

Thank You very much for this package! I am new in the amazing Elm world, but love it and want to build our websites with it and your library. However I came across to a bug (?) and I'm not "Elm-experienced" enough to fix it / send PR alone yet.

Problem: (!! on the first line - it has to be !)

mutation ($intId: Int!, $date: String!, $fields: [HistoryItemfieldsHistoryItemField!]!!) 
{
  createHistoryItem(intId: $intId, date: $date, fields: $fields) 
  {
    id  
  }
}

Example Query Variables:

{
  "intId":7,
  "date":"2017-04-23",
  "fields":[
    {"name":"mysaMurtaRozdil","value":2000},
    {"name":"internet","value":499},
    {"name":"majitelka","value":8000},
    {"name":"najem","value":6483}]
}

GraphQL backend:


Elm Code:

import GraphQL.Request.Builder exposing (..)
import GraphQL.Request.Builder.Arg as Arg
import GraphQL.Request.Builder.Variable as Var
import GraphQL.Client.Http as GraphQLClient

type alias HistoryItem =
    { id : Int
    , date : String
    , data : List ( String, Float )
    }

-- historyItemExample =
--     HistoryItem 123
--         "2015-02-03"
--         [ ( "mysaMurtaRozdil", 1500 )
--         , ( "internet", 455 )
--         , ( "majitelka", 7500 )
--         , ( "najem", 5000 )
--         ]

saveHistoryItemRequest : HistoryItem -> Request Mutation Int
saveHistoryItemRequest historyItem =
    let        
        historyItemField =
            -- is this naming correct / is it possible to generate brackets [..]?
            Var.object "[HistoryItemfieldsHistoryItemField!]"
                [ Var.field "name" Tuple.first Var.string
                , Var.field "value" Tuple.second Var.float
                ]

        intIdVar =
            Var.required "intId" .id Var.int

        dateVar =
            Var.required "date" .date Var.string

        fieldsList =
            Var.required "fields" .data (Var.list historyItemField)
    in
        extract
            (field "createHistoryItem"
                [ ( "intId", Arg.variable intIdVar )
                , ( "date", Arg.variable dateVar )
                , ( "fields", Arg.variable fieldsList )
                ]
                (extract (field "id" [] int))
            )
            |> mutationDocument
            |> request historyItem

-- ....

Thank You for a fix / direction me to the right solution!


P.S. An idea - You could add jamesmacaulay/elm-graphql to

jamesmacaulay commented 7 years ago

I'm not sure why you'd get exactly that output from that Elm code, but I can tell you that the first argument to Var.object should not include any non-null or (!) or list ([]) modifiers. It should merely be the unmodified name of a single type in the GraphQL schema you're working with. What's still confusing to me, though, is why the type would be serialized as [HistoryItemfieldsHistoryItemField!]!! instead of as [[HistoryItemfieldsHistoryItemField!]!]!. I would have expected the Var.list wrapper that you use to insert an extra pair of brackets.

(The name you're using there, also, is surprising to me: "HistoryItemfieldsHistoryItemField". Is that whole string the name of one of your types?)

MartinKavik commented 7 years ago

Well, leave this issue to me, I'll try to fix it and create a more isolated and testable example + maybe send PR. Thanks.

jamesmacaulay commented 7 years ago

fixed with #12, thanks!