dgraph-io/dgraph · error

error while getting the schema for ns %d

Error message

error while getting the schema for ns %d

What it means

The getGQLSchema query resolver looks up the current stored schema string for the request's namespace in as.gqlSchemas; when the lookup returns !ok the query fails with this wrapped error. It means no schema record is cached/known for that namespace at query time.

Source

Thrown at graphql/admin/admin.go:983

	// * if the schema hasn't yet been set even once for a non-Galaxy namespace
	// If schema is nil then do not attach Resolver for
	// introspection operations, and set GQL schema to empty.
	if gqlSchema == nil {
		resolverFactory = resolverFactoryWithErrorMsg(errNoGraphQLSchema)
		gqlSchema, _ = schema.FromString("", ns)
	} else {
		resolverFactory = resolverFactoryWithErrorMsg(errResolverNotFound).
			WithConventionResolvers(gqlSchema, as.fns)
		// If the schema is a Federated Schema then attach "_service" resolver
		if gqlSchema.IsFederated() {
			resolverFactory.WithQueryResolver("_service", func(s schema.Query) resolve.QueryResolver {
				return resolve.QueryResolverFunc(func(ctx context.Context, query schema.Query) *resolve.Resolved {
					as.mux.RLock()
					defer as.mux.RUnlock()
					sch, ok := as.gqlSchemas.GetCurrent(ns)
					if !ok {
						return resolve.EmptyResult(query,
							fmt.Errorf("error while getting the schema for ns %d", ns))
					}
					handler, err := schema.NewHandler(sch.Schema, true)
					if err != nil {
						return resolve.EmptyResult(query, err)
					}
					data := handler.GQLSchemaWithoutApolloExtras()
					return resolve.DataResult(query,
						map[string]interface{}{"_service": map[string]interface{}{"sdl": data}},
						nil)
				})
			})
		}

		if as.withIntrospection {
			resolverFactory.WithSchemaIntrospection()
		}
	}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Confirm the namespace id matches the one that actually has a GraphQL schema (check the X-Dgraph-Namespace header).
  2. Upload a schema for that namespace via updateGQLSchema.
  3. Inspect the Dgraph data for the stored schema (dgraph.graphql.schema) in that namespace.

Example fix

// before
curl -H 'X-Dgraph-Namespace: 5' ... '{ getGQLSchema { schema } }'
// after: use the namespace that holds the schema
curl -H 'X-Dgraph-Namespace: 0' ... '{ getGQLSchema { schema } }'
Defensive patterns

Strategy: validation

Validate before calling

// verify namespace before querying the schema
const ns = Number(process.env.DGRAPH_NAMESPACE ?? 0);
if (!Number.isInteger(ns)) throw new Error('X-Dgraph-Namespace must be an integer');

Try / catch

try { await getGQLSchema(); } catch (e) { if (String(e).includes('error while getting the schema')) { checkNamespaceHeader(); await uploadSchema(sdl); } else throw e; }

Prevention

When it happens

Trigger: Querying getGQLSchema for a namespace whose schema was never stored (or was deleted) in Dgraph, or where the in-memory key server has no entry under that namespace id.

Common situations: Wrong namespace id in the X-Dgraph-Namespace header; multi-tenancy setup where the schema was only added to namespace 0; schema table entry removed while server keeps serving.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/5ee1dd3fdb662caf. Report an issue: GitHub.