我是GraphQL的新手,想运行我的第一个应用程序。我看过几个例子,但是他们使用了几个库,我只想简单的例子。 我见过https://www.youtube.com/watch?v=hLcAlln-D_s
我的Index.js代码
const { ApolloServer, gql } = require("apollo-server");
const fetch = require("node-fetch");
const typeDefs = `
type Query {
getApplicant: [Applicant]
}
type Applicant {
FiscalYear:String
JobNumber: String
JobDescription: String
AppsReceived: Int
Female: Int
Male: Int
UnknownGender: Int
Black: Int
Hispanic: Int
Asian: Int
Caucasian: Int
AmericanIndian:Int
Filipino: Int
UnknownEthnicity: Int
} `;
const url = `http://localhost:52993/api/values`;
const resolvers = {
Query: {
getApplicant: async () => {
const response = await fetch(url).then(res => res.json());
console.log(response);
return response;
}
}
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`? Server ready at ${url}`);
});
从API返回的数据
[{ "FiscalYear": "2013-2014",
"JobNumber": "9206 OP 2014/04/18",
"JobDescription": "311 DIRECTOR 9206",
"AppsReceived": 54,
"Female": 20,
"Male": 31,
"UnknownGender": 3,
"Black": 25,
"Hispanic": 18,
"Asian": 1,
"Caucasian": 6,
"AmericanIndian": 0,
"Filipino": 0,
"UnknownEthnicity": 4
},
{
"FiscalYear": "2013-2014",
"JobNumber": "1223 P 2013/08/09",
"JobDescription": "ACCOUNTING CLERK 1223",
"AppsReceived": 648,
"Female": 488,
"Male": 152,
"UnknownGender": 8,
"Black": 151,
"Hispanic": 204,
"Asian": 123,
"Caucasian": 62,
"AmericanIndian": 3,
"Filipino": 79,
"UnknownEthnicity": 26
}]
当我查询数据时,它始终显示为null。我缺少什么?
答案 0 :(得分:0)
我尝试了另一个API,并且它与相同的代码很好地工作,只是更改了与架构相关的内容。尽管我的.net核心Web API返回的数据与上述相同,但为了使解析器正常工作,我做了两件事
我从API Get方法返回了以下内容:
var myJObject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Applicant>>(json);
return Ok(myJObject);
在架构中,我将“ Applicant”属性修改为驼峰式。如我所见,API返回的是驼峰式案例属性,这些属性未映射到我的“申请人”模式
我花了几个小时,所以我觉得值得分享。