跳至主要内容

Basic certification Notes

  • schema first design

    1. Define the schema
    2. Backend implementation on GQL Server
    3. Frontend Implementation
    • Benefit:
      • Reduce total development time
        • Frontend can work as soon as schema is defined.
        • Backend develop API based on same schema
  • Think about data before defining schema

    • In a graph, define:
      • node as a object
      • edge as a relationship between object
  • Schema

    • A contract between server and client:
      • Define what GraphQL API can and can't do
      • how clients can request or change data
      • Abstraction that hide backend implementation.
  • Schema definition language (SDL)

    • Schema is a collection of objects that contain fields.

    • Each field can be scalar or another object.

      E.g. Code challange I:

      Define a SpaceCat type with the following fields: name of type String (non null), age of type Int, and missions of type List of Mission

      type SpaceCat {
      name: String!
      age: Int
      missions: [Mission]
      }
  • Comments

    • Can be oneline "" or multiline """comment"""

    • E.g. Code challange II:

      Add a block description for the SpaceCat type (triple "double quotes") and a normal description for the name field.

      """
      This is a SpaceCat
      """
      type SpaceCat {
      "This is name"
      name: String!
      age: Int
      missions: [Mission]
      }

Define Schema

"A track is a group of Modules that teaches about a specific topic"
type Track {
id: ID!
title: String!
author: Author!
thumbnail: String
length: Int
modulesCount: Int
}

Query type

  • The fields of Query type are entry points into the rest of our schema.
  • This is top-level fields that client can query for.
type Query {
"Get tracks array for homepage grid"
tracksForHome: [Track!]!
}

Code Challenge:

  • Create a full schema with: a type Query containing a field spaceCats to fetch a List of SpaceCat. A type SpaceCat with its subfields: id of type ID!, name of type String!, age of type Int and missions of type List of Mission. Finally define the Mission type with its subfields: id of type ID!, name of type String!, and description of type String!.
type Query {
spaceCats: [SpaceCat]
}

type SpaceCat {
id: ID!
name: String!
age: Int
missions: [Mission]
}

type Mission {
id: ID!
name: String!
description: String!
}

Backend

Start server

  • Import schema and start server
    async function startApolloServer() {
    const server = new ApolloServer({ typeDefs });
    const { url } = await startStandaloneServer(server);
    }

Create Mock data