Code Monkey home page Code Monkey logo

Comments (7)

nikcio avatar nikcio commented on June 27, 2024

Hi @chris-evansnz,

In this case, you want to extend the BasicBlockListItem and use this instead of the BasicBlockListItem that is used in the documentation.

image

The BasicBlockListModel takes a type of BasicBlockListItem as the generic parameter so you just need to have another class inherit from the BasicBlockListItem eg.

public class CustomBlockListItem : BasicBlockListItem<BasicProperty> {

    public string MyCustomProperty { get; set; }

    public CustomBlockListItem (CreateBlockListItem createBlockListItem, IPropertyFactory<BasicProperty> propertyFactory) {
        MyCustomProperty = "Hello I'm Custom!";
    }
}
public class CustomBlockListModel : BasicBlockListModel<CustomBlockListItem> {

    public string MyCustomProperty { get; set; }

    public CustomBlockListModel(CreatePropertyValue createPropertyValue, IDependencyReflectorFactory dependencyReflectorFactory) : base(createPropertyValue, dependencyReflectorFactory) {
        MyCustomProperty = "Hello I'm Custom!";
    }
}

from nikcio.uheadless.

nikcio avatar nikcio commented on June 27, 2024

@chris-evansnz Did this answer your question?

from nikcio.uheadless.

chris-evansnz avatar chris-evansnz commented on June 27, 2024

@nikcio thanks so much for the quick response! I live in New Zealand so my time zone is a bit odd - hence the slower reply.

I figured something like this was required. I just tried implementing that, and the code compiles ok but I'm not sure how to query my new property.

I've got this:

public class CustomBlockListModel : BasicBlockListModel<BasicBlockListItem<BasicProperty>>
{

    public string MyCustomProperty { get; set; }

    public CustomBlockListModel(CreatePropertyValue createPropertyValue, IDependencyReflectorFactory dependencyReflectorFactory) : base(createPropertyValue, dependencyReflectorFactory)
    {
 
        MyCustomProperty = "Hello I'm Custom at parent!";
        
    }
}

public class CustomBlockListItem : BasicBlockListItem<BasicProperty>
{

    public string MyCustomProperty2 { get; set; }

    public CustomBlockListItem(CreateBlockListItem createBlockListItem, IPropertyFactory<BasicProperty> propertyFactory) : base(createBlockListItem, propertyFactory)
    {
        MyCustomProperty2 = "Hello I'm Custom in block item!2";
    }
}

And I'm running this graphql query on a page I have called Contact Us:

{
  contentByAbsoluteRoute (route: "/contact-us") {      
    name,
    id,     
    properties {
      alias,
      value,
      editorAlias,
      __typename      
    }
  }
}

I get this response, which shows the first custom property on the parent Block List, but not the second custom property on the block children:

{
  "data": {
    "contentByAbsoluteRoute": {
      "name": "Contact us",
      "id": 1070,
      "properties": [
        {
          "alias": "contentBlocks",
          "value": {
            "myCustomProperty": "Hello I'm Custom at parent!",
            "blocks": [
              {
                "contentProperties": [
                  {
                    "alias": "title",
                    "value": {
                      "value": "My test feature"
                    },
                    "editorAlias": "Umbraco.TextBox"
                  },
                  {
                    "alias": "text",
                    "value": {
                      "value": "<p>This is some text for the feature</p>"
                    },
                    "editorAlias": "Umbraco.TinyMCE"
                  },
                  {
                    "alias": "image",
                    "value": {
                      "mediaItems": [
                        {
                          "url": "https://localhost:44387/media/ivmfpss5/90mmplusnailerpluswebplusp1.png",
                          "id": 1069
                        }
                      ]
                    },
                    "editorAlias": "Umbraco.MediaPicker3"
                  },
                  {
                    "alias": "showImageOnRight",
                    "value": {
                      "value": false
                    },
                    "editorAlias": "Umbraco.TrueFalse"
                  }
                ],
                "settingsProperties": [],
                "contentAlias": "featureBlock",
                "settingsAlias": null
              },
              {
                "contentProperties": [
                  {
                    "alias": "title",
                    "value": {
                      "value": "Second feature"
                    },
                    "editorAlias": "Umbraco.TextBox"
                  },
                  {
                    "alias": "text",
                    "value": {
                      "value": "<p>Some text for the second feature</p>"
                    },
                    "editorAlias": "Umbraco.TinyMCE"
                  },
                  {
                    "alias": "image",
                    "value": {
                      "mediaItems": [
                        {
                          "url": "https://localhost:44387/media/b1sgsw1d/_0002_layer-30.png",
                          "id": 1072
                        }
                      ]
                    },
                    "editorAlias": "Umbraco.MediaPicker3"
                  },
                  {
                    "alias": "showImageOnRight",
                    "value": {
                      "value": true
                    },
                    "editorAlias": "Umbraco.TrueFalse"
                  }
                ],
                "settingsProperties": [],
                "contentAlias": "featureBlock",
                "settingsAlias": null
              }
            ]
          },
          "editorAlias": "Umbraco.BlockList",
          "__typename": "BasicProperty"
        }
      ]
    }
  }
}

Is there something else I need to do in code to expose this new custom property on the children, or is there something I need to add in the graphql query? I couldn't see the MyCustomProperty2 in the playground schema docs.

If I try to query it explicity I get this response in the graphql playground:

"The field `myCustomProperty2` does not exist on the type `BasicProperty`.",

Thanks again for your help!

from nikcio.uheadless.

nikcio avatar nikcio commented on June 27, 2024

@chris-evansnz you need to change this line:

public class CustomBlockListModel : BasicBlockListModel<BasicBlockListItem<BasicProperty>>

To this

public class CustomBlockListModel : BasicBlockListModel<CustomBlockListItem<BasicProperty>> 

Otherwise you don't use your custom implementation of the block list item.

About the properties not being available in the GraphQL playground. This will be fixed in version 3. Right now it's just a normal JSON text that's put into the property hence why you can't specifically query the properties value.

from nikcio.uheadless.

chris-evansnz avatar chris-evansnz commented on June 27, 2024

Hmm - when I try that I get this compilation error:

image

is there something else I need to include or declare?

from nikcio.uheadless.

nikcio avatar nikcio commented on June 27, 2024

Oh sorry @chris-evansnz. It needs to say:

public class CustomBlockListModel : BasicBlockListModel<CustomBlockListItem> 

Your CustomBlockListItem isn't generic and therefore doesn't take an type argument eg. <BasicProperty>

from nikcio.uheadless.

chris-evansnz avatar chris-evansnz commented on June 27, 2024

Ah yes, that makes sense. I can see the property in the results now, thanks so much for your responses!

Will keep experimenting with this package and see how we can make use of it in our projects, it's very promising indeed.

from nikcio.uheadless.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.