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

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

问题描述

我对 GraphQL 有问题(具体来说,是实现 GraphQLList)

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

我有一个查询 getItemByName,它正确返回一个 Item,类型为 itemType.但我还没有能够实现一个 getItemList (或者一个适当的例子 btw).我知道它应该有一个实现 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 - 两者都是列表),您应该定义一个单独的类型.记得相应地修改解析函数:

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天全站免登陆