GraphQL:如何实现GraphQLObjectTypes的GraphQLList [英] GraphQL: How to implement a GraphQLList of GraphQLObjectTypes

查看:237
本文介绍了GraphQL:如何实现GraphQLObjectTypes的GraphQLList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GraphQL上遇到问题(具体来说,实现GraphQLList)

I have an issue with GraphQL (Specifically, implementing GraphQLList)

我有一个查询getItemByName,它正确返回类型为itemTypeItem.但是我还不能实现getItemList(或者适当的示例顺便说一句).我知道它应该具有实现GraphQLList的类型.

I have a query getItemByName that correctly returns an Item, with Type itemType. But I haven't been able to implement a getItemList (Or a proper example btw). I know that it's supposed to have a type that implements GraphQLList.

但是我做对了吗? resolve应该接收什么数据格式? (或者getItems()应该返回).对象数组?这些对象是否应该实现"itemType"定义?应该实现一个接口吗?

But am i doing it right? What's the data format that resolve should recieve? (Or that getItems() should return). An array of objects? Should those objects be implementing "itemType" definition? Should an interface be implemented?

export const itemType = new GraphQLObjectType({
  name: 'Item',
  fields: {
    name: {
      type: GraphQLString,
      description: 'Item Name',
    },
  },
});

const itemListType = new GraphQLObjectType({
  name: 'ItemsList',
  fields: {
    items: {
      type: new GraphQLList(itemType),
      description: 'List of items',
    },
  },
});

{...}

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'Query',
        fields: {
           itemList: {
               type: itemListType,
               resolve: () => getItems(),
           }
        }
    })
})

现在,查询:

itemList {
  items {
    name
  }
}

其返回:

{
  "data": {
    "itemList": {
      "items": null
    }
  }
}

真的很欢迎任何帮助! :D

Any help is really really welcome! :D

致谢!

推荐答案

我将首先说明问题的根本原因.然后,我将讨论通常如何使用GraphQLList.

I'll first explain the root cause of your problem. Then I'll discuss how GraphQLList is generally used.

在架构查询中,itemList字段的类型为itemListType.现在,此itemListType具有字段items.因此,查询的itemList字段必须解析为具有items字段的对象.但是getItems()函数返回一个数组或一个承诺数组,并且没有字段items.这就是为什么您在查询响应中得到"items": null的原因.

In your schema query, itemList field is of type itemListType. Now this itemListType has a field items. Therefore, the query's itemList field must be resolved to an object which has an items field. But the getItems() function returns an array or a promised array and does not have field items. That's why you're getting "items": null in the query response.

现在关于GraphQLList的困惑:如果只是一个列表,实际上不需要为此定义单独的GraphQL类型.在您的查询中,包含一个项目类型为GraphQLList的字段:

Now about the confusion around GraphQLList: if it's just a list, there's actually no need to define a separate GraphQL type for that. In your query, you include a field with type GraphQLList of item type:

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'Query',
        fields: {
           itemList: {
               type: new GraphQLList(itemType),
               resolve: () => getItems(),
           }
        }
    })
});

如果列表不是那么简单,例如,itemList实际上有两个字段(oldItems,newItems-都是列表),则应定义一个单独的类型.记住要相应地修改resolve函数:

If your list is not that simple, for instance, itemList actually has two fields(oldItems, newItems - both are lists), you should define a separate type. Remember to modify the resolve function accordingly:

itemList: {
  type: itemListType,
  resolve: () => {
    // get new items
    // get old items
    return {
      newItems, 
      oldItems
    };
  },
},

(顺便说一下,您的代码有一些语法错误,例如itemsList,itemList,itemListType,itemListType.我已经编辑了您的问题.更改将在我的编辑获得批准后显示.)

(By the way, your code has some syntax errors like itemsList, itemList, itemsListType, itemListType. I have edited your question. The changes will appear after my edit is approved.)

这篇关于GraphQL:如何实现GraphQLObjectTypes的GraphQLList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆