study - GraphQL

Official website

Schema

  • Schema defines the field type, data structure, and describes the rules of interface data request
  • Schema uses a simple strongly typed schema grammar called Schema Definition Language (SDL) ⬇️

SDL(Schema Definition Language)

  • GraphQL has its own language to write GraphQL schemas: GraphQL Schema Definition Language (SDL)
  • SDL is simple and intuitive to use, yet powerful and expressive

What is GraphQL schema definition ❓

  • GraphQL schema definition refers to GraphQL模式the most concise method
  • The grammar is clearly defined and is part of the official GraphQL specification
  • Schema definition is sometimes called IDL (Interface Definition Language) or SDL (Architecture Definition Language)

The main components of the pattern definition ⬇️

  • Type - 类型

    A type has a name and can implement one or more interfaces (type has a name and can implement one or more interfaces)

    type Post implements Item {
      # ...
    }
    
  • Field - 字段

    A field has a name and a type

    • Basic field

      age: Int
      
    • Fields that cannot be empty are indicated by an exclamation mark ⬇️

      age: Int!
      
    • The list is indicated by square brackets

      names: [String!]
      

Type

  • Scalar

    Scalar is a type that resolves to a single scalar object, and can no longer be secondary selection (without subfields, not split)
    • Default scalar type
      • Int
      • Float
      • String
      • Boolean
      • ID
    • Custom scalar type
      • For example: Define a Url type:, scalar Urland then define how to serialize, deserialize and verify it in the implementation.
  • Object

    basic components in the Schema object, Schema Most types are object types. Object consists of subfields
type Story {
    id: ID!
    author: user
    comments(after: ID, limit: Int = 5): [Comment]
}

Description⬇️

  • id, author, and comments are the fields of the Story object. This means that in the operation of the Story object, only these three fields can appear at most

  • ID! indicates that this field is non-empty, and the GraphQL service will ensure that as long as you query this field, you will receive the value

  • [Comment] Indicates that this field is a list composed of Comments

The basic implementation process of the application code of the GraphQL server ⬇️

  • Define the Schema (three root types are allowed in each Schema: query, mutation, subscription, at least query is required)

    schema {     //表示这是一个GraphQL schema定义
     query: Query    //定义查询操作,必须有
     mutation: Mutation    //定义变更操作,可以省略
     subscription: Subscription   //定义订阅操作,可以省略
    }
    
  • Define the root type (the name is arbitrary, the same as in the Schema)

    type Query {
        user(id: ID): User
        viewer: User
        stories(after: ID, limit: Int = 10): [Story]!
    }
    
    type Mutation {
        ...// 暂时省略
    }
    
    type Subscription{
        ...// 暂时省略
    }
    
  • Define user-defined types (each field of the type must be defined, and ultimately are the types defined in GraphQL)

    interface Human {
     id: ID!
     name: String
     age: Int
    }
    
    scalar Url //标量类型
    
    type User implements Human { 
     id: ID!
     name: String
     age: Int
     is_active: Boolean
     friends: [User]!
     website: Url
     lastStoryPosted: Story
    }
    
    type Story {
        id: ID!
        author: user
        comments(after: ID, limit: Int = 5): [Comment]
    }
    

Every time the GraphQL service is called, you need to:

  • Explicitly specify which root type in the Schema is called (the default is query)
  • Then specify which fields under this root type (each field corresponds to a user-defined type)
  • Then specify which of the subfields in these fields. Until all fields have no subfields

Guess you like

Origin blog.csdn.net/LLLLLLLLLLe/article/details/102875013