Only returns a single Entity entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Entity entity is found. Returns a *NotFoundError when no Entity entities are found.
(ctx context.Context)
| 179 | // Returns a *NotSingularError when more than one Entity entity is found. |
| 180 | // Returns a *NotFoundError when no Entity entities are found. |
| 181 | func (eq *EntityQuery) Only(ctx context.Context) (*Entity, error) { |
| 182 | nodes, err := eq.Limit(2).All(setContextOp(ctx, eq.ctx, "Only")) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | switch len(nodes) { |
| 187 | case 1: |
| 188 | return nodes[0], nil |
| 189 | case 0: |
| 190 | return nil, &NotFoundError{entity.Label} |
| 191 | default: |
| 192 | return nil, &NotSingularError{entity.Label} |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // OnlyX is like Only, but panics if an error occurs. |
| 197 | func (eq *EntityQuery) OnlyX(ctx context.Context) *Entity { |
no test coverage detected