vendrinc / elm-gql

https://www.npmjs.com/package/elm-gql
BSD 3-Clause "New" or "Revised" License
69 stars 13 forks source link

Nested fragments produce the wrong payload query #29

Closed joakin closed 1 year ago

joakin commented 1 year ago

Originally here (only for Vendr folk): https://github.com/Blissfully/ironzion/pull/11352/commits/7a9dfff05db1559bc128225e256e86d60fbe4036

Tried to improve a query to generate better names by using fragments, like specified in LifeOfAQuery.md, which resulted in the following query:

query MyPortal_OnboardingPlans() {
  onboardingPlans {
    ... OnboardingPlan
  }
}

fragment OnboardingPlan on OnboardingPlan {
  icon
  title
  description
  tasks {
    ... OnboardingTask
  }
}

fragment OnboardingTask on OnboardingTask {
  icon
  image
  previewTitle
  fullTitle
  description
  primaryAction {
    __typename
    ... on OnboardingGoToRoute {
      label
      route
    }
  }
  secondaryAction {
    __typename
    ... on OnboardingAppCue {
      label
      id
    }
    ... on OnboardingExternalLink {
      label
      url
    }
  }
  completed
}

This query fails when run at runtime with the error:

ERROR: Unknown fragment "OnboardingTask".

GraphQL request:98:11
97 | description
98 | tasks {...OnboardingTask} }
   |           ^
99 | fragment OnboardingTask_batch_1 on OnboardingTask {icon

ERROR: Fragment "OnboardingTask_batch_1" is never used.

GraphQL request:99:1
 98 | tasks {...OnboardingTask} }
 99 | fragment OnboardingTask_batch_1 on OnboardingTask {icon
    | ^
100 | image

The generated code for the fragments in the query is:

toFragments_ : Int -> String
toFragments_ version_ =
    String.join
        """
"""
        [ ("fragment " ++ GraphQL.Engine.versionedName version_ "OnboardingPlan"
          )
            ++ """ on OnboardingPlan {icon
title
description
tasks {...OnboardingTask} }"""
        , ("fragment " ++ GraphQL.Engine.versionedName version_ "OnboardingTask"
          )
            ++ """ on OnboardingTask {icon
image
previewTitle
fullTitle
description
primaryAction{__typename
... on OnboardingGoToRoute {label
route}}
secondaryAction{__typename
... on OnboardingAppCue {label
id}
... on OnboardingExternalLink {label
url}}
completed }"""
        ]

Which uses the versioned fragment name when defining the OnboardingTask fragment, but it doesn't use it in the OnboardingPlan fragment itself, resulting in the above mentioned error.

mdgriffith commented 1 year ago

This should be fixed with 0.7.0!