Skip to content

Polymorphic Variants capturing a Union with Fragments #215

Closed
@GTDev87

Description

@GTDev87

Hi, I'm using "reason-apollo-client": "1.0.0-beta.0", and "@reasonml-community/graphql-ppx": "1.0.1",

and I'm having trouble interpreting unions into polymorphic variants.

Using a Js.log, I have an object which looks like the following:

{__typename: "MultipleChoiceQuestion", id: "91d7d8ec-a581-56ef-8467-ca4d54da1dd7", questionBaseId: "91d7d8ec-a581-56ef-8467-ca4d54da1dd7", questionBase: {…}, choices: Array(4)}choices: (4) [{…}, {…}, {…}, {…}]

With this graphql fragment:

module Fields = [%graphql
  {|
    fragment TestFields on Test {
      questions {
        __typename
        ...on LongAnswerQuestion {
          ...LongAnswerQuestionFields
        }
        ...on MultipleChoiceQuestion {
          ...MultipleChoiceQuestionFields
        }
        ...on TrueFalseQuestion {
          ...TrueFalseQuestionFields
        }
        ...on FillInTheBlankQuestion {
          ...FillInTheBlankQuestionFields
        }
      }
   |}
 ];

and the polymorphic variant is returning:

{__typename: "MultipleChoiceQuestion", id: "91d7d8ec-a581-56ef-8467-ca4d54da1dd7", questionBaseId: "91d7d8ec-a581-56ef-8467-ca4d54da1dd7", questionBase: {…}, choices: Array(4)}choices: (4) [{…}, {…}, {…}, {…}]
LongAnswerQuestion q
undefined

Capture through here:

  Js.log("Question_Model question");
  Js.log(question);
  switch (question) {
  | `LongAnswerQuestion(q) =>{
    Js.log("LongAnswerQuestion q");
    Js.log(q);
    q
    |> LongAnswerQuestion.Model.objectToId
    |> Schema.LongAnswerQuestion.idToString
    |> q => UnionSchema.Question.LongAnswer(q)
  }
  | `MultipleChoiceQuestion(q) =>{
    Js.log("MultipleChoiceQuestion q");
    Js.log(q);
    q
    |> MultipleChoiceQuestion.Model.objectToId
    |> Schema.MultipleChoiceQuestion.idToString
    |> q => UnionSchema.Question.MultipleChoice(q)
   }
//Where question is of type 
type unionType = [
  | `LongAnswerQuestion(LongAnswerQuestionFields.t)
  | `MultipleChoiceQuestion(MultipleChoiceQuestionFields.t)
  | `TrueFalseQuestion(TrueFalseQuestionFields.t)
  | `FillInTheBlankQuestion(FillInTheBlankQuestionFields.t)
  | `FutureAddedValue(Js.Json.t)
];
   ...

So the polymorphic variant is interpreted as the incorrect type and is returning undefined when passed through the switch statement.

Do i need a __typespec somewhere to inform the union?
Is there a working example of unions being evaluated into fragments that i can follow? I believe I had this working in 1.0.0-beta.11.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions