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

How to receive/convert the result to strong typed object? #1

Open
ZedZipDev opened this issue Jun 8, 2021 · 3 comments
Open

How to receive/convert the result to strong typed object? #1

ZedZipDev opened this issue Jun 8, 2021 · 3 comments

Comments

@ZedZipDev
Copy link

I have created the console client and call this code

   var client = new TheGraphClient("michielpost/graph-bookstore");

                string query = @"
                        {
                          gravatars(first: 5) {
                            id
                            owner
                            displayName
                            imageUrl
                          }
                        }
                        ";

                var result2 = await client.SendQueryAsync<Gravatars>(query);
...
    public class Gravatars
    {
        public int id {get;set;}
        public string owner { get; set; }
        public string displayName { get; set; }
        public string imageUrl { get; set; }
      
    }

The result2.Data contains object Gravatars, but all fields are emty.

How to receive or convert the result of server call to a strong typed object?

@michielpost
Copy link
Owner

The json that is returned by the query is an object that has an array with Gravatars. So you need one more class that has a property List<Gravatars>.

Should be something like this:

public class GravatarResultModel
{
  [JsonPropertyName("gravatars")]
  public List<Gravatars> Gravatars { get; set; } = new List<Gravatars>();
 }

And then use that for the Query result:

var result2 = await client.SendQueryAsync<GravatarResultModel>(query);

See this example in the unit tests:

class GravatarResultModel
{
[JsonPropertyName("gravatars")]
public List<GravatarModel> Gravatars { get; set; } = new List<GravatarModel>();
}
class GravatarModel
{
[JsonPropertyName("id")]
public string? Id { get; set; }
[JsonPropertyName("owner")]
public string? Owner { get; set; }
[JsonPropertyName("displayName")]
public string? DisplayName { get; set; }
[JsonPropertyName("imageUrl")]
public string? ImageUrl { get; set; }
}

Let me know if this works.
For what project are you using it. or going to use it? I can add a link to it! Thanks.

@ZedZipDev
Copy link
Author

Great, thank you!. Now I am learning the GraphQL.NET and later want to apply it to my service and client.
I'll let you know when it is ready.

Yet another question:
For example, I have a query as in your application, but sometimes want to filter\limit the result set using skip, take, where.
Should they be implemented on the server side (queries) or they can be used on the client side when need?

@michielpost
Copy link
Owner

It depends on your result set. If you have a small result set,you can filter it on the client side. But I think thegraph.com returns max 1000 results. So if you have more entities, you will need filter it based on a query on the server.

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

2 participants