如何使用 Apollo 为 API 返回的对象列表制作 GraphQL 解析器?
How to make GraphQL resolver for list of objects returned by API using Apollo?
我是 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.What我不见了?
我已经尝试了另一个 API 并且它使用相同的代码运行良好,只是更改了架构相关的东西。尽管我的 .net 核心 Web API 返回了与上述相同的数据,但为了使我的解析器正常工作,我做了两件事
我从 API Get 方法返回了以下内容:
var myJObject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Applicant>>(json);
return Ok(myJObject);
在架构中,我将 "Applicant" 属性修改为驼峰式大小写。正如我所见,我的 API 正在返回未映射到我的 "Applicant" 架构
的驼峰式属性
我为此花了几个小时,所以我发现它值得分享。
我是 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.What我不见了?
我已经尝试了另一个 API 并且它使用相同的代码运行良好,只是更改了架构相关的东西。尽管我的 .net 核心 Web API 返回了与上述相同的数据,但为了使我的解析器正常工作,我做了两件事
我从 API Get 方法返回了以下内容:
var myJObject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Applicant>>(json); return Ok(myJObject);
在架构中,我将 "Applicant" 属性修改为驼峰式大小写。正如我所见,我的 API 正在返回未映射到我的 "Applicant" 架构
的驼峰式属性
我为此花了几个小时,所以我发现它值得分享。