javascript - GraphQL 类型 : What is the correct bucketing?

标签 javascript graphql

官方 graphql 指南和 howtographql 教程都没有指定一致的存储/类型层次结构。

来自 Official "Learn" GraphQL :

The most basic components of a GraphQL schema are object types

...

Most types in your schema will just be normal object types, but there are two types that are special within a schema: Query, Mutation. [my note: Here they forgot subscription]

...

Except for being "entry point" into schema, Query and Mutation types are the same as any other GraphQL object type, and their fields work exactly the same way

...

That's where the scalar types come in: they represent the leaves of the query

...

Enumeration types are a special kind of scalar

...

Object types, scalars, and enums are the only kinds of types you can define in GraphQL. [my note: That is not entirely correct, as enums are special scalar types]

...

An Interface is an abstract type

...

Union types are very similar to interfaces

...

Input types look exactly the same as regular object types, but with the keyword input instead of type

来自 HowToGraphQL :

In GraphQL, there are two different kinds of types.

  • Scalar types

  • Object types

...

enums are special kinds of scalar types.

...

An interface can be used to describe a type in an abstract way.

...

Union types can be used to express that a type should be either of a collection of other types.

我的最佳猜测

  • GraphQL 中仅存在两种类型:
    • 对象类型
    • 标量类型
  • 对象类型包括
    • 自定义对象类型
    • 3 种特殊对象类型:查询、变更、订阅
  • 标量类型包括
    • 5 种内置标量类型
    • 自定义标量类型
    • 枚举类型
  • 接口(interface)和联合
    • 这些不是实际类型
    • 它们是用于描述对象类型的机制
  • 输入类型
    • 再说一次,实际上不是一种类型
    • 这是一种机制,通过该机制可以将对象和标量类型用作查询中的输入

问题

是否有正确的(官方)分桶?我上面的“最佳猜测”正确吗?

最佳答案

GraphQL 有 specification详细描述了类型系统。我会仔细阅读该规范,因为它也可能回答您有关 GraphQL 的其他问题。您应该对其他网站的说法持保留态度,因为它可能是完全错误的、使用不正确的术语或过于简单化的内容(以免让刚接触 GraphQL 的读者不知所措)。

The fundamental unit of any GraphQL Schema is the type. There are six kinds of named type definitions in GraphQL, and two wrapping types.

The most basic type is a Scalar. A scalar represents a primitive value, like a string or an integer. Oftentimes, the possible responses for a scalar field are enumerable. GraphQL offers an Enum type in those cases, where the type specifies the space of valid responses.

Scalars and Enums form the leaves in response trees; the intermediate levels are Object types, which define a set of fields, where each field is another type in the system, allowing the definition of arbitrary type hierarchies.

GraphQL supports two abstract types: interfaces and unions.

An Interface defines a list of fields; Object types that implement that interface are guaranteed to implement those fields. Whenever the type system claims it will return an interface, it will return a valid implementing type.

A Union defines a list of possible types; similar to interfaces, whenever the type system claims a union will be returned, one of the possible types will be returned.

Finally, oftentimes it is useful to provide complex structs as inputs to GraphQL field arguments or variables; the Input Object type allows the schema to define exactly what data is expected.

规范提到的两种包装器类型 - ListNon-Null 稍后描述。 List 是“一种特殊的集合类型,它声明列表中每个项目的类型”,而 Non-Null 包装现有类型并使 null 成为无效响应(默认情况下所有类型均可为空)。

该规范还描述了根操作类型。根操作类型分为三种:查询变异订阅。只需要查询。这些本身并不是单独的类型,而是有效指向某些其他类型的模式定义的一部分。所有三个都必须是对象类型。

schema {
  query: Foo
}

type Foo {
  someField: String
}

关于javascript - GraphQL 类型 : What is the correct bucketing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53224613/

相关文章:

node.js - 是否可以在 graphQL 执行期间访问特定的查询变量?

javascript - 为什么 $ ("body") == $ ("body") 返回 false?

javascript - react : event bubbling through nested components

php - 如何将数据库值存储为逗号分隔?

javascript - 错误 : Network error: Error writing result to store for query (Apollo Client)

android - 专门使用 GraphQl 解析 JSON 数据

javascript - “this”在 Node 环境中的行为与浏览器不同

javascript - 从函数 jquery 调用数组

express - Graphql查询仅不是空对象

github - 什么时候不应该使用 GitHub GraphQL API?