Do Fragments somehow interfiere with normalised ca...
# apollo-kotlin
m
Do Fragments somehow interfiere with normalised cache? I have a query that reutrns ( among other things) a list of defined Objects int schema. This objects have an id so I defined de declarative cache keys in
extra.grapqhqls
. I've made a fragment on that object and thats what Im querying but upong inspecting the cache I cant see entries for those object/types ( I woudl expect something similar to
"author(\"id\": \"au456\")": {"id": "au456", "name": "Pierre Bordage"}
The only difference that I can see from the docs is the fact that I use fragments?
m
Fragments should not interfere, they disappear during normalisation
Can you double check that the id is present in the generated query document (in your operation companion object
OPERATION_DOCUMENT
Also if you can share the type + extension + query that'd help a lot investigating this
m
Can you double check that the id is present in the generated query document (in your operation companion object
OPERATION_DOCUMENT
Not exactly sure what you mean bit is does not look likes it?
Copy code
fragment componentInfo on Component { fieldsetId fieldsetType id type assets { __typename ...assetInfo } text integer float boolean selection list isMutable }
πŸ™ 1
Also if you can share the type + extension + query that'd help a lot investigating this
Sure
extension:
Copy code
extend type  Component  @typePolicy(keyFields: "id")
Type:
Copy code
type Component {
  fieldsetId: UUID!

  fieldsetType: String! @deprecated(reason: "Use fieldsetTemplateCode")

  fieldsetTemplateCode: TaskTemplateName!

  id: UUID!

  createdAt: DateTime

  updatedAt: DateTime

  lastEditedBy: OESUserType

  type: ComponentType!

  boolean: Boolean

  selection: [ComponentSelectionType!]

  list: [String!]

  text: String

  assets: [FileAsset!]

  integer: Int

  float: Float

  isMutable: Boolean!

  label: String

  comments: [Comment!]
}
And the query:
Copy code
FetchWorkOrder($id: ID!) {
    workOrder(id: $id) {
        ...workOrderInfo
    }
    validFieldsetOptions(
        workOrderId: $id
    ) {
        value
        label
    }
}

fragment workOrderInfo on WorkOrderType {
    externalId
    type
    status
    template {
        label
        value
    }
    project {
         property {
             id
             postcode
             address
             coordinates {
                 latitude
                 longitude
             }
         }

         customer {
             id
             accountNumber
             name
             phone
             email
             familyName
             givenName
         }
    }
    fieldsets {
        ...fieldSetInfo
    }
    project {
        type
        ... customerType
        ... propertyType
    }
}

fragment fieldSetInfo on Fieldset {
    id
    type
    status
    fieldsetDescription
    components {
        ...componentInfo
    }
    isComplete
}

fragment componentInfo on Component {
    fieldsetId
    fieldsetType
    id
    type
    assets {
        ...assetInfo
    }
    text
    integer
    float
    boolean
    selection
    list
    isMutable
}
m
Thanks! That looks correct at first glance. And you're not seeing a cache entry for
Component:$id
?
Copy code
fragment componentInfo on Component { fieldsetId fieldsetType id type assets { __typename ...assetInfo } text integer float boolean selection list isMutable }
I wanted to double check that the selection for Component was containing
id
, which is the case
m
Thanks! That looks correct at first glance. And you're not seeing a cache entry for
Component:$id
?
Exactly! I do see entries like
workOrder({"id":"J-7E494E4C"}).fieldsets.13.components.0
But not for Component. Not sure if the extension its being picked up but other than have it on the extra.grapqhqls file I dont think Im suposed to to anything else?
m
extra.grapqhqls
Sorry I missed that earlier. It should be just
extra.graphqls
(there's an extra 'q', could it be just that?)
m
sorry thats just a typo πŸ˜„
πŸ‘ 1
m
And you're not overriding the default
cacheKeyGenerator
, right?
m
MM nop
Copy code
ApolloClient.Builder()
    .okHttpClient(
        builder.addInterceptor(authenticationInterceptor)
            .addInterceptor(headerInterceptor)
            .build()
    )
    .serverUrl(environment.current.endpoint.oes + GRAPH_QL_PATH)
    .addCustomScalarAdapter(Date.type, GraphQLDateConverter())
    .doNotStore(true)
    .normalizedCache(SqlNormalizedCacheFactory(APOLLO_CACHE_DB)) // TODO this is not KMP friendly, change when  moving into shared
    .fetchPolicy(FetchPolicy.NetworkOnly)
    .addCustomScalarAdapter(DateTime.type, GraphQLDateTimeConverter())
    .addCustomScalarAdapter(Time.type, GraphQLTimeToLocalTimeConverter())
    .build()
thats how the client its configured
m
the plot thickens...
Next thing you can try is put a breakpoing in the default
CacheKeyGenerator
(around here) and investigate why you're not going into the
keyFields.isNotEmpty()
branch
Make the breakpoint conditional on something like
obj["__typename"] == "Component"
or you will hit the breakpoint many many times
m
will do! thanks!
Copy code
fun CompiledNamedType.keyFields(): List<String> {
  return when (this) {
    is InterfaceType -> keyFields
    is ObjectType -> keyFields
    else -> emptyList()
  }
Si I go through that, and its effectively a a ObjectType
but the
keyFIelds
property is empty
SO its the plugin not generating the keys somehow?
If its any useful here is the gradle setup
Copy code
apollo {

    service("core") {
        packageName.set("energy.octopus.fieldservices.base.core.libraries.network.datasource.graphql")
        mapScalar("DateTime", "org.threeten.bp.ZonedDateTime")
        mapScalar("Date", "org.threeten.bp.LocalDate")
        mapScalar("UUID", "kotlin.String")
        mapScalar("Time", "org.threeten.bp.LocalTime")
        generateAsInternal.set(false)

        schemaFile.set(file("src/main/graphql/schema.graphqls"))
        srcDir([file("src/main/graphql")])

        introspection {
            endpointUrl.set("<https://api.oes-test.systems/api/v1/graphql>")
            schemaFile.set(file("src/main/graphql/schema.graphqls"))
        }
    }
m
There should be something in the generated folder in
$packageName.type.Component
Something like so:
Copy code
public class Component {
  public companion object {
    public val type: ObjectType =
        ObjectType.Builder(name = "Component").keyFields(listOf("id")).build()
  }
}
But looks like the
keyFields
call is not generated in your case indeed, that's interesting
Ah! Can you try removing the
schemaFile.set
?
schemaFile
expects a single merged schema so it will not pick up any type extension
m
O I see! Trying that now. Hopefully is that 🀞
🀞 1
m
If you remove it, the plugin will detect the schema files from
srcDir
. If you want to specify your schema files explicitly, you can do
Copy code
schemaFiles.set(listOf(file("src/main/graphql/schema.graphqls"), file("src/main/graphql/extra.graphqls"))
m
Well Now Im getting errors on the extensions so its deffo picking it up πŸ™‚ . Will fix and check the cache entries
πŸ‘ 1
m
progress! πŸŽ‰
m
Yes!! they are there now
πŸŽ‰ 1
thanks a lot for the time!
πŸ™
m
Sure thing! Thanks for the investigation!