OBLYTECH Logo
OBLYTECH Logo

GraphQL Queries in the Platform

GraphQL in ServiceNow lets a component or external system request selected record fields through a schema backed by resolver scripts. Build the schema, connect its fields to record data, apply authentication and access controls, then test a namespaced query in GraphQL Explorer before using it in a component.

Plan a GraphQL query around the screen that needs it

Start with the information a reader needs to see together. A case panel might need a case identifier, a short description, and a related person’s display name. Write that field list before designing the schema. It gives the query a concrete purpose and gives reviewers a way to judge whether the response exposes unnecessary information.

The scripted GraphQL API framework separates the shape of the response from the code that retrieves its values. The schema declares available fields and their types. Resolvers supply values, and resolver mappings connect scripts to schema paths. This distinction matters when a field appears in a query but returns an unexpected value: inspect both the declaration and its data mapping.

Before building, make sure you can work with the ServiceNow data model, JavaScript, GraphQL Schema Definition Language, and GlideRecord. Component work also needs knowledge of web components. For broader capability questions before building, use the ServiceNow GraphQL API support overview.

These steps use the Brazil release of ServiceNow. Confirm the labels on your instance before applying them. Keep a small test case that records the expected fields, calling identity, and expected result. That becomes the baseline as you move from the editor to a consuming component.

Create the schema and trace each field to data

Creating a schema requires graphql_schema_admin or admin. Navigate to All > System Web Services > GraphQL > GraphQL APIs and select New. Set Name and Schema namespace, then review the read-only Application and Application namespace values. Use Active to control whether the schema is enabled.

Enter valid SDL in Schema. Syntax problems produce errors when the form is saved, so resolve those before investigating a resolver. Treat the schema as the contract the consumer will use: choose field names that describe the returned values and keep their types consistent with the intended display.

  1. Identify the root field that supplies the record or collection the query needs.
  2. Decide which child values can come from that parent object and which require a separate resolver.
  3. In GraphQL Scripted Resolvers, select New and complete Name and Script. Review the read-only Schema and Application fields.
  4. In GraphQL Scripted Resolver Mappings, select New and supply Path and Resolver, then select Submit.

Resolver scripts can access GlideRecord. The environment functions getArguments() and getSource() provide the previous field’s arguments and the parent object respectively. Trace those inputs before adding more retrieval logic. A script receiving a parent value needs different handling from one receiving an explicit identifier argument.

A field without its own resolver can inherit a matching value from its parent object. The source directive can also map a field to a parent property, including a record value or display value. Review these paths deliberately: a missing explicit mapping does not, by itself, prove that a field will return nothing.

Set access controls before testing real records

The schema form separates authentication from authorization. Requires authentication controls whether a caller must authenticate. Clearing it makes the schema publicly available. Choose that behavior deliberately before the schema is used with sensitive record data, and document the expected calling identity alongside the test query.

Requires ACL authorization enables authorization for the entire API. When selected, ACLs lets you choose ACL records of the GraphQL type. Path ACL depth controls where path-specific ACL checks run. To configure checks for specific paths, clear Requires ACL authorization first, then set the depth for the API paths you need to protect before testing queries against record data. The API-wide ACLs field is independent of those path-specific checks; do not treat one as proof that the other is configured.

For a path-specific ACL, use the GraphQL type and put the exact API path, beginning with the schema namespace, in Name. Wildcards are not supported. A path depth of three or lower uses fewer resources and helps query responses. Select depth based on the access boundary you need, then review the response with that boundary in mind.

Keep the access review attached to the field inventory. For each field, record why the consumer needs it and which caller should receive it. If the integration design also involves separately defined request handlers, the guide to building secure Scripted REST APIs covers that neighboring approach.

For help planning this work, see ServiceNow implementation.

Test a complete namespaced query in GraphQL Explorer

Using GraphQL Explorer requires graphql_schema_admin or admin. Open All > System Web Services > GraphQL > GraphQL Explorer after the schema exists. In the editor, include both the application namespace and schema namespace around the fields you request. A field name by itself does not identify the full query path.

Build the first test around a small, recognizable result. Supply any required inputs through Variables at the bottom of the editor, then select Execute query. The response appears in the right pane. Compare the returned structure with the expected field list before expanding the selection.

Introspection makes schema discovery available through documentation and completion suggestions. Enabling it requires admin: navigate to System Web Services > GraphQL > Properties, select Enable/Disable introspective queries for the GlideRecord Schema and Enable/Disable introspective queries for users to discover the supported GraphQL queries and types, then select Save. Introspection requests require graphql_schema_admin.

The corresponding properties, glide.graphql.glide_record_schema.introspection_enabled and glide.graphql.introspection_enabled, default to false. With introspection enabled, Show Documentation Explorer exposes available queries and mutations. Keep introspection out of production, including deployed component queries. Maintain the explicit query and expected response so testing does not depend on discovery being enabled.

Move the tested query into a component

The component-query procedure requires admin and a custom component. Define the query in the component’s index.js file, retaining both namespaces. Import createGraphQLEffect from the @servicenow/ui-effect-graphql package and use that API to call the query. Pass the intended variables rather than changing the query’s structure for each input.

Provide action handlers for the request starting, succeeding, and failing. A loading state lets the screen communicate that retrieval is underway. A completed request needs inspection of its payload: schema or server errors can arrive through the success path as error objects, while the failure action handles network errors. A successful transport event is therefore insufficient evidence that the requested data is usable.

Test the consumer with a known result, an empty result, and an error response. Decide what the person using the component should see in each situation. Avoid leaving a loading indicator active after a completed request, and keep the error state distinct from an ordinary absence of matching data.

If a fragment contains slow fields, the defer directive can return other fields first and stream the deferred results later. The client must accept multipart/mixed responses. Test performance before applying it: deferral can also worsen performance. Measure the screen’s behavior with the same query and representative records before keeping the change.

Diagnose the response before widening the query

Work through failures in layers. Check that the schema is active and the SDL saved successfully, then verify both namespaces. Next, trace the requested field through its mapping and resolver inputs. Finally, compare the calling identity with the authentication and authorization settings. This sequence keeps a schema-path problem from being mistaken for missing record data.

The glide.graphql.debug property controls GraphQL debug logging and defaults to false. If you change it during investigation, record that change with the test case. For issues spanning other integration mechanisms, use the REST and SOAP integration troubleshooting guide to organize the wider investigation.

Answers to common questions about extending the schema

Can this framework provide subscription operations?

Subscription operations are unsupported. The SDL supports Query and Mutation operations. If a consumer needs ongoing updates, treat that requirement as a separate design decision rather than adding a subscription to this schema.

Can I define a custom scalar for a specialized field?

Custom scalar types are unsupported. Plan the field contract within the supported type system before implementing the consumer, so its interpretation of a value does not depend on a scalar the framework cannot provide.

How can an interface or union resolve to a concrete type?

Use GraphQL Scripted Type Resolvers to define the resolution script. Its environment includes getObject() and getTypeName(). Map the resolver to the relevant schema field so the returned object has the intended concrete type.

Is there a demonstration schema for experimenting?

The GraphQL Framework Demo Application plugin, com.glide.graphql.framework.demo, provides a PTO calendar demonstration with queries and mutations. Use a controlled development context to explore its structure before adapting ideas to your own record model.

Can Explorer show queries I ran earlier?

Select Show History in the left sidebar to view previous queries. Use Add tab when comparing additional queries, keeping their inputs and expected results distinguishable during the test session.