Apollo Link State中__typename取值规则?Todo示例值来源解析
About the
__typename: "TodoItem" in Apollo Link State's Todo Example Let's break down your questions clearly and concisely:
Where does the TodoItem value come from?
The TodoItem here is a custom local type name you define in your Apollo Link State schema.
In the Todo example, you must have declared a type named TodoItem in the local schema when setting up clientState (Apollo Link State's core configuration). It would look something like this:
type TodoItem { id: ID! text: String! completed: Boolean! }
This type defines the structure of your local todo items. When creating a new todo object in the resolver, you specify __typename: "TodoItem" to tell Apollo Client exactly which local type this object corresponds to.
Can this value be set arbitrarily, or are there specific requirements?
You cannot set this value arbitrarily—it has strict rules to follow:
- It must exactly match the name of the type you defined in your local schema. Apollo Client uses
__typenameto normalize data in its cache and resolve fields correctly. If the name doesn't match, the cache won't recognize the object, leading to issues like missing data or failed queries. - If you're combining local state with a remote GraphQL API, avoid naming conflicts with remote type names unless you intentionally want to reuse a remote type (in which case the
__typenamemust match the remote type's name exactly). __typenameis a core identifier for Apollo Client's cache system. Using an incorrect or random value will break cache normalization and cause unexpected behavior, such as null results when querying local state or mismatched field resolutions.
内容的提问来源于stack exchange,提问作者deadcoder0904
相关产品推荐
相关产品推荐

