Here is a GraphQL server that works perfectly until the day you obfuscate it:
public class Query
{
public IQueryable<Product> GetProducts(CatalogContext db) => db.Products;
public Task<Product?> GetProductById(int id, IProductRepository repo)
=> repo.FindAsync(id);
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = "";
public decimal Price { get; set; }
public ProductStatus Status { get; set; }
}
public enum ProductStatus { Draft, Published, Archived }
Register it with AddGraphQLServer().AddQueryType<Query>() and HotChocolate emits this schema:
type Query {
products: [Product!]!
productById(id: Int!): Product
}
type Product {
id: Int!
name: String!
price: Decimal!
status: ProductStatus!
}
enum ProductStatus { DRAFT PUBLISHED ARCHIVED }
Run the assembly through a renamer and start it again, and the schema becomes type a { b: Int! c: String! … } with a query field called d. Nothing threw. The server started cleanly and the schema is perfectly valid. But every client query you have ever shipped — { products { id name } } — now fails with "The field products does not exist on the type Query." The contract broke at runtime, in a place no compiler and no build step could see.
This is the single most common way obfuscation "breaks GraphQL", and like the serialization version of the problem it is worth pinning down precisely — because the fix is not a blunt keep rule. It is one attribute, and it lets every symbol keep renaming.
Why the rename reaches the wire
Internal calls survive renaming because they are linked by metadata token: the compiler emits call against a token, and a renamer that rewrites the callee rewrites every token that points at it, so the call graph stays connected. HotChocolate does not build the schema from tokens. At schema-build time it reflects over each registered type, reads the member's actual CLR Name, runs it through the naming convention, and uses that string as the GraphQL name. The default DefaultNamingConventions does three things worth knowing:
- a type name is used as-is (
Product→Product); - a field name is camel-cased, and a leading
Geton a resolver method is stripped first (GetProducts→products,GetProductById→productById); - an enum value is converted to CONSTANT_CASE (
Draft→DRAFT); a resolver parameter name becomes the argument name (id→id).
So the wire name is a pure function of the CLR name. The renamer rewrites the CLR name to a; the naming convention faithfully turns a into the field a. HotChocolate has no idea a client ever called it products, because the only record of that name was the method name, and the method name is gone.
The fix: pin the name, keep the rename
The decoupling you want is: the resolver method renames (it is your logic, protect it), but the field name it exposes is frozen. HotChocolate gives you exactly that with [GraphQLName]. Annotate every public surface with the name the schema must keep:
[GraphQLName("Product")]
public class Product
{
[GraphQLName("id")] public int Id { get; set; }
[GraphQLName("name")] public string Name { get; set; } = "";
[GraphQLName("price")] public decimal Price { get; set; }
[GraphQLName("status")] public ProductStatus Status { get; set; }
}
[GraphQLName("ProductStatus")]
public enum ProductStatus
{
[GraphQLName("DRAFT")] Draft,
[GraphQLName("PUBLISHED")] Published,
[GraphQLName("ARCHIVED")] Archived,
}
public class Query
{
[GraphQLName("products")]
public IQueryable<Product> GetProducts(CatalogContext db) => db.Products;
[GraphQLName("productById")]
public Task<Product?> GetProductById(
[GraphQLName("id")] int id, IProductRepository repo)
=> repo.FindAsync(id);
}
Why this holds under obfuscation is a metadata detail worth understanding. The string "products" is a custom-attribute argument, so it is stored in the assembly's #Blob heap and reached through the CustomAttribute table — it is not an ldstr instruction in any method body. HotChocolate reads that blob with reflection at schema-build time and uses it in preference to the member's CLR name. So the renamer is free to turn GetProducts into a; the schema builder never consults the CLR name because the attribute answered first. The field stays products.
This also means string encryption cannot touch it. Nebula's string encryption rewrites ldstr in method bodies into a decrypt call; it does not route custom-attribute blobs through the encryptor, because those are consumed by reflection and the framework expects the plain value. Your [GraphQLName] literals survive renaming and string encryption with no special configuration — which is precisely what you want, because the whole point is to protect the resolver bodies while leaving the public contract legible on the wire.
The surfaces people forget
The type and field names are the obvious ones. Three others break the same way and are easy to miss:
-
Resolver arguments.
GetProductById(int id, …)exposes an argument namedidderived from the parameter name. Nebula leaves parameter names alone by default, but if you enable parameter renaming the argument becomesa;[GraphQLName("id")]on the parameter pins it regardless. -
Input objects. An input type binds incoming values to properties by GraphQL name, and HotChocolate appends an
Inputsuffix by convention (AddProductInput). Attribute the type and every property, exactly as you would an output type — an un-attributed input property silently drops the client's value and leaves the field at its default. -
Relay global IDs. If you expose nodes with
[Node]and[ID], the global ID is a base64 of the type name plus the key. Rename the type without pinning its schema name and every previously issued global ID becomes unresolvable, because the decoder looks for a type whose schema name matches the embedded string. Pin the node type's name and the IDs stay stable across builds.
Verifying it, and the keep-rule fallback
Treat the schema as a contract artifact: export the SDL in CI before and after the protected build and diff them. HotChocolate ships dotnet graphql schema export (and you can print ISchema in a test), so a byte-for-byte SDL diff of the plain vs. obfuscated build is a hard gate — if any name moved, the diff is non-empty and the build fails. You can confirm the attribute literals survived at the metadata level by opening the protected assembly in Glass.NET and reading the CustomAttribute blobs on the renamed members; the [GraphQLName] strings are plainly there while the member names are a, b, c.
There is a fallback for the rare type you genuinely cannot annotate — a schema stitched from a type you do not own, say. Add it to Nebula's keep-list so its names are excluded from renaming:
{
"rename": { "keep": [ "MyApp.Api.ExternalType", "MyApp.Api.ExternalType.*" ] }
}
But reach for [GraphQLName] first and the keep-list almost never. A keep rule preserves the CLR name in metadata — it leaks the symbol you obfuscated for and protects nothing. The attribute is strictly better: the member renames, the metadata is scrubbed, and only an opaque string in the blob heap carries the public name.
Takeaway
A GraphQL schema built by convention is a reflection contract wearing a different hat: HotChocolate reads your CLR names and your CLR names are the wire API, so a renamer rewrites the API. Make the wire name explicit with [GraphQLName] on every type, field, argument, enum value, and input property, and the relationship inverts — the names become data in the blob heap that renaming and string encryption both leave alone, and the resolver logic you actually wanted to protect renames underneath a frozen schema. Export the SDL on both sides of the build and diff it, and you never ship a silently renamed contract. See the Nebula.NET obfuscation guide for the full rename and string-encryption configuration.
Top comments (0)