GraphQL Schema and type specification

Schema is used to define the data structure, for example, what attributes are in the User object, and what is the relationship between objects and so on.

Refer to the official website document: http://graphql.cn/learn/schema/

Schema definition structure

schema { #定义查询 
    query: UserQuery 
}
type UserQuery { #定义查询的类型 
    user(id:ID) : User #指定对象以及参数类型 
}
type User { #定义对象 
    id:ID! # !表示该属性是非空项 
    name:String 
    age:Int 
} 

Scalar Types

In the GraphQL specification of enumerated types, 5 types are defined by default:

  • Int: Signed 32-bit integer.
  • Float: Signed double-precision floating-point value.
  • String: UTF-8 character sequence.
  • Boolean: true or false. 
  • ID: ID scalar type represents a unique identifier, usually used to retrieve the object or as a key in the cache.

The five types defined in the specification obviously cannot meet the needs, so in various language implementations, the types have been expanded, that is, GraphQL supports custom types, for example, the graphql-java implementation adds: Long, Byte, etc.

Enumeration type
Enumeration type is a special scalar, which is restricted to a special set of optional values.

enum Episode { #定义枚举 
    NEWHOPE 
    EMPIRE 
    JEDI 
}
type Human { 
    id: ID! # !表示该属性是非空项 
    name: String! # !表示该属性是非空项 
    appearsIn: [Episode]! #使用枚举类型 
    homePlanet: String 
} 

 

Interface (interface)
Like many type systems, GraphQL supports interfaces. An interface is an abstract type, which contains certain fields, and the object type must contain these fields in order to be considered to implement this interface.

For example, you can use a Character interface to represent any character in the "Star Wars" trilogy:

interface Character { #定义接口 
    id: ID! # !表示该属性是非空项 
    name: String! # !表示该属性是非空项 
    friends: [Character] 
    appearsIn: [Episode]! 
}
#实现接口 
type Human implements Character { 
    id: ID! 
    name: String! 
    friends: [Character] 
    appearsIn: [Episode]! 
    starships: [Starship] 
    totalCredits: Int 
}
type Droid implements Character { 
    id: ID! 
    name: String! 
    friends: [Character] 
    appearsIn: [Episode]! 
    primaryFunction: String 
} 

 

Guess you like

Origin blog.csdn.net/qq_26896085/article/details/104832969