如何为使用Graphql.NET设计graphql端点的graphql模型类建模?

时间:2018-11-01 20:40:15

标签: c# rest asp.net-web-api2 graphql asp.net-core-2.0

我有一个使用ASP.NET Web API2(.Net Framework 4.6.2)和以下代码库开发的现有REST API:

// CountriesController.cs
public class CountriesController: BaseApiController {
 private readonly ICountryService _countryService;
 private readonly ICommonService _commonService;
 private readonly IImageService _imageService;

 public CountriesController(ICountryService countryService, ICommonService commonService, IImageService imageService) {
  countryService = countryService;
  commonService = commonService;
  imageService = imageService;
 }

 // GET api/<controller>
 [Route("countries")]
 public IHttpActionResult Get() {
  var resp = _countryService.GetAllCountries(locale);
  return Ok(resp);
 }
}
// CountryService.cs
public class CountryService: ICountryService {
 private readonly ICommonService _commonService;

 public CountryService(ICommonService commonService) {
  _commonService = commonService;
 }

 public CountryResult GetAllCountries(string locale) {
  CountryResult result = new CountryResult();

  // Code to return the countryresult object
  return result;
 }
}

// Models
public class CountryResult: APIResult {
 public CountryResult() {
  Countries = new List < CountryDTO > ();
 }
 public List < CountryDTO > Countries {
  get;
  set;
 }
}


public class CountryDTO {
 [JsonProperty("pngimagePath")]
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string PNGImagePath {
  get;
  set;
 }
 [JsonProperty("svgimagePath")]
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string SVGImagePath {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string DisplayName {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string DisplayNameShort {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string ProviderName {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string ProviderTerms {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string Uuid {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string Name {
  get;
  set;
 }
 [RegularExpression(Constants.GeneralStringRegularExpression)]
 public string Path {
  get;
  set;
 }
 public bool CompleteResponse {
  get;
  set;
 }

}

public class APIResult {
 public Locale Locale {
  get;
  set;
 }
 [JsonProperty("authorised")]
 public bool Authorized {
  get;
  set;
 }
 public string UserMessage {
  get;
  set;
 }
}

public class Locale {
 public string LocalisationIdentifier {
  get;
  set;
 }
 public bool Enabled {
  get;
  set;
 }
 public string Language {
  get;
  set;
 }
 public string country {
  get;
  set;
 }
 public string Description {
  get;
  set;
 }
 public string Uuid {
  get;
  set;
 }
 public string Name {
  get;
  set;
 }
 public string Path {
  get;
  set;
 }
 public bool RightToLeft {
  get;
  set;
 }
 public bool CompleteResponse {
  get;
  set;
 }

 public string ScriptDirection {
  get {
   return RightToLeft ? "rtl" : "ltr";
  }
 }

 public Locale() {}
}

REST API响应: api / tax / v1 /国家/地区

{
  "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
    }
  ],
  "locale": {
    "localisationIdentifier": "en",
    "enabled": true,
    "language": "en",
    "country": "",
    "description": "English",
    "uuid": "37",
    "name": "English",
    "path": "/locales/en",
    "rightToLeft": false,
    "completeResponse": true,
    "scriptDirection": "ltr"
  },
  "authorised": false,
  "userMessage": ""
}

我要基于上述设置设计graphql类型所需的模型,并使grahpql端点的响应与现有REST API响应相同。

有人可以帮助我提供他们的意见,以便针对这种情况更好地设计graphql模型吗?

0 个答案:

没有答案