Adam Rackis
Adam Rackis

Reputation: 83358

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

Upvotes: 23

Views: 6850

Answers (1)

ericArbour
ericArbour

Reputation: 703

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

Upvotes: 18

Related Questions