API for mutating documents #4212
-
Hey I'm asking here because I think I might just not be getting something obvious. In GraphQL the node types that represent a document appear to be read-only. How can one mutate them (say, inject a field to the root selection set) without recreating the whole document? I don't see anything Bout mutable types in the repo so maybe I'm missing an obviously solution. export declare type DefinitionNode =
| ExecutableDefinitionNode
| TypeSystemDefinitionNode
| TypeSystemExtensionNode;
export declare type ExecutableDefinitionNode =
| OperationDefinitionNode
| FragmentDefinitionNode;
export interface OperationDefinitionNode {
readonly kind: Kind.OPERATION_DEFINITION;
readonly loc?: Location;
readonly operation: OperationTypeNode;
readonly name?: NameNode;
readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
readonly directives?: ReadonlyArray<DirectiveNode>;
readonly selectionSet: SelectionSetNode;
}
declare enum OperationTypeNode {
QUERY = 'query',
MUTATION = 'mutation',
SUBSCRIPTION = 'subscription',
}
export { OperationTypeNode };
export interface VariableDefinitionNode {
readonly kind: Kind.VARIABLE_DEFINITION;
readonly loc?: Location;
readonly variable: VariableNode;
readonly type: TypeNode;
readonly defaultValue?: ConstValueNode;
readonly directives?: ReadonlyArray<ConstDirectiveNode>;
}
export interface VariableNode {
readonly kind: Kind.VARIABLE;
readonly loc?: Location;
readonly name: NameNode;
}
export interface SelectionSetNode {
kind: Kind.SELECTION_SET;
loc?: Location;
selections: ReadonlyArray<SelectionNode>;
}
export declare type SelectionNode =
| FieldNode
| FragmentSpreadNode
| InlineFragmentNode; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
So, although the types say so, it's not like the objects are actually frozen. Mutating does work at runtime: This is a bit confusing. Are there unforeseen consequences by doing this? It's quite convenient for some use-cases though. If its inconsequential to do so, then I don't see why the types are read-only. |
Beta Was this translation helpful? Give feedback.
-
It might be dangerous/unpredictable to mutate documents if they are ever reused. It would be safest to create a new document or other node with a modified attribute as necessary. |
Beta Was this translation helpful? Give feedback.
It seems a bit cumbersome to have two versions of the types, so I experimented a bit with some generic types:
The a…