首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GraphQL.NET为graphql查询响应生成动态路径值?

如何使用GraphQL.NET为graphql查询响应生成动态路径值?
EN

Stack Overflow用户
提问于 2018-11-27 18:53:28
回答 1查看 379关注 0票数 0

我有REST (api/tax/v1/countries)和下面的响应。在下面的代码中,pngimagePath和svgimagePath属性指向图像类型pngimagePath或FlagSVG})

在这种情况下,路径是动态生成的。

代码语言:javascript
复制
{
  "countries": [
    {
      "pngimagePath": "https://test.com/api/tax/v1/country/Country1/4/image/FlagPNG",
      "svgimagePath": "https://test.com/api/tax/v1/country/Country1/405/image/FlagSVG",
      "displayName": "Country1",
      "displayNameShort": "Country1",
      "providerName": "Testing",
      "providerTerms": null,
      "uuid": "1",
      "name": "Country1",
      "path": "Country1",
      "completeResponse": true
    },
    {
      "pngimagePath": "https://test.com/api/tax/v1/country/Country2/5/image/FlagPNG",
      "svgimagePath": "https://test.com/api/tax/v1/country/Country2/406/image/FlagSVG",
      "displayName": "Country2",
      "displayNameShort": "Country2",
      "providerName": "Testing one",
      "providerTerms": null,
      "uuid": "2",
      "name": "Country2",
      "path": "Country2",
      "completeResponse": true
    }
  ],  
  "authorised": false,
  "userMessage": ""
}



 // Code to generate the image path
var apiPath = _appSettings.Value.ApiPath + "country/";
result.Countries.AddRange(rawCountries.Select(country  =>  new DTO.CountryDTO {
PNGImagePath =  $"{apiPath}{Helper.ReplaceChars(country.DefaultDisplayName)}/{country.PngImageId}/image/{country.PngImageType}" ,  SVGImagePath =  $"{apiPath}{Helper.ReplaceChars(country.DefaultDisplayName)}/{country.SvgImageId}/image/{country.SvgImageType}" ,  } ) ) ; 

我希望使用GraphQL.NET生成图像路径。

有人能帮助我知道如何实现这个特性吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-28 07:33:16

对于基于pngimagePath生成的country,可以使用resolve定义一个新字段以生成pngimagePath的值。

代码语言:javascript
复制
    public class PlayerType : ObjectGraphType<Player>
{
    public PlayerType(ISkaterStatisticRepository skaterStatisticRepository)
    {
        Field(x => x.Id);
        Field(x => x.Name, true);
        Field(x => x.BirthPlace);
        Field(x => x.Height);
        Field(x => x.WeightLbs);


        Field<StringGraphType>("pngimagePath", resolve: context => $"{context.Source.Name} {context.Source.BirthDate} {context.Source.BirthPlace}");
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53506345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档