如何使用graphql-js定义片段? [英] How do you define a fragment using graphql-js?

查看:69
本文介绍了如何使用graphql-js定义片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用graphql-js在架构中定义片段?

How do I define fragments in my schema using graphql-js?

import graphql from 'graphql'
/* how do I do this?
fragment authorInfo on Author {
  name
}
*/

例如,要定义作者类型,我将:

For example, to define the Author type I would:

import graphql from 'graphql'
export default new graphql.GraphQLObjectType({
  description: `An author`,
    name: {
      description: `The author's legal name.`,
      type: GraphQLString
    }
  }),
  name: `Author`
})

因此,此处的类型定义由 GraphQLObjecType 生成.什么功能生成片段?

So the type definition here is generated by GraphQLObjecType. What function generates fragments?

推荐答案

片段是用于对字段进行分组并在客户端上重复使用它们.在服务器上以及在创建架构时,您不必担心它们.

Fragments are used to group the fields and reuse them on the client-side. They are not something you should worry about at the server and while you are creating the schema.

从服务器查询数据时,客户端代码应提供片段.GraphQL本身负责在查询中添加碎片字段.在服务器上,您需要指定所有对象上的所有字段.

The client-side code should provide the fragments when querying the data from the server. GraphQL itself takes care of adding the fragmented fields on the query. On the server, you need to specify all the fields on all the objects.

当然,您可以编写自己的助手来减少手工工作.

Of course you can write your own helpers to reduce the manual work.

变量也是如此.

这篇关于如何使用graphql-js定义片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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