Only returns a single Node entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Node entity is found. Returns a *NotFoundError when no Node entities are found.
(ctx context.Context)
| 131 | // Returns a *NotSingularError when more than one Node entity is found. |
| 132 | // Returns a *NotFoundError when no Node entities are found. |
| 133 | func (nq *NodeQuery) Only(ctx context.Context) (*Node, error) { |
| 134 | nodes, err := nq.Limit(2).All(setContextOp(ctx, nq.ctx, "Only")) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | switch len(nodes) { |
| 139 | case 1: |
| 140 | return nodes[0], nil |
| 141 | case 0: |
| 142 | return nil, &NotFoundError{node.Label} |
| 143 | default: |
| 144 | return nil, &NotSingularError{node.Label} |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // OnlyX is like Only, but panics if an error occurs. |
| 149 | func (nq *NodeQuery) OnlyX(ctx context.Context) *Node { |