简体   繁体   English

如何使用graphql-js定义片段?

[英]How do you define a fragment using graphql-js?

How do I define fragments in my schema using graphql-js? 如何使用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`
})

So the type definition here is generated by GraphQLObjecType . 因此,这里的类型定义是由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. 在服务器上以及在创建模式时,您不必担心它们。

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. GraphQL本身负责在查询中添加碎片字段。 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. 当然,您可以编写自己的助手来减少手工工作。

Same goes with variables too. 变量也一样。

you do not define fragment in the schema. 您没有在架构中定义片段。 you define on the graphql interface when you do query. 您在查询时在graphql接口上定义。 this is about do not repeat yourself. 这是关于不要重复自己。

We can make as many as different queries inside a query{ } 我们可以在一个query{ }进行多达不同的查询

query {
   company(id:"1"){
    name
    description
  }
  company(id:"4"){
    name
    description
  }
}

if you do so you will get an ERROR: fields “company” conflicts because they have different arguments. 如果这样做,则会出现错误:字段“ company”冲突,因为它们具有不同的参数。 To get rid of this, we can name the result of the query when it comes back by writing out some arbitrary key in front. 为了消除这种情况,我们可以通过在前面写一些任意键来命名查询返回时的结果。 the reason why we get this error is response object will have 2 nested objects and they both will have "company" key value but inside javascript we cannot have duplicate keys on an object. 我们收到此错误的原因是响应对象将具有2个嵌套对象,并且它们都将具有“ company”键值,但是在javascript中,我们不能在对象上具有重复键。

{
  apple: company(id:"1"){
    name
    description
  }
  google: company(id:"4"){
    name
    description
  }
}

now we tackled this. 现在我们解决了这个问题。 imagine we wanna do 100 queries in a single query object with too many fields. 想象一下,我们想在一个具有太多字段的查询对象中进行100个查询。 it would be a big headache. 这将是一个很大的头痛。 so we define fragment 所以我们定义片段

fragment companyDetails on Company{  
  name
  description
  }

note that we have to specify on Company . 请注意,我们必须on Company上指定。 It helps graphql for type checking. 它有助于graphql进行类型检查。 graphql checks if those fields are valid under company. graphql检查这些字段在company下是否有效。 finally this is how we use it 最终,这就是我们的使用方式

{
  apple: company(id:"1"){
    ...companyDetails
  }
  google: company(id:"4"){
    ...companyDetails
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM