# Generate flags on backend

**URL:** <https://discourse.elm-lang.org/t/generate-flags-on-backend/1213>\
**Category:** Request Feedback\
**Created:** [May 3, 2018, 8:20pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213 "2018-05-03T20:20:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![paparga](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paparga/32/932_2.png) [@paparga](https://discourse.elm-lang.org/u/paparga)\
**Post date:** [May 3, 2018, 8:20pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/1 "2018-05-03T20:20:36Z")

</div>

Hi elm Folks!!

We are considering generating initial data as flags, instead of having an initial fetch.

One of the upside it’s eliminating maybes from the model, but we don’t know what are the downsides or even if it is a good idea after all.

Anyone has tried something like this? Or can have some insights?

Thanks!

Pablo

---

<div class="post-metadata">

**Author:** ![charliek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/charliek/32/3865_2.png) [@charliek](https://discourse.elm-lang.org/u/charliek)\
**Post date:** [May 3, 2018, 10:22pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/2 "2018-05-03T22:22:05Z")

</div>

> [@paparga](#):
>
> One of the upside it’s eliminating maybes from the model

Just a quick note related to the topic, you may be interested in [5 minutes from a talk by Kris Jenkins](https://youtu.be/6mUAvd6i4OU?t=767) regarding Maybes and union types for representing async data.

This is something I’ve written and spoke about and I believe @joelq has too. There are other ways of eliminating Maybes if that is truly your end goal.

---

<div class="post-metadata">

**Author:** ![paparga](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paparga/32/932_2.png) [@paparga](https://discourse.elm-lang.org/u/paparga)\
**Post date:** [May 3, 2018, 10:42pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/3 "2018-05-03T22:42:50Z")

</div>

One of the end goals is to use a primitive instead of a union type. The whole app makes more sense if you are dealing with a primitive, instead of dealing with cases or passing values as arguments. If you don’t have that value is because the app it’s not ready.

Right now we have an inner app with primitives and an outer app that deals with fetching and union types. It works for us, but I don’t think that is idiomatic and also more complex.

---

<div class="post-metadata">

**Author:** ![ericgj](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ericgj/32/85_2.png) [@ericgj](https://discourse.elm-lang.org/u/ericgj)\
**Post date:** [May 3, 2018, 11:09pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/4 "2018-05-03T23:09:15Z")

</div>

> [@paparga](#):
>
> initial data as flags

Do you mean static data, generated and embedded in the HTML (or imported via javascript)?

Or do you mean dynamic data, i.e. doing a fetch from javascript before initializing the elm app?

The first seems fine to me, the second seems less safe than doing it within Elm.

Note that if you are passing in the flags data as `Json.Value`, I think you are still inside a `Result` , unless you `Debug.crash` on a decode failure (which won’t be allowed in production apps in the future).

---

<div class="post-metadata">

**Author:** ![paparga](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paparga/32/932_2.png) [@paparga](https://discourse.elm-lang.org/u/paparga)\
**Post date:** [May 3, 2018, 11:21pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/5 "2018-05-03T23:21:26Z")

</div>

it’s the first one.

> [@ericgj](#):
>
> which won’t be allowed in production apps in the future

Thanks for the info!

---

<div class="post-metadata">

**Author:** ![brasilikum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brasilikum/32/524_2.png) [@brasilikum](https://discourse.elm-lang.org/u/brasilikum)\
**Post date:** [May 4, 2018, 12:17am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/6 "2018-05-04T00:17:59Z")

</div>

I have done it and it works fine. Just (as always) be aware of the edgecases. You don’t want your app to fail init because a list is null instead of empty.

---

<div class="post-metadata">

**Author:** ![paparga](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paparga/32/932_2.png) [@paparga](https://discourse.elm-lang.org/u/paparga)\
**Post date:** [May 4, 2018, 12:36am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/7 "2018-05-04T00:36:39Z")

</div>

> [@brasilikum](#):
>
> list is null instead of empty.

I wasn’t aware 😅. I will study those cases

---

<div class="post-metadata">

**Author:** ![brasilikum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brasilikum/32/524_2.png) [@brasilikum](https://discourse.elm-lang.org/u/brasilikum)\
**Post date:** [May 4, 2018, 12:40am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/8 "2018-05-04T00:40:58Z")

</div>

Well, fortunately not in elm but in whatever language you use to pass the data to the flags

---

<div class="post-metadata">

**Author:** ![christian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/christian/32/1384_2.png) [@christian](https://discourse.elm-lang.org/u/christian)\
**Post date:** [May 4, 2018, 1:50am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/9 "2018-05-04T01:50:01Z")

</div>

We are doing something like this. We treat the flags parameter passed into the init function as a `Value` type then apply a custom decoder to it using `decodeValue` to produce a “configuration” record with several fields containing primitives.

In the event that the decoding fails, we set an error value in the model that notifies the user that the app could not be initialized correctly (and display some debug information produced by the `Err` result). This approach works very well for data that must be generated from your back end and is required to intersection initialize the app.

---

<div class="post-metadata">

**Author:** ![rtfeldman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rtfeldman/32/50_2.png) [@rtfeldman](https://discourse.elm-lang.org/u/rtfeldman)\
**Post date:** [May 4, 2018, 3:56am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/10 "2018-05-04T03:56:41Z")

</div>

We do this all over the place at work. Our flag type is always `Json.Decode.Value`. We run a decoder on it to decode the data from the server. If it succeeds, we have our initial `Model` and proceed as normal. If it fails, we render an error message to the user and send an error report to our logging service ([Rollbar](http://rollbar.com)).

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [May 4, 2018, 5:16am UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/11 "2018-05-04T05:16:57Z")

</div>

> [@paparga](#):
>
> Anyone has tried something like this? Or can have some insights?

I also do this in some of my projects. I have the initial user data rendered into the main html page via a phoenix template and load it all in Elm through the flags. Works great.

---

<div class="post-metadata">

**Author:** ![StoatPower](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stoatpower/32/751_2.png) [@StoatPower](https://discourse.elm-lang.org/u/StoatPower)\
**Post date:** [May 4, 2018, 3:27pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/12 "2018-05-04T15:27:55Z")

</div>

We also rely heavily on this approach. We have loads of configuration that is described in YAML, combined in various ways during the build step based on feature flags and cli parameters, translated to json, further combined with a handful of flags written to the response from the Razor page, and then finally sent to the Elm app as a `Json.Decode.Value` where it can be chunked out into the various feature configs. Like @rtfeldman mentions, if any decoding of a config chunk fails for a particular feature, we render an error message where appropriate and report to Rollbar.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 14, 2018, 3:28pm UTC](https://discourse.elm-lang.org/t/generate-flags-on-backend/1213/13 "2018-05-14T15:28:00Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
