# Instructions for Fluent GraphQLApi API

Always reference the GraphQLApi API specifications for more details.

1. Import `GraphQLApi` from `@servicenow/sdk/core`. One call writes `sys_graphql_schema` plus one `sys_graphql_resolver` per `resolvers` entry, one `sys_graphql_resolver_mapping` per `paths` entry inside each resolver, and one `sys_graphql_typeresolver` per `typeResolvers` entry — never author those child tables separately with `Record()`.
2. Reach for a GraphQL API when clients need to request exactly the fields they want in one round trip, when a schema is genuinely graph-shaped (unions, interfaces, nested traversal), or when access must be controlled **per field** rather than all-or-nothing. For a small fixed set of endpoints, a Scripted REST API (`RestApi`) remains simpler and cheaper — do not convert working REST resources without a reason.
3. **Write the SDL first, then bind resolvers to it.** `schema` is mandatory and is compiled *by the platform on insert*, not by the Fluent build — so a malformed SDL surfaces at deploy time, not build time. Keep small schemas inline; move anything substantial to `Now.include('./schema.graphql')`.
4. `namespace` is mandatory, must be a valid GraphQL name, and must be unique among the APIs in its application namespace — a duplicate is a build error. It is both the envelope key and the second segment of every runtime ACL path, so renaming it later invalidates every field-level path ACL you wrote.
5. **Do not set `applicationNamespace` in a scoped app.** It is derived from the scope (`x_snc_my_app` → `xSncMyApp`), and an explicit differing value is reported as a hint and ignored. Conversely, a **global-scope** app *should* set it explicitly: the build cannot read the instance settings and caller roles the platform uses, so it falls back to `now`.
6. **Give every resolver and type resolver its own `$id`.** Names and SDL type names change; `$id` is what keeps an update attached to the original record instead of inserting a duplicate. Omitting a child `$id` is the most common cause of orphaned resolver records after a rename.
7. **Do not write a resolver function inline in `script`.** The type is `((env) => unknown) | string`, so an inline arrow *type-checks* — but it is a build error. The three legal forms are an imported named function from a server module (preferred: type-checked, reusable, editable), an inline script string, or `Now.include('./resolver.js')`. Read field arguments inside the script with `env.getArguments()`.
8. Keep the two path formats straight — they are different and not interchangeable. A resolver's `paths` entries are colon-delimited `Type:field` (e.g. `Query:items`, `Item:cost`). A field-level ACL's `name` is the slash-delimited runtime query path (e.g. `/xSncMyApp/catalogGql/items/cost`), starting with the application namespace and namespace. Neither uses the raw application scope.
9. Add a `typeResolvers` entry for **every** union and interface in the SDL. Without one the engine cannot tell which concrete type a value is, so it cannot decide which fields to resolve next. `typeName` must match the SDL type exactly.
10. **Never leave `enforceAcl` empty and assume you are covered.** Unlike the three `requires*` flags, `enforceAcl` is not a secure-by-default boolean — it is an array of ACL references that defaults to empty, meaning no schema gate at all. `requiresAclAuthorization: true` only enforces the gates you actually listed.
11. Keep `requiresAuthentication`, `requiresAclAuthorization`, and `requiresSncInternalRole` at their `true` defaults unless you have a specific, stated reason. Setting any of them to `false` removes a layer of access control for every field in the API at once. Omit them rather than restating `true`.
12. **Count ACL path segments before setting `contextualAclMaxDepth`.** Depth includes the two envelope segments, so `/xSncMyApp/catalogGql/items/cost` is depth 4, not 2. The default of `4` covers top-level and second-level fields; a third-level field ACL needs `5` or it is silently never enforced. Setting it below the depth of an ACL you rely on is a security hole with no error.
13. Do not reuse a resolver `name`, a `paths` entry, or a `typeResolvers` `typeName` within one API — each must be unique and duplicates are build errors.
14. Remember what a denied field does: it resolves to `null` rather than failing the query, and a user with the `admin` role overrides the check unless the ACL sets `adminOverrides: false`. Design clients to tolerate `null` on protected fields.
15. Skip redundant defaults (`active: true`, `requiresAuthentication: true`, `contextualAclMaxDepth: 4`) — they round-trip back as omitted. `GraphQLApi` accepts `protectionPolicy` and `$override`, but **not** `$meta` (no `installMethod`, no `useEsLatest`). As of SDK v4.13.0 a `resolvers` or `typeResolvers` entry does type-check with `$meta`, but the build silently ignores it there (no install-method folder, no ES-latest record) — never set `$meta` on those entries.
