GraphQL 新手教程 - 速查表
基本输入
输入默认值
带多个参数的输入
输入包含多个参数和默认值
工会
介绍
我是GraphQL 可视化编辑器的创始人之一。这篇博文是面向新手的教程系列的一部分。关注我,获取更多内容并查看其他文章。我已经介绍了 GraphQL 的基础知识、简介和模式定义语言。欢迎评论和提出修改建议。
类型定义
scalar > Scalar type
type > Object type
interface > Interface type
union > Union type
enum > Enumerable type
input > Input object type
标量类型
String - set of characters in UTF-8 format,
Int - 32-bit integer,
Float - floating point number,
Boolean - value true or false
ID - a type representing the unique identifier for the object.
类型修饰符
String > Nullable string
String! > Required string
[String] > List of strings
[String]! > Required list of strings
[String!]! > Required list of required strings
GraphQL模式示例
type Author {
id: Int!
firstName: String
lastName: String
"""
the list of Books by this author
"""
books: [Book]
}
type Book {
id: Int!
title: String
author: Author
pages: Int
}
在我们的GraphQL 可视化编辑器中探索此示例
此模式允许以下查询:
type Query {
book: [Book]
author(id: Int!): Author
}
输入参数
基本输入
type Root {
users(limit: Int): [User]!
}
输入默认值
type Root {
users(limit: Int = 10): [User]!
}
带多个参数的输入
type Root {
users(limit: Int, sort: String): [User]!
}
输入包含多个参数和默认值
type Root {
users(limit: Int = 10, sort: String): [User]!
}
或者
type Root {
users(limit: Int, sort: String = "asc" ): [User]!
}
接口
interface Publication {
title: String!
releasedDate: String!
}
type Magazine implements Publication {
title: String!
releasedDate: String!
version: Int!
}
type Book implements Publication {
title: String!
releasedDate: String!
pages: Int!
}
工会
union SearchResult = Book | Author
type Query {
search(text: String!): SearchResult
}
query {
search(text: "Park") {
... on Book {
title
}
... on Author {
name
}
}
}
枚举
enum RGB {
RED
GREEN
BLUE
}
type Root {
color: RGB
}
输入对象类型
input ListUsersInput {
limit: Int
since_id: ID
}
type Root {
users(params: ListUsersInput): [Users]!
}
如果您正在寻找最佳的GraphQL教程,请查看这篇文章。
文章来源:https://dev.to/iamrobert/graphql-tutorial-for-newbies---cheatsheet-gb5



