Query record data using the GraphQL API framework
Summarize
Summary of Query record data using the GraphQL API framework
This content explains how ServiceNow customers can create custom GraphQL APIs to query record data from ServiceNow components or third-party systems. Using the GraphQL API framework, developers can define schemas and resolvers to expose specific data from ServiceNow tables, such as cases associated with SLAs, enabling efficient and precise data retrieval within custom Workspace components built with the Next Experience UI Framework.
Show less
Key Features
- GraphQL Schema Definition: Define the structure and data types of fields available for querying using Schema Definition Language (SDL) in the GraphQL Scripted Schemas table.
- Resolvers: Script the data returned for each field in the schema to map fields to actual record data using GlideRecord.
- Typeresolvers: Resolve GraphQL interfaces and unions to concrete types to support complex data models.
- Resolver Mappings: Map resolvers to schema fields to control data retrieval behavior.
- Introspection: By default disabled for custom schemas but can be enabled to allow clients to discover available queries and mutations.
- GraphQL Explorer: Integrated tool to test GraphQL queries and mutations against your schemas.
- Directives: Support for @source directive to map fields to parent object properties and @defer directive to improve user experience by deferring slow field responses.
- Global Resolver and Typeresolver Functions: Provide access to arguments, parent objects, and type names within resolver scripts for dynamic data handling.
- Namespace Handling: Requires inclusion of both application and schema namespaces in queries to ensure uniqueness and avoid conflicts.
What You Need Before You Begin
- Knowledge of GraphQL schema creation and JavaScript for scripting resolvers.
- Basic understanding of web component development and the ServiceNow data model.
- Experience with GlideRecord to map schema fields to platform record data.
- A custom Workspace component ready to consume queried data.
Limitations
- Subscription operations and custom scalar types are not supported.
- Introspection is disabled by default and must be explicitly enabled.
Practical Benefits for ServiceNow Customers
- Enables precise, client-optimized data queries that reduce over-fetching compared to REST APIs.
- Simplifies integration with third-party systems by exposing a single, flexible GraphQL endpoint.
- Improves user experience by allowing deferred loading of slow fields using the @defer directive.
- Supports evolving data needs with flexible schema and resolver configurations, enhancing component and application development.
Additional Resources
- Demo GraphQL PTO calendar schema available via the GraphQL Framework Demo Application plugin.
- Configuration options through system properties to fine-tune GraphQL API behavior.
Create a custom GraphQL API to query record data from a component or a third-party system.
For example, you can create a component that displays the cases associated with an SLA. You can use the Next Experience UI Framework to develop the component you need, and access case data from the platform by creating a GraphQL schema that defines data in the Case table.
To learn more about developing components, see Developing components for Workspace.
Benefits of GraphQL
GraphQL is a web query language optimized for client-side development. Using scripted GraphQL, you can:
- Discover fields and objects available to query through introspection.
- Query the exact data you need from a component.
- Manage multiple possible queries from a single API, as opposed to multiple endpoints for a REST request.
- Integrate with third-party systems by making the schema public.
- Generate the GraphQL query from your component and handle the response.
What to know before you begin
Before you start creating custom GraphQL APIs, make sure you have:
- GraphQL knowledge to create a schema.
- JavaScript knowledge to define the API behavior.
- General knowledge of web component concepts.
- A custom Workspace component to consume record data.
- Understanding of the ServiceNow data model that you want to expose in the schema.
- GlideRecord knowledge to map fields to record data in your resolver scripts.
GraphQL overview
Creating a scripted GraphQL API includes these parts:
- GraphQL Schema Definition Language (SDL)
- Define the structure and data type of fields available in a GraphQL query. You can define the SDL using the Schema script field in the GraphQL Scripted Schemas [sys_graphql_schema] table. The SDL only supports Query and Mutation operations.
- Resolvers
- Define the data returned by each field. You can define the resolvers for each field in the GraphQL Scripted Resolvers related list on the GraphQL Scripted Schemas form.
- Typeresolvers
- Resolve interfaces and unions into concrete GraphQL types. For example, you might define a union between an
incidenttype and aproblemtype. Use the typeresolver script to define when to return which. You can define the typeresolvers in the GraphQL Scripted Typeresolvers related list on the GraphQL Scripted Schemas form. - Resolver mappings
- Map resolvers to fields in the schema. You can define resolver mappings in the GraphQL Scripted Resolver Mappings related list on the GraphQL Scripted Schemas form.
To learn more about the GraphQL query language, see the GraphQL website.
To test queries to your GraphQL APIs, you can use the GraphQL Explorer, an integrated GraphQL testing tool. For more information, see Test GraphQL APIs with GraphQL Explorer.
Limitations
The following GraphQL features aren't supported:
- Subscription operations
- Custom scalar types
Introspection
By default, introspective queries into your custom schemas aren’t enabled. To turn on introspection, see Enable introspective queries for GraphQL schemas.
Namespaces
GraphQL APIs have two different namespaces:
- Application namespace
- The namespace for the custom application. To learn more about application namespaces, see Application scope.
- Schema namespace
- The namespace for the schema to ensure that all queries are unique. You can have multiple schema namespaces in a single application.
When querying data, you must include both namespaces in your query. For example, the following query is searching for data with the following namespaces:
- Application namespace:
x_graph_scope - Schema namespace:
planet
query {
x_graph_scope {
planet {
findAll {
name
mass
distance
}
}
}
}Directives and global functions
@sourceschema directiveMaps a GraphQL field to the value of a property of the parent object. If the field has a separate resolver script, the system uses the record that it resolves to instead of the parent object.
Use the
@sourcedirective in your schema script.@deferquery directive- Defer processing of a GraphQL fragment until later in the query. Use this query directive to delay returning data for slow-responding fields within a fragment. Stream the field results of the deferred fragment as a multi-part response.Note:To use the
@deferdirective, your GraphQL client must accept multipart/mixed HTTP headers. For example, set the HTTP headers toAccept: multipart/mixed; boundary="-".Use the
@deferdirective to improve user time to interaction. Avoid applying this query directive indiscriminately as it can also cause performance degradations. Conduct performance testing to determine which fields can be deferred for better performance. - Resolver functions
These functions are available on the global env object.
- getArguments(): Returns the arguments of the previous field.
- getSource(): Returns the parent object.
Use in your resolver script.
- Typeresolver functions
These functions are available on the global env object.
- getArguments(): Returns the arguments of the previous field.
- getObject(): Returns the parent object.
- getTypeName(): Returns the name of the interface or union type.
Use in your typeresolver script.
Demo application
To see a demo GraphQL PTO calendar schema with mutations and queries, enable the GraphQL Framework Demo Application plugin (com.glide.graphql.framework.demo).