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