loading...
Cover image for redwoodJS schema definition languages

redwoodJS schema definition languages

ajcwebdev profile image anthonyCampolo Updated on ・2 min read

GraphQL has its own type system that’s used to define the schema of an API. The syntax for writing schemas is called the Schema Definition Language or SDL. It's a very abstract concept, but once I had seen a bunch of them they became very readable.

These are taken from the RedwoodJS example apps:

Todos Schema

export const schema = gql`
  type Todo {
    id: Int!
    body: String!
    status: String!
  }

  type Query {
    todos: [Todo]
  }

  type Mutation {
    createTodo(body: String!): Todo
    updateTodoStatus(id: Int!, status: String!): Todo
    renameTodo(id: Int!, body: String!): Todo
  }
`
Enter fullscreen mode Exit fullscreen mode

Blog Posts Schema

export const schema = gql`
  type Post {
    id: ID!
    title: String!
    slug: String!
    body: String!
    author: String!
    image: String
    postedAt: DateTime
    tags: [Tag]
  }

  type PostsSet {
    posts: [Post]!
    count: Int!
  }

  type Query {
    allPosts(page: Int, limit: Int): PostsSet
    findPostById(id: ID): Post
    findPostBySlug(slug: String): Post
    findPostsByTag(tag: String): [Post]
    searchPosts(term: String): [Post]
  }

  input PostInput {
    title: String!
    slug: String!
    author: String!
    body: String!
    image: String
    postedAt: DateTime
  }

  type Mutation {
    createPost(input: PostInput!): Post
    updatePost(id: ID!, input: PostInput!): Post
    hidePost(id: ID!): Post
    deletePost(id: ID!): Post
  }
`
Enter fullscreen mode Exit fullscreen mode

Blog Tags Schema

export const schema = gql`
  type Tag {
    id: ID!
    name: String
  }

  type Query {
    tags: [Tag]
  }
`
Enter fullscreen mode Exit fullscreen mode

Invoice Schema

export const schema = gql`
  type Invoice {
    id: Int!
    body: String!
    date: String!
    invoiceNumber: String!
    createdAt: DateTime!
    updatedAt: DateTime!
  }

  type Query {
    invoice(id: Int): Invoice
  }

  input InvoiceInput {
    id: Int
    date: String!
    invoiceNumber: String!
    body: String!
  }

  type Mutation {
    setInvoice(input: InvoiceInput!): Invoice
  }
`
Enter fullscreen mode Exit fullscreen mode

Users Schema

export const schema = gql`
  type User {
    id: Int!
    email: String!
    name: String
  }

  type Query {
    users: [User!]!
  }

  input CreateUserInput {
    email: String!
    name: String
  }

  input UpdateUserInput {
    email: String
    name: String
  }
`
Enter fullscreen mode Exit fullscreen mode

Posted on by:

Discussion

pic
Editor guide