dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.

Home Page:https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enforce nonempty list in required list arguments

prange opened this issue · comments

I have a query

loadPageContentQuery : String -> SelectionSet PageContent RootQuery
loadPageContentQuery slug =
    GraphCMS.Query.page
        { where_ = { id = Absent, title = Absent, slug = Present slug }, stage = Published, locales = [ Locale.En, Locale.De ] } -- (1)
        pageContentSelectionSet
        |> SelectionSet.nonNullOrFail

If I assign an empty list to locales in the required argument record (at (1) ), the server yields an error "Locales cannot be empty".
I am not sure if empty lists should be allowed, but if not - would it be an idea to use NonEmptyList instead of List?

type alias PageRequiredArguments =
    { where_ : GraphCMS.InputObject.PageWhereUniqueInput
    , stage : GraphCMS.Enum.Stage.Stage
-   , locales : List GraphCMS.Enum.Locale.Locale -- (2)
+   , locales : NonEmptyList GraphCMS.Enum.Locale.Locale -- (3)
    }

Unfortunately there is no built-in concept of a non-empty list in GraphQL. It's fairly common to have runtime validations, which is exactly what this message Locales cannot be empty is. So that type information doesn't exist in the schema, and as far as the schema describes it the list is valid as an empty list. Wish there were more nuanced types for some of those things in GraphQL so we could get stronger guarantees. You could always represent it as { first: "First locale", rest: [] }, but that's up to the schema designer to do.

I see, that is too bad.

It s up to the schemadesigner to design safe schemas. Closing.