question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make ResourceGraphBuilder extensible

See original GitHub issue

Is your feature request related to a problem? Please describe. I need to make a resource type accessable through JADNC that is part of a third party lib. So I can’t add AttrAttribute to the properties of this type.

Describe the solution you’d like It would be nice if it was possible to extend the ResourceGraphBuilder. Maybe through JsonApiResourceDefinition? Sadly, the methods CreateResourceType and GetAttributes are private and sealed off.

Or did I miss another way to do this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bart-degreedcommented, Nov 2, 2021

Hi @Khaos66, thanks for asking.

I agree that ResourceGraphBuilder being non-sealed without any virtual members is not very useful. We could open it up for extension, but in your case, that’s not going to solve your problem for two reasons:

  • Building the graph requires to set ResourceFieldAttribute.Property, whose setter is internal.
  • Resources must implement IIdentifiable, which requires the 3rd party library to have a dependency on JsonApiDotNetCore.

The way to solve this is by wrapping the type from the 3rd party library. I’ve attached a sample project that wraps System.Version. You can run it with F5, which shows sample data.

WrappedThirdPartyResourceDemoApi.zip

For convenience, I’m adding the wrapper here as text too:

[Resource("versions")]
public sealed class VersionResource : Identifiable<string>
{
    private Version _innerVersion = new();

    public override string Id
    {
        get => _innerVersion.ToString();
        set => _innerVersion = value == null ? new Version() : Version.Parse(value);
    }

    [Attr]
    public int Major
    {
        get => _innerVersion.Major;
        set => _innerVersion = new Version(value, _innerVersion.Minor);
    }

    [Attr]
    public int Minor
    {
        get => _innerVersion.Minor;
        set => _innerVersion = new Version(_innerVersion.Major, value);
    }
}

when running the project, it requests:

GET http://localhost:24883/releases?include=version

Which returns:

{
  "links": {
    "self": "http://localhost:24883/releases?include=version",
    "first": "http://localhost:24883/releases?include=version"
  },
  "data": [
    {
      "type": "releases",
      "id": "1",
      "attributes": {
        "title": "JsonApiDotNetCore"
      },
      "relationships": {
        "version": {
          "links": {
            "self": "http://localhost:24883/releases/1/relationships/version",
            "related": "http://localhost:24883/releases/1/version"
          },
          "data": {
            "type": "versions",
            "id": "1.2"
          }
        }
      },
      "links": {
        "self": "http://localhost:24883/releases/1"
      }
    }
  ],
  "included": [
    {
      "type": "versions",
      "id": "1.2",
      "attributes": {
        "major": 1,
        "minor": 2
      },
      "links": {
        "self": "http://localhost:24883/versions/1.2"
      }
    }
  ]
}

Hope that helps.

0reactions
bart-degreedcommented, Nov 3, 2021

Glad to help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add custom data to resources using extensions
In this article, we'll discuss how Microsoft Graph supports extending its resources, the options available to add custom properties and when to ...
Read more >
The Resource Graph
The ResourceGraph is a map of all the JSON:API resources and their relationships that your API serves. It is built at app startup...
Read more >
Make Bicep extensibility production ready (phase I) #6864
Creating the issue to track the tasks for making Bicep extensibility production ready. Plan Items. Legend of annotations: Mark, Description.
Read more >
How Threat Graph Leverages DSL to Improve Data ...
In our earlier post, Making Threat Graph Extensible: Leveraging a DSL to Improve Data Ingestion (Part 1 of 2), we explored how and...
Read more >
Requirements for an extensible object-oriented tree/graph ...
Software engineers use graphs to represent many types of information. This paper describes a tool which is used to rapidly extend base classes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found