Only returns a single Group entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Group entity is found. Returns a *NotFoundError when no Group entities are found.
(ctx context.Context)
| 155 | // Returns a *NotSingularError when more than one Group entity is found. |
| 156 | // Returns a *NotFoundError when no Group entities are found. |
| 157 | func (gq *GroupQuery) Only(ctx context.Context) (*Group, error) { |
| 158 | nodes, err := gq.Limit(2).All(setContextOp(ctx, gq.ctx, "Only")) |
| 159 | if err != nil { |
| 160 | return nil, err |
| 161 | } |
| 162 | switch len(nodes) { |
| 163 | case 1: |
| 164 | return nodes[0], nil |
| 165 | case 0: |
| 166 | return nil, &NotFoundError{group.Label} |
| 167 | default: |
| 168 | return nil, &NotSingularError{group.Label} |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // OnlyX is like Only, but panics if an error occurs. |
| 173 | func (gq *GroupQuery) OnlyX(ctx context.Context) *Group { |
no test coverage detected