Types

Introduction to the automatically generated GraphQL API.

Tensei automatically generates a fully featured and customisable GraphQL API for all your defined resources. This API would have queries, mutations and subscriptions for each resource.

Types

For each resource, a number of types are generated. Let's take an example Comment resource:

import { resource, text, textarea } from '@tensei/core'

resource('Comment')
    .fields([
        text('Title').sortable(),
        textarea('Content')
    ])

The following types will be generated for the Comment resource:

type comment {
  id: ID!
  title: String
  content: String
  created_at: String
  updated_at: String
}

input CommentWhereQuery {
  _and: [CommentWhereQuery]
  _or: [CommentWhereQuery]
  _not: CommentWhereQuery
  id: id_where_query
  title: StringWhereQuery
  content: StringWhereQuery
  created_at: StringWhereQuery
  updated_at: StringWhereQuery
}

input CreateCommentInput {
  title: String!
  content: String!
}

input UpdateCommentInput {
  title: String!
  content: String!
}

input StringWhereQuery {
  _eq: String
  _ne: String
  _in: [String!]
  _nin: [String!]
  _gt: String
  _gte: String
  _lt: String
  _lte: String
  _like: String
  _re: String
  _ilike: String
  _overlap: String
  _contains: String
  _contained: String
}

input CommentQueryOrder {
  id: query_order
  title: query_order
}

enum query_order {
  asc
  asc_nulls_last
  asc_nulls_first
  desc
  desc_nulls_last
  desc_nulls_first
}
  • The type CommentWhereQuery defines the where parameters accepted when fetching multiple comments.
  • The input CreateCommentInput defines the accepted fields when inserting a comment.
  • The input UpdateCommentInput defines the accepted fields when updating a comment.

Queries

For each resource, three queries are generated, one to fetch and filter all the resource records, one to count the resource records, and another to find a single resource record. Let's take an example Comment resource:

import { resource, text, textarea } from '@tensei/core'

resource('Comment')
    .fields([
        text('Title').sortable(),
        textarea('Content')
    ])

This would generate the following queries:

type Query {
  comment(id: ID!): comment
  commentsCount(where: CommentWhereQuery): Int!
  comments(offset: Int, limit: Int, where: CommentWhereQuery, order_by: CommentQueryOrder): [comment]
}
  • comment to fetch a single comment by ID.
  • commentsCount to count comments.
  • comments to fetch, filter and order all comments.

Hiding queries

Sometimes, you may not want a resource to be exposed via the API. You may use the .hideOnFetchApi() method on a resource to hide all of the queries for that resource:

resource('Comment')
  .hideOnFetchApi()

If you want to completely hide a resource from being exposed to the API, you may use the .hideOnApi() method.

resource('Comment')
  .hideOnApi()

Mutations

For each resource, six mutations are generated, one to insert, one to update, and another to delete a single resource record. Let's take an example Comment resource. The following mutations will be generated by default:

type Mutation {
  createComment(object: CreateCommentInput): comment!
  createComments(objects: [CreateCommentInput]!): [comment]!

  updateComment(id: ID!, object: UpdateCommentInput!): comment!
  update_comments(where: CommentWhereQuery!, object: [UpdateCommentInput]!): comment!

  deleteComment(id: ID!): comment
  deleteComments(where: CommentWhereQuery): [comment]!
}
  • createComment inserts a single comment.
  • createComments inserts multiple comments at a time.
  • updateComment updates a single comment by ID.
  • update_comments updates multiple comments that match a query.
  • deleteComment deletes a single comment by ID.
  • deleteComments deletes multiple comments that match a query.

Hiding mutations

If you do not want to expose inserting a specific resource via the API, you may use the .hideOnInsertApi() on the resource to automatically hide the createResource and createResources mutations from the GraphQL APi.

resource('Comment')
  .hideOnInsertApi()

You may also want to hide a specific field from the createResource field. You may do this using the .hideOnInsertApi() method on the field.

resource('Customer')
  .fields([
    text('Stripe Secret Key')
      .hideOnInsertApi()
  ])

Likewise, you may use the .hideOnUpdateApi() and .hideOnDeleteApi() to hide fields or resources from being exposed to the Graph QL API.

resource('Customer')
  .fields([
    text('Stripe Secret Key')
      .hideOnInsertApi()
  ])
  .hideOnUpdateApi()

If you want to completely hide a resource from being exposed to the API, you may use the .hideOnApi() method.

resource('Password Reset')
  .fields([
    text('Secret Token')
  ])
  .hideOnApi()

Subscriptions

For each resource, three subscriptions are generated: resource_inserted, resourceUpdated and resourceDeleted. Consider we have a Post resource:

type Subscription {
  postCreated(filter: JSONObject): post!
  postUpdated(filter: JSONObject): post!
  postDeleted(filter: JSONObject): post!
}

Showing subscriptions

By default, subscriptions are not generated for a resource. To generate the resource_inserted subscription, you may use the .showOnInsertSubscription() method on the resource:

resource('Post')
  .showOnInsertSubscription()

You may also use the .showOnUpdateSubscription() and .showOnDeleteSubscription() to expose the resourceUpdated and resourceDeleted subscriptions respectively.