Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONAPISource Serialization Problem #987

Open
gang-qiu opened this issue Feb 25, 2023 · 0 comments
Open

JSONAPISource Serialization Problem #987

gang-qiu opened this issue Feb 25, 2023 · 0 comments

Comments

@gang-qiu
Copy link

gang-qiu commented Feb 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant