CMeeg / gridsome-source-kentico-kontent

Kentico Kontent data source plugin for Gridsome.
MIT License
6 stars 4 forks source link

Asset related issues #11

Closed ChristopherJennings closed 4 years ago

ChristopherJennings commented 4 years ago

I found that when I try to connect to a project with nothing published (I forgot that this was the case) I got this error:

Gridsome v0.7.11

Initializing plugins...
Load sources - 1.83s
Error: Cannot find ObjectTypeComposer with name Asset
    at SchemaComposer.getOTC (S:\Source\FCM\fcm-gridsome\node_modules\graphql-compose\lib\SchemaComposer.js:465:11)
    at resolvers.forEach.typeMap (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:395:43)
    at Array.forEach (<anonymous>)
    at addResolvers (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:392:13)
    at createSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:63:3)
    at Schema.buildSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\app\Schema.js:28:28)
    at Plugins.createSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\app\Plugins.js:94:22)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Once I realized that I had nothing published, I went ahead and published a bunch of things. This got rid of the above error, but then I got this error:

Gridsome v0.7.11

Initializing plugins...
Load sources - 2s
Error: Resolver for Asset.url must have a "type" property.
    at resolvers.forEach.typeMap (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:420:19)
    at Array.forEach (<anonymous>)
    at addResolvers (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:392:13)
    at createSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\graphql\createSchema.js:63:3)
    at Schema.buildSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\app\Schema.js:28:28)
    at Plugins.createSchema (S:\Source\FCM\fcm-gridsome\node_modules\gridsome\lib\app\Plugins.js:94:22)
    at process._tickCallback (internal/process/next_tick.js:68:7)

This turned out to be because my types didn't have any asset fields. Once I added an asset field the above was resolved. I think it got this far because I had referenced assets in rich text fields. If I didn't have those I think I would have seen the first error again.

As near as I can tell this has to do with how the Asset resolver is loaded. I think it works fine when an Asset type resolver is added because of an asset field, but if there isn't one because there's no field or asset referenced it throws an error.

I'm still super new to Gridsome plugin development, but if you could point me in the right direction as to how to develop this and some resources to start figuring out how to resolve the asset issues, I'd be happy to give it a shot.

CMeeg commented 4 years ago

Hi @ChristopherJennings I haven't had a chance to look into this in much detail unfortunately to try to repro it, but from the description of the issues you have had I think there are two things that can be looked at. I suspect the root cause is the same thing though, which is to do with the how the GraphQL schema definition for Kontent types is being created.

  1. Currently, the plugin adds collections, nodes and references to the GraphQL data layer using the data store API and Gridsome infers the types that need to be added to the GraphQL schema based on the shape of the nodes. This is why you get build errors if the plugin doesn't attempt to add any data (e.g. when there is no published content) as the schema will be undefined.

The plugin also uses the schema API to add a schema resolver to the Asset type. Without any asset fields being defined there will be no asset nodes added to the data layer and no asset type in the schema. The plugin attempts to add the schema resolver without checking that the Asset type exists first so I suspect that is the cause of the issue.

  1. Following on from the above, the schema API was added in Gridsome 0.7, which was later than when I was doing the initial development on this plugin, which was all on 0.6. Adding schema resolvers used to be a part of the data store API, but apart from that you had little direct control over the schema itself. That has now changed with the schema API and I think a better, long-term fix would be to modify the plugin to make more extensive use of the schema API. Instead of inferring the schema it would be possible to define the schema based on the content types of your Kontent project, and by directly defining types for things like assets and taxonomy.

I started looking into this a little while back, but realised that it will be more work than I had originally thought so plan on coming back to that once I have some time to properly think about it and dedicate some time.

I don't currently have an ETA on any of this stuff, but hoping I may get some downtime during the holidays to at least kick things off. In the meantime if you do want to get involved I am definitely open to help! Perhaps looking at route 1 may be a good starting point. You can use the data store API to check if a type (collection) exists.

In terms of developing on the plugin, this is what I would do (based on my current setup):

Tbh, I am fairly new to Gridsome plugin development too! The setup works pretty well (I really like yarn link), but debugging can be a bit of a pain. I still need to look into getting the vscode debugger involved (Ondrej was showing me how he did that on the Gatsby plugin) and sometimes I have to resort to console.log. I am also going to be adding Jest tests when I do the schema work - I wish I had done that from the start!

Hope that helps, and thanks for getting in touch - let me know how you get on, and if you have no luck I will address this issue as soon as I can.

CMeeg commented 4 years ago

A fix for both of the errors reported has been included in 0.4.1.