https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
m

Marco Pierucci

10/17/2023, 2:10 PM
Hello All! Got a doubt regarding interfaces and generated code 🧵
So
ConditionType
is an interface with the following implementations: • ConditionExpressionComponentConditionBoolComponentConditionSelection And because they might have different attributes here's how Im querying it:
Copy code
visibilityCondition {
          __typename
          ... on ComponentConditionBool {
            component
            fieldset
            boolean
          }
          ... on ComponentConditionSelection {
            component
            fieldset
            selection
          }
          ... on ConditionExpression {
            operator
            conditions {
              __typename
              ... on ComponentConditionBool {
                component
                fieldset
                boolean
              }
              ... on ComponentConditionSelection {
                component
                fieldset
                selection
              }
            }
          }
        }
[4:08 PM] SO far all god but the generated code I get is :
Copy code
public data class VisibilityCondition(
    public val __typename: String,
    /**
     * Synthetic field for inline fragment on ComponentConditionBool
     */
    public val onComponentConditionBool: OnComponentConditionBool?,
    /**
     * Synthetic field for inline fragment on ComponentConditionSelection
     */
    public val onComponentConditionSelection: OnComponentConditionSelection?,
    /**
     * Synthetic field for inline fragment on ConditionExpression
     */
    public val onConditionExpression: OnConditionExpression?,
  )

  public data class OnComponentConditionBool(
    public val component: ComponentType,
    public val fieldset: TaskTemplateName,
    public val boolean: Boolean,
  )

  public data class OnComponentConditionSelection(
    public val component: ComponentType,
    public val fieldset: TaskTemplateName,
    public val selection: ComponentSelectionType,
  )

  public data class OnConditionExpression(
    public val `operator`: LogicOperator,
    public val conditions: List<Condition?>,
  )
the generated code is not following the interfaces hireachy I'd hope ( I dont ahve a common interface to represent a
VisibilityCondition
is there any way to achieve this or am i completely misunderstanding interfaces?
s

Stylianos Gakis

10/17/2023, 2:25 PM
If you turn inline fragments you do like:
Copy code
... on ComponentConditionSelection {
  component
  fieldset
  selection
}
into a proper gql fragment
Copy code
fragment FooFragment on MyType
that you reuse, could that be what you are looking for here? And depending on the
codegenModels
option you’ve selected you might be getting a different result here in general, have you looked into that as well?
☝️ 1
m

Marco Pierucci

10/17/2023, 2:26 PM
I'm querying with inline fragments cause that how Ive seen interfaces are queried in the docs and soem blogs 😄 . In any case making not inline should't change much ?Will check the codegenModels option
s

Stylianos Gakis

10/17/2023, 2:29 PM
Doing it with fragments is completely fine, my point here was doing
… on Foo
vs doing
Copy code
fragment MyFragment on Foo {}
And then in your query
Copy code
{
  ...MyFragment
}
Like we do here, see difference between an inline fragment
... on TerminatedInFutureStatus
vs
... UpcomingAgreementChangeFragment
which is defined a bit lower in this file In this case you’d get a super-type for that fragment which you can have common between places where you use it. But I have a feeling I am misunderstanding what you may be going for here.
m

Marco Pierucci

10/17/2023, 2:30 PM
Ohh I see
Nice let me give it a try
🤔
Copy code
public data class BoolConditionFragment(
  public val component: ComponentType,
  public val fieldset: TaskTemplateName,
  public val boolean: Boolean,
) : Fragment.Data
ok never mind what Im going for might not actually be possible I need custom mappings
s

Stylianos Gakis

10/17/2023, 2:46 PM
If you need to have a common interface for
VisibilityCondition
that could be a fragment itself too. So in the
ConditionExpression
case, you can ask for this fragment again. Is that what you are looking for?
Maybe would be good to take a step back and explain what you want to achieve in the end, and someone else might be able to help you better here that way 😄
m

Marco Pierucci

10/17/2023, 2:48 PM
Yeah I need a common interface for the different framgnets ( which are querying impl of an interface) but again thats fine, I nee dot map some other bits anyways