Queries

Grafbase automatically generates queries to fetch entries by ID, unique field, or by collection based on the types you define in your schema.

Grafbase generates a query for all types in your schema tagged with the @model directive.

The generated query contains a single by argument to fetch a record by id, or by @unique field.

  • by: [modelName]ByInput! — The input [modelName]ByInput type contains id, and all other unique fields of that the @model.

The query returns the type [modelName].

type Post @model {
  title: String
  slug: String! @unique
}

Grafbase generates a query in the format of [modelName]Collection, camelCased for all types tagged with a @model directive.

The response type is in the format of [modelName]Connection.

type Post @model {
  title: String
}

Collection queries contain the following optional arguments to aid things like pagination and ordering:

  • after: String
  • before: String
  • first: Int
  • last: Int
  • orderBy: PostOrderByInput
type Post @model {
  title: String
}

If your schema contains multiple, related types, you can query for those at the same time you request the parent/child.

type Post @model {
  title: String!
  category: Category
}

type Category @model {
  name: String!
  posts: [Post]
}
Was this page helpful?