cayleygraph/cayley · error

error fetching size, context is nil, graph not correctly ini

Error message

error fetching size, context is nil, graph not correctly initialised

What it means

Stats reads the MetadataEntry from the datastore to report quad counts; the datastore Get needs the App Engine request context. A nil context means the store was not initialized, so sizes cannot be fetched.

Source

Thrown at graph/gaedatastore/quadstore.go:514

		if _, ok := err.(*datastore.ErrFieldMismatch); !ok {
			return quad.Quad{}, err
		}
	}
	var label interface{}
	if q.Label != "" {
		label = q.Label
	}
	return quad.Make(
		q.Subject,
		q.Predicate,
		q.Object,
		label,
	), nil
}

func (qs *QuadStore) Stats(ctx context.Context, exact bool) (graph.Stats, error) {
	if qs.context == nil {
		return graph.Stats{}, errors.New("error fetching size, context is nil, graph not correctly initialised")
	}
	key := qs.createKeyForMetadata()
	m := new(MetadataEntry)
	err := datastore.Get(qs.context, key, m)
	if err != nil {
		return graph.Stats{}, err
	}
	return graph.Stats{
		Nodes: refs.Size{
			Value: m.NodeCount,
			Exact: true,
		},
		Quads: refs.Size{
			Value: m.QuadCount,
			Exact: true,
		},
	}, nil
}

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Initialize the store per-request with opts["HTTPRequest"] = r before calling Stats.
  2. Check store-creation errors and skip stats collection when initialization failed.
  3. Cache size stats and compute them within a properly contextualized request.
  4. Move stats to a backend that supports background contexts if you need off-request reporting.

Example fix

// before
st, err := qs.Stats(ctx, false)
// after
if qs.context == nil { return graph.Stats{}, errors.New("gae store not initialized") }
st, err = qs.Stats(ctx, false)
Defensive patterns

Strategy: validation

Validate before calling

if qs.context == nil {
    return graph.Stats{}, errors.New("gaedatastore not initialized; Stats unavailable")
}

Type guard

func statsAvailable(qs *QuadStore) bool { return qs.context != nil }

Try / catch

st, err := qs.Stats(ctx, false)
if err != nil {
    if strings.Contains(err.Error(), "not correctly initialised") {
        // skip stats or re-init store with request context
    }
    return err
}

Prevention

When it happens

Trigger: Calling Stats (directly or via getSize, QuadIteratorSize, estimateSize, sizeForIterator) on a QuadStore with nil context — store created without the HTTPRequest option or init failed silently.

Common situations: Collecting size metrics in cron/background jobs without a request context; dashboard code opening the store at process start; ignoring the creation error from graph.NewQuadStore.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/6e597bc456d959b3. Report an issue: GitHub.