Does the Graphql-SelectionSet.map2 function bring only a check on the number of fields advantage on Pipelines?

Hi,

In « Mapping & Combining » section in elm-graphql/Graphql-SelectionSet document page, I read:

Note: If you run out of mapN functions for building up SelectionSet s, you can use the pipeline which makes it easier to handle large objects, but produces lower quality type errors.

The syntax with map2:

hero : SelectionSet Hero StarWars.Object.Human
hero =
    SelectionSet.map2 Human
        Human.name
        Human.id

The syntax with « Pipelines »:

hero : SelectionSet Hero StarWars.Object.Human
hero =
    SelectionSet.succeed Human
        |> with Human.name
        |> with Human.id

Excluding |> with the 2 code look alike.

Question: does the map2 function bring only a check on the number of fields advantage?

Best regards,
Stéphane

1 Like

I think this largely a matter of taste.

However, mapN do have some advantages to consider:

  1. The types are easier to understand and hence error messages tend to be more straightforward.
  2. Since there are fewer closures created, there is likely to be a slight performance advantage (note: for instance with List this performance advantage could be dramatic, but for GraphQL it will likely be very small).

For these reasons I personally always use mapN if I can.

7 Likes

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