{"record":{"id":"d1409a3d12c1d1f6","repo":"cayleygraph/cayley","slug":"context-is-nil-graph-is-not-initialized","errorCode":null,"errorMessage":"context is nil, graph is not initialized","messagePattern":"context is nil, graph is not initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/gaedatastore/quadstore.go","lineNumber":460,"sourceCode":"\treturn qs.newIterator(quadKind, dir, v)\n}\n\nfunc (qs *QuadStore) NodesAllIterator() iterator.Shape {\n\treturn qs.newAllIterator(nodeKind)\n}\n\nfunc (qs *QuadStore) QuadsAllIterator() iterator.Shape {\n\treturn qs.newAllIterator(quadKind)\n}\n\nfunc (qs *QuadStore) ValueOf(s quad.Value) (graph.Ref, error) {\n\tid := hashOf(s)\n\treturn &Token{Kind: nodeKind, Hash: id}, nil\n}\n\nfunc (qs *QuadStore) NameOf(val graph.Ref) (quad.Value, error) {\n\tif qs.context == nil {\n\t\treturn nil, errors.New(\"context is nil, graph is not initialized\")\n\t} else if v, ok := val.(refs.PreFetchedValue); ok {\n\t\treturn v.NameOf(), nil\n\t}\n\tvar key *datastore.Key\n\tif t, ok := val.(*Token); ok && t.Kind == nodeKind {\n\t\tkey = qs.createKeyFromToken(t)\n\t} else {\n\t\treturn nil, errors.New(\"token not valid\")\n\t}\n\n\t// TODO (panamafrancis) implement a cache\n\n\tnode := new(NodeEntry)\n\terr := datastore.Get(qs.context, key, node)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn quad.Raw(node.Name), nil","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/gaedatastore/quadstore.go#L442-L478","documentation":"NameOf resolves a node reference to its quad.Value via a datastore lookup, which requires the App Engine request context. A nil context means the store was not initialized, so node names cannot be fetched.","triggerScenarios":"Calling NameOf (directly or via String, Contains, IteratedStrings/Values, iterator Next/Contains) on a QuadStore whose context is nil because getContext failed at init.","commonSituations":"Store opened at module init outside a request; ignoring New's error then querying; using the GAE backend outside Google App Engine entirely.","solutions":["Initialize the store within a request handler passing opts[\"HTTPRequest\"] = r.","Check and handle the error from store creation before any reads.","For PreFetchedValue refs the lookup is bypassed — consider pre-fetching values to reduce datastore dependency.","Switch backends if you are not actually running on App Engine."],"exampleFix":"// before\nval, err := qs.NameOf(ref)\n// after\nif qs.context == nil { return nil, errors.New(\"store missing gae context\") }\nval, err := qs.NameOf(ref)","handlingStrategy":"validation","validationCode":"if qs.context == nil {\n    return errors.New(\"gaedatastore not initialized; NameOf unavailable\")\n}","typeGuard":"func readable(qs *QuadStore) bool { return qs.context != nil }","tryCatchPattern":"val, err := qs.NameOf(ref)\nif err != nil {\n    if strings.Contains(err.Error(), \"not initialized\") {\n        // re-create store with current request\n    }\n    return err\n}","preventionTips":["Open the store inside a request handler with HTTPRequest set.","Handle New's error before any read.","Use PreFetchedValue refs to avoid datastore lookups when possible.","Switch backend if not on App Engine."],"tags":["appengine","read","initialization","state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}