Only returns a single User entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one User entity is found. Returns a *NotFoundError when no User entities are found.
(ctx context.Context)
| 323 | // Returns a *NotSingularError when more than one User entity is found. |
| 324 | // Returns a *NotFoundError when no User entities are found. |
| 325 | func (uq *UserQuery) Only(ctx context.Context) (*User, error) { |
| 326 | nodes, err := uq.Limit(2).All(setContextOp(ctx, uq.ctx, "Only")) |
| 327 | if err != nil { |
| 328 | return nil, err |
| 329 | } |
| 330 | switch len(nodes) { |
| 331 | case 1: |
| 332 | return nodes[0], nil |
| 333 | case 0: |
| 334 | return nil, &NotFoundError{user.Label} |
| 335 | default: |
| 336 | return nil, &NotSingularError{user.Label} |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // OnlyX is like Only, but panics if an error occurs. |
| 341 | func (uq *UserQuery) OnlyX(ctx context.Context) *User { |
no test coverage detected