ServiceNow’s GraphQL API framework lets you define a scripted schema, attach resolver scripts to its fields, and expose record data to a custom component or a third-party system. Four parts make up the build: schema, resolvers, type resolvers and resolver mappings. Access is set per schema and per path, and introspection stays off until an administrator turns it on.
What the framework is built from
Each part is configured from the GraphQL Scripted Schemas record, which the platform stores as sys_graphql_schema.
| Part | Where it is configured | What it does |
|---|---|---|
| Schema (SDL) | Schema script field on the schema record | Sets the types, fields and data types a query can use |
| Resolvers | GraphQL Scripted Resolvers related list | Return the data for each field |
| Type resolvers | GraphQL Scripted Type Resolvers related list | Pick the concrete type when a field is an interface or a union |
| Resolver mappings | GraphQL Scripted Resolver Mappings related list | Connect a resolver to one field path in the schema |
The related lists appear once the schema record is saved. A component that needs three fields from a record can request exactly those three in one call, and several queries can sit behind one API instead of several REST endpoints.
Creating the schema record
Creating a schema requires the graphql_schema_admin or admin role. Go to All, then System Web Services, then GraphQL, then GraphQL APIs, and select New.
Name is the name of the schema. Schema namespace must be unique within the name of its application. Application and Application namespace are read-only; the application namespace works with the schema namespace so that schemas sharing a name do not conflict. The Active option turns the schema on or off.
The Schema field holds the SDL and must be valid GraphQL syntax. Saving the form with invalid syntax shows an error message, so mistakes surface at save time.
Writing the schema in SDL
The SDL defines object types, the fields on each type, and the data type of each field. Queries read data and mutations change it. Subscription operations and custom scalar types are not supported. A field returns data once the schema declares it, so the SDL comes first.
The source directive maps a field to a property of the parent object. If the field also has a resolver script, the record that resolver returns is used instead. A worked example defines an Incident type in which fields such as state and priority return the display value of the matching Incident column, while resolved_by returns the stored value.
Resolvers, type resolvers and mappings
A resolver returns the value for one field. Its form holds a Name, a read-only Schema and Application, and a Script. The script has access to GlideRecord and to two functions on the global env object. getArguments() returns the arguments passed to the previous field, and getSource() returns the parent object.
If a field has no resolver, the query returns the matching field value from the parent object’s data source. A worknotes field on an Incident type, for example, resolves from the Incident GlideRecord without any resolver code.
Type resolvers handle interfaces and unions, where one field can return several types. The Type field names the interface or union, and the script decides which member a value belongs to. Its functions are getArguments(), getObject() for the parent object, and getTypeName() for the name of the interface or union. The example pairs an incident type and a problem type in one union, with the type resolver script choosing which type to return.
A resolver mapping ties everything together. Its Path points to a schema field, and its Resolver names the resolver that fills it. A resolver mapping tells the system which resolver supplies the value for one schema field path.
Securing the schema
Security is set in the Security section of the schema form and works at two levels: the whole API and individual paths.
Requires authentication controls whether callers must sign in. Clearing it makes the schema publicly available. Requires ACL authorization applies ACL checks to the entire API; selecting it reveals an ACLs field where you choose ACLs of the GraphQL type. Requires SNC internal requires the SNC internal role, and it appears when the Explicit Roles plugin (com.glide.explicit_roles) is enabled.
Path ACL depth sets the level of the API at which ACLs apply. A depth of 3 or lower uses fewer resources and helps with returning query responses. For path-level control, leave Requires ACL authorization cleared, set the depth, and create an ACL of the GraphQL type whose name is the exact path starting with the schema namespace, in the form planet, then findAll, then name. Wildcards are not supported. The ACLs field and path ACLs are separate: the first covers the whole API, and the second covers the named paths.
When access is denied, the API returns a clear error message, whether the user lacks access to the schema or is not authenticated for a schema that requires it.
Namespaces and the shape of a query
A GraphQL API has two namespaces. The application namespace is the namespace of the custom application that owns the schema. The schema namespace identifies the schema and keeps query names unique. One application can hold several schema namespaces.
Both namespaces must be included in each query. The application namespace wraps the schema namespace, and the schema namespace wraps the requested field. In the worked example, the application namespace is x_graph_scope, the schema namespace is planet, and a findAll field returns name, mass and distance.
Turning on introspection
Introspection lets a client ask the instance to describe its schema. It is off by default. When introspection is off, GraphQL Explorer does not show the schemas in its documentation pane or as autocomplete suggestions.
Three Boolean system properties sit under System Web Services, then GraphQL, then Properties. Each defaults to False.
| Property | What it controls |
|---|---|
| glide.graphql.glide_record_schema.introspection_enabled | Shows the GlideRecord schema in the GraphQL Explorer documentation pane and in autocomplete |
| glide.graphql.introspection_enabled | Lets users discover supported queries and types; introspection requests need the graphql_schema_admin role |
| glide.graphql.debug | Turns on debug logging for GraphQL |
To enable discovery, an administrator with the admin role selects both introspection options and saves the form. Introspection should not be left on in production, and a component deployed to production should not use introspective queries. Turn the options on to build and test, then off before go-live.
Testing queries with GraphQL Explorer
GraphQL Explorer is the built-in testing tool for these APIs, and it integrates the GraphiQL IDE. Go to All, then System Web Services, then GraphQL, then GraphQL Explorer. It requires the graphql_schema_admin or admin role, and the schema must exist first.
The editor takes the query, and variables sit at the bottom. The Execute query control runs the request, and results appear in the right pane. Add tab opens another query, and Show History lists earlier ones. When introspection is enabled, a Show Documentation Explorer icon lists the queries and mutations, which makes field names easier to find. An empty documentation pane points to the introspection options being off.
Deferring slow fields with the defer directive
The defer directive delays processing of a fragment, so its fields stream back as a separate part of the response after the first results arrive. The client must accept multipart/mixed HTTP responses, sent as an Accept header with a boundary value. A client that does not send that header cannot receive the deferred parts.
Applying the defer directive indiscriminately can degrade performance. Test responses with and without the directive, and defer the slow fields that are not needed right away.
Calling the API from a component
An example Workspace component shows the cases attached to an SLA, built with the Next Experience UI Framework and reading the Case table through the schema.
The query goes in the component’s index.js file and includes both namespaces. The component imports createGraphQLEffect from the ui-effect-graphql package and passes the query with options that name its variables and three action types: one for when the request starts, one for success, and one for error. Action handlers do the rest. A started action can set a loading flag, a success action stores the payload, and the view reads the fields it needs from it.
Network failures go to the error action type. Schema and server errors do not. They arrive as error objects inside a successful request, so the success handler should check the response for error objects before reading the data. A component that handles just the error action would miss them.
Limits to plan around
Subscription operations are not supported, so a schema cannot stream live changes to a caller. Custom scalar types are not supported either. The SDL supports queries and mutations, which cover reading and writing record data.
Frequently asked questions
Where is the schema written?
In the Schema field of the GraphQL Scripted Schemas record, reached through All, System Web Services, GraphQL, GraphQL APIs. The field must be valid GraphQL syntax, and the form reports syntax errors when it is saved.
Why does the GraphQL Explorer documentation pane show nothing?
Introspection is off by default. An administrator turns the options on under System Web Services, then GraphQL, then Properties. The GlideRecord option covers the GlideRecord schema, and the other option covers queries and types.
What does a caller see when it has no access?
A clear error message. That applies to a user without access to the schema and to an unauthenticated user when the schema requires authentication. Path-level access is set with ACLs of the GraphQL type, named with the exact path that starts at the schema namespace.
What happens when a component’s query fails?
A network failure goes to the error action type. A schema or server error comes back as an error object inside a successful request, so the success handler has to check for it.