Only returns a single Metadata entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Metadata entity is found. Returns a *NotFoundError when no Metadata entities are found.
(ctx context.Context)
| 130 | // Returns a *NotSingularError when more than one Metadata entity is found. |
| 131 | // Returns a *NotFoundError when no Metadata entities are found. |
| 132 | func (mq *MetadataQuery) Only(ctx context.Context) (*Metadata, error) { |
| 133 | nodes, err := mq.Limit(2).All(setContextOp(ctx, mq.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{metadata.Label} |
| 142 | default: |
| 143 | return nil, &NotSingularError{metadata.Label} |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // OnlyX is like Only, but panics if an error occurs. |
| 148 | func (mq *MetadataQuery) OnlyX(ctx context.Context) *Metadata { |
no test coverage detected