Giter Site home page Giter Site logo

Comments (7)

bkoelman avatar bkoelman commented on June 3, 2024 1

This can be done using a resource definition. See json-api-dotnet/JsonApiDotNetCore.MongoDb#54 for an example. The second commit backports to the version you're using.

from jsonapidotnetcore.

bkoelman avatar bkoelman commented on June 3, 2024 1

There's no looping in the code. You can ensure fields are always returned using the .Including method, which works similarly.

More importantly, JSON:API is loved by clients because it allows them to specify exactly what data to fetch and return. A server that is sending fields the client never asked for negates that optimized network bandwidth experience (between client and server, as well as between server and database). A better approach would be to break down your data structure into smaller parts, so you won't need to include/exclude so many fields.

from jsonapidotnetcore.

patcharees avatar patcharees commented on June 3, 2024

Iā€™m newbie.
I have a lot of fields to exclude/include. Is there a better way to implement this than looping one by one?

public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
    {
        if (!_queryStringAccessor.Query.ContainsKey("fields[books]"))
        {
            return existingSparseFieldSet
                .Excluding<Book>(book => book.Refs1, ResourceGraph)
                .Excluding<Book>(book => book.Refs2, ResourceGraph)
                .Excluding<Book>(book => book.Refs3, ResourceGraph);
        }

        return existingSparseFieldSet;
    }

from jsonapidotnetcore.

patcharees avatar patcharees commented on June 3, 2024

I got it. Thanks :)

from jsonapidotnetcore.

patcharees avatar patcharees commented on June 3, 2024

I created the following method to

  1. return all DefaultAttributes (primitive & string)
  2. return all DefaultAttributes + specific fields by fields[]

The first case works well. But the 2nd returns only the specific fields, even though I add more attributes into existingSparseFieldSet. Do you know why?

public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
{
    //var resourceContext = ResourceGraph.GetResourceContext<TResource>();
    // set default attribute first
    var attributes = _resourceGraph.GetResourceContext<TResource>().Attributes;
    var fields = "fields[" + _resourceGraph.GetResourceContext<TResource>().PublicName + "]";

    var defaultAttributes = (IReadOnlyCollection<ResourceFieldAttribute>)
        attributes.Where(a => PageConfigurableDefinition<TResource>.IsDefaultAttribute(a)).ToList();

    if (_queryStringAccessor.Query.ContainsKey(fields) && existingSparseFieldSet != null) 
    {
        HashSet<ResourceFieldAttribute> fieldSet = existingSparseFieldSet.Fields.ToHashSet();
        foreach (var attr in defaultAttributes)
            fieldSet.Add(attr);
        return new SparseFieldSetExpression(fieldSet);
    }
    else
    {
        return new SparseFieldSetExpression(defaultAttributes);
    }
}

from jsonapidotnetcore.

patcharees avatar patcharees commented on June 3, 2024

This does not work

    public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
    {
        return existingSparseFieldSet
                .Including<Book>(t => t.bookId, ResourceGraph)
                .Including<Book>(t => t.bookGroup, ResourceGraph)
                .Including<Book>(t => t.bookGroupName, ResourceGraph);
    }

from jsonapidotnetcore.

bkoelman avatar bkoelman commented on June 3, 2024

You're right, using .Including() doesn't add extra fields to the JSON response. I assumed it would, but that's not the case. It is actually by design and documented at https://www.jsonapi.net/api/JsonApiDotNetCore.Resources.IResourceDefinition-2.html#JsonApiDotNetCore_Resources_IResourceDefinition_2_OnApplySparseFieldSet_JsonApiDotNetCore_Queries_Expressions_SparseFieldSetExpression__remarks:

Including extra fields from this method will retrieve them, but not include them in the json output. This enables you to expose calculated properties whose value depends on a field that is not in the sparse fieldset.

So in that case, what you want is not possible. You'll just need to send the desired fields in the query string.

from jsonapidotnetcore.

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.