简体   繁体   English

将graphQL片段添加到架构,并且可用于所有查询

[英]Add graphQL fragment to schema, and have available for all queries

The following executes correctly in graphiQL 以下在graphiQL中正确执行

fragment BookGridFields on Book {
  _id
  title
}

{
  allBooks {
    ...BookGridFields
  }
}

My question is, it possible to specify the fragment right in my schema, right below where my Book type is defined, like so 我的问题是,可以在我的架构中指定片段,就在我的Book类型定义的下方,就像这样

type Book {
  _id: String
  title: String
  pages: Int
  weight: Float
  authors: [Author]
}

fragment BookGridFields on Book {
  _id
  title
}

So that I could just run queries like this 所以我可以像这样运行查询

{
  allBooks {
    ...BookGridFields
  }
}

without needing to define the fragment as part of my query. 无需将片段定义为我的查询的一部分。

Currently the above errors with 目前上述错误有

Unknown fragment \\"BookGridFields\\" 未知片段\\“BookGridFields \\”

Per the graphql docs, I see that fragments are a part of the query api and not valid syntax for setting up a schema. 根据graphql文档,我看到片段是查询api的一部分,而不是用于设置模式的有效语法。 This leads me to conclude that it is not currently possible to specify a fragment in a schema. 这使我得出结论,目前无法在模式中指定片段。

https://graphql.org/learn/queries/#fragments https://graphql.org/learn/queries/#fragments

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

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