rbi-learning / Today-I-Learned

1 stars 0 forks source link

08/18 Day 17 Lecture Notes #150

Open Limlight86 opened 4 years ago

Limlight86 commented 4 years ago

08/18 Day 17 Lecture Notes

Sanity.io

Content Management System CMS - store data, be able to access on another site. Similar to a backend. Useful for non developers to update apps without the use of developers.

Getting started :

yarn global add @sanity/cli
sanity init

Sanity init inside the existing project, will add a folder and setup a place for us to store our CMS. Similar to doing Yarn init.

Create new project > name > public(easier to expose without auth) > agree to path (good idea to rename the folder to sanity) > no project template (clean).

Schema - what is the shape of the data in a data store.

sanity start - will serve a UI at localhost:3333, the content studio. This is where you can add content to it.

In an empty project, you need to define the schema. Inside the sanity folder, in the schema file is where we o that.

https://www.sanity.io/guides/introduction-to-content-modeling

import createSchema from 'part:@sanity/base/schema-creator'

// Then import schema types from any plugins that might expose them
import schemaTypes from 'all:part:@sanity/base/schema-type'

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({

  // We name our schema
  name: 'mySchema',

  // Then proceed to concatenate our document types (just one, for now)
  // to the ones provided by any plugins that are installed
  types: schemaTypes.concat([
    {
      // This is the display name for the type
      title: "Person",

      // The identifier for this document type used in the api's
      name: "person",

      // Documents have the type 'document'. Your schema may describe types beyond documents
      // but let's get back to that later.
      type: "document",

      // Now we proceed to list the fields of our document
      fields: [
        // This document has only one field
        {
          // The display name for this field
          title: "Name",

          // The identifier for this field used in the api's
          name: "name",

          // The type of this field
          type: "string",
        }
      ]
    }
  ])
})

Fields are where you populate the key/value pairs when the document is retrieved. name is how it appears in the key/value pair. type is the kind of file or data type that will be saved, from strings to images and more.

https://www.sanity.io/docs/schema-types

Sanity exposes a graphql api. Enable by doing sanity graphql deploy enable the playground. You now have an endpoint url for hitting the graphql api.