Skip to content

JSONAPISource Serialization Problem #987

@gang-qiu

Description

@gang-qiu

Hello there, Thanks for this amazing library! I have been following the setup instructions and I was able to set up my schema, load it into Memory and JSONAPI sources, query my backend and it is responding with standard json-api format. However, I was quite surprised to see that the result of the query is just the raw json-api body, and it appears the data isn't being parsed. I am wondering if I have to explicitly pass in a serializer to my JsonApiSources configurations?

const localCacheSource = new MemorySource({
      schema,
      name: 'localCache'
    })
    setLocalCache(localCacheSource)

    // set up api service
    const apiServiceSource = new JSONAPISource({
      schema,
      name: 'apiService',
      host: baseUrl,
      defaultFetchSettings: {
        headers: {
          Authorization: `Bearer ${authToken}`
        }
      },

      // pluralize resource names in urls properly. ie, entity model => entities (defaults to entitys)
      // https://github.com/orbitjs/orbit/issues/885#issuecomment-1065926542
      serializerSettingsFor: buildSerializerSettingsFor({
        sharedSettings: {
          inflectors: {
            pluralize: buildInflector(
              { entity: 'entities'},
              (input) => `${input}s`
            ),
            singularize: buildInflector(
              { entities: 'entity'},
              (arg) => arg.substring(0, arg.length - 1)
            )
          }
        }
      })
    })
    setApiService(apiServiceSource)

    // set up coordinator
    const coordinator = new Coordinator({
      sources: [localCacheSource, apiServiceSource]
    })

    coordinator.addStrategy(
      new RequestStrategy({
        source: 'localCache',
        on: 'beforeQuery',
        target: 'apiService',
        action: 'query',
        blocking: true,
      })
    )

    // Update the apiSource server whenever the memory source is updated
    coordinator.addStrategy(
      new RequestStrategy({
        source: 'localCache',
        on: 'beforeUpdate',
        target: 'apiService',
        action: 'update',
        blocking: false,
      })
    )
    await coordinator.activate()

   apiServiceSource.query(q => q.findRelatedRecords(
        {type: 'entity', id: '1'}, 'documents')
      ).then(data => console.log(data))

// in the console i see the serialized json response

{
  "data": [
    {
      "id": "1",
      "type": "document",
      "attributes": {
        "name": "test document"
      },
      "relationships": {
        "documentType": {
          "data": {
            "id": "1",
            "type": "documentType"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "1",
      "type": "documentType",
      "attributes": {
        "name": "Certificate of Incorporation"
      }
    }
  ]
}

I expect to see...

{name: 'test document', documentType: {id: "1", name: "Certificate of Incorporation"}}

Thank you in advance

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