如何使用Apollo为API返回的对象列表生成GraphQL解析器?

如何使用Apollo为API返回的对象列表生成GraphQL解析器?,graphql,graphql-js,apollo-server,Graphql,Graphql Js,Apollo Server,我是GraphQL新手,想运行我的第一个应用程序。我看过几个例子,但他们使用了几个库,我只想要一个简单的例子。 我见过 我的Index.js代码 const { ApolloServer, gql } = require("apollo-server"); const fetch = require("node-fetch"); const typeDefs = ` type Query { getApplicant: [Applicant] } type Ap

我是GraphQL新手,想运行我的第一个应用程序。我看过几个例子,但他们使用了几个库,我只想要一个简单的例子。 我见过

我的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(`I have tried another API and it worked well with the same code, just changed the schema related things. Although my .net core Web API was returning same data as mentioned but to make my resolver work, I did 2 things

  1. I returned the following from the API Get method:

    var myJObject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Applicant>>(json);
    return Ok(myJObject);
    
    const{ApolloServer,gql}=require(“阿波罗服务器”);
    const fetch=require(“节点获取”);
    常量typeDefs=`
    类型查询{
    申请人:[申请人]
    }
    类型申请人{
    财政部:字符串
    作业编号:字符串
    职位描述:字符串
    接收的应用程序:Int
    女:Int
    男:Int
    未知者:Int
    黑色:Int
    西班牙裔:Int
    亚洲人:Int
    高加索人:Int
    美国印第安语:Int
    菲律宾语:Int
    未知度:Int
    } `;
    常量url=`http://localhost:52993/api/values`;    
    常量解析程序={
    查询:{
    getapplicator:async()=>{
    const response=await fetch(url).then(res=>res.json());
    控制台日志(响应);
    返回响应;
    }
    }
    };
    const server=new ApolloServer({typeDefs,resolvers});
    server.listen().then({url})=>{
    
    console.log(`我尝试了另一个API,它在相同的代码中运行良好,只是更改了与模式相关的内容。虽然我的.net core Web API返回了与前面提到的相同的数据,但为了使解析器工作,我做了两件事

  2. 我从API Get方法返回了以下内容:

    var myJObject=Newtonsoft.Json.JsonConvert.DeserializeObject(Json);
    返回Ok(myJObject);
    
  3. 在模式中,我将“申请人”属性修改为camel case。正如我看到的,我的API返回的camel case属性没有映射到我的“申请人”模式


  4. 我花了几个小时在这上面,所以我觉得它值得分享。

    尝试将查询更改为类型查询{getapplicator:[applicator]}。我在这里使用@Indragith,您需要指定查询的结果是一个数组,
    [applicator]
    我将其更改为[applicator]..,但仍然不起作用