简体   繁体   English

返回自定义 object 到 GraphQL 解析器

[英]Return custom object to GraphQL resolver

I need to provide custom custom Object as ouput for GraphQL resolver.我需要提供自定义 Object 作为 GraphQL 解析器的输出。

This is for Node-Express backend server with GraphQL setup.这适用于具有 GraphQL 设置的 Node-Express 后端服务器。 I tried sequelized, but I'm testing a new approach, where i need to pass an object as output to graphQL.我尝试了续集,但我正在测试一种新方法,我需要将 object 作为 output 传递给 graphQL。

const RootQueryType = new GraphQLObjectType({
  name: "Query",
  fields: {
  Sample: {
      type: SampleType,
      args: {
        sampleID: { type: GraphQLID }
      },
      resolve: async (parent, args) => {
        try {
          const result = await sql.connect(config).then(pool =>
            pool
              .request()
              .input("sampleID", sql.Int, args.sampleID)
              .query(
                "SELECT * FROM samples WHERE sampleID = @sampleID"
              )
          );
          const ASB = await result.recordsets[0];
          console.log(ASB);
          sql.close();
          return ASB;
        } catch (err) {
          console.log(err);
          sql.close();
          return;
        }
      }
    }
  }
});

the result of the query output is查询 output 的结果是

[
  {
     sampleID: 1,
     sampleText: 'sample1'
  },
  {
     sampleID: 2,
     sampleText: 'sample2'
  }
]

When i return plain object ie ASB[0]当我返回普通 object 即 ASB[0]

{
     sampleID: 1,
     sampleText: 'sample1'
}

I am getting output in GraphQL but when the entire ASB object is returned, I'm not able to get ouput.我在 GraphQL 中得到 output 但是当返回整个 ASB object 时,我无法获得输出。

The problem is you're declaring the type as single item SampleType but returning the array of SampleType问题是您将类型声明为单项SampleType但返回SampleType数组

you can fix it be changing SampleType to new GraphQLList(SampleType)您可以通过将SampleType更改为new GraphQLList(SampleType)来修复它

const RootQueryType = new GraphQLObjectType({
  name: "Query",
  fields: {
  Sample: {
      type: new GraphQLList(SampleType), // THIS LINE !!!
      args: {
        sampleID: { type: GraphQLID }
      },
      resolve: async (parent, args) => {
        try {
          const result = await sql.connect(config).then(pool =>
            pool
              .request()
              .input("sampleID", sql.Int, args.sampleID)
              .query(
                "SELECT * FROM samples WHERE sampleID = @sampleID"
              )
          );
          const ASB = await result.recordsets[0];
          console.log(ASB);
          sql.close();
          return ASB;
        } catch (err) {
          console.log(err);
          sql.close();
          return;
        }
      }
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在GraphQL解析器中使用多个API调用来构造承诺数组,以返回单个对象类型列表 - How to architect array of promises in GraphQL resolver with multiple API calls to return a single object type list (Graphql) 解析器值不是对象类型 - (Graphql) The resolver value isn't of type object GraphQL 对解析器的返回值有何作用? - What does GraphQL do with the return value from a resolver? 返回 mysql query for graphql resolver function in node-mysql - Return mysql query for graphql resolver function in node-mysql GraphQL-subscriptions:如何在订阅解析器中获取已发布的对象 - GraphQL-subscriptions: How to get published object in subscription resolver graphql-tools突变解析器的参数在v> 0.8.0中不是对象吗? - Arguments to graphql-tools mutation resolver is not an object in v > 0.8.0? graphql 解析器返回 无法从 nodejs 中的异步 function 为非空字段返回 null - graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs 在任何父解析器中使用子 graphql 解析器 - 解析器链接 - use child graphql resolver inside any parent resolver - resolver chaining 如何在需要return语句的GraphQL解析器中调用异步node.js函数? - How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement? 用于授权检查的GraphQL解析器中间件 - GraphQL resolver middleware for auth check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM