因此,我从Web服务中获取了具有国家代码和描述的XML数据,然后将这些数据存储在map(key-> code,value-> descriptions)中,所以现在我应该如何编写graphql schema ..以便可以获取特定代码的描述?
XML:
<GetCountriesAvailableResult>
<CountryCode>
<Code>Canada</Code>
<Description>Canada</Description>
</CountryCode>
<CountryCode>
<Code>GreatBritain</Code>
<Description>Great Britain and Wales</Description>
</CountryCode>
<CountryCode>
<Code>IrelandNorthern</Code>
<Description>Northern Ireland</Description>
</CountryCode>
<CountryCode>
<Code>IrelandRepublicOf</Code>
<Description>Republic of Ireland</Description>
</CountryCode>
<CountryCode>
<Code>Scotland</Code>
<Description>Scotland</Description>
</CountryCode>
<CountryCode>
<Code>UnitedStates</Code>
<Description>United States</Description>
</CountryCode>
</GetCountriesAvailableResult>
//接线
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type("Query",typeWriting -> typeWriting
.dataFetcher("countryByCode", graphQLDataFetcher.getDataFromWebService())
).build();
}
//获取代码说明
String country_code = dataFetcherFactoryEnvironment.getArgument("code");
return map.entrySet()
.stream()
.filter(code ->(code.getKey()).equals(country_code))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
};
架构文件:
type Query {
countryByCode(code: ID): CountryCode
}
type CountryCode {
code: ID
descriptions: String
}
//我得到的输出
{
"data": {
"countryByCode": {
"code": null,
"descriptions": null
}
}
}