What is right part in "query : SelectionSet Response RootQuery" type annotations?

I try to understand this line:

query : SelectionSet Response RootQuery

I understand this is a “Type Annotations” syntax, but I don’t find documentation example or explanation about multiple “word” separated by whitespace.

I see these examples:

  • answer : Int
  • factorial : Int -> Int
  • distance : { x : Float, y : Float } -> Float
  • add : number -> number -> number (ref)

I found anywhere query: Int Int Int syntax, neither in Elm Syntax nor in Beginning Elm nor in Elm FAQ.

Do SelectionSet, Response, RootQuery are

  • functions arguments?
  • multi value function results?

Best regards,

Stéphane

I can’t help you with your exact situation, but in general if there are

multiple “word” separated by whitespace

it’s because that type is “constructed” from multiple types. So the first word is the “generic” type and the other words are the “type parameters”. Imagine a list of numbers: Its type would be

value : List Int
value =
   [ 1, 2, 3 ]

or a piece of HTML markup that can trigger your specific MyMsg type:

type MyMsg
  = DoStuff

viewButton : Html MyMsg
viewButton =
  button [ onClick DoStuff ] [ text "Click me to do stuff!" ]

Other classic examples are Dict Int String for example: A dictionary that maps Int keys to String values. There are also Maybe and Result documented in the official documentation, which use type parameters.

The documentation page Reading types might also help you understand how to read the type annotations.

2 Likes

As @IloSophiep explained, these are “type variables”. You might know them from other languages as “generics”. They allow you to define types and functions that focus on the “shape” of something instead of the particular type of content. Types can have multiple variables that are separated by spaces.

In this particular case a dillonkearns/elm-graphql's SelectionSet is a structure that represents a set of selected fields from a GraphQL type. The first type variable Response indicates the Elm type that you’d get after running the query. The second type variable (scope) indicates that this selection can be peformed on the RootQuery GraphQL type.

3 Likes

Also of interest: the elm-graphql library has an FAQ page answering how do I read the type of SelectionSet?

3 Likes

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