简体   繁体   中英

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

The following executes correctly in 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

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\\"

Per the graphql docs, I see that fragments are a part of the query api and not valid syntax for setting up a schema. This leads me to conclude that it is not currently possible to specify a fragment in a schema.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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