Create a GraphQL schema to make data available to GraphQL queries.

Before you begin

Role required: graphql_schema_admin or admin

Procedure

  1. Navigate to All > System Web Services > GraphQL > GraphQL APIs.
  2. Select New.
  3. Complete these fields.

    You can use this directive in your schema:

    @source: Maps 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.

    This example defines an Incident object type in the schema and uses a resolver script to map the type to a GlideRecord from the Incident table. Using the @source directive maps the fields within the Incident type to the value or display_value in the Incident GlideRecord.

    type Incident {
        id: String @source(value: "sys_id.value")
        active: Boolean @source(value: "active.display_value")
        state: String @source(value: "state.display_value")    
        priority: String @source(value: "priority.display_value")
        severity: String
        description: DisplayableString
        resolvedBy: User @source(value: "resolved_by.value")
        openedBy: User @source(value: "opened_by.value")
        child_incidents: String
        opened_at: String
        resolved_at: String
        closed_at: String
        work_notes: String    
        comments: String @source(value: "comments.display_value")
        parent_incident: String
    }
  4. In the Security section, fill in the fields.
    Note: The API returns clear error messages to users who don’t have access to a schema, or who aren’t authenticated when the API requires authentication to access.
  5. Save the form.
  6. (Optional) Create a resolver script to define what value the schema returns when a component queries a field.
    If you don't define a resolver for a field, the query returns any matching field value from the parent object type. For example, suppose you have an Incident object type in your schema with a worknotes field. The Incident object type has a resolver that maps to a GlideRecord from the Incident table. If you don’t create a resolver mapping for the worknotes field, the system searches the parent object's data source, which is the GlideRecord from the Incident table, for a worknotes field and assigns the associated value.
    1. Select the GraphQL Scripted Resolvers tab and select New.
    2. Complete the form.

      This example returns a record from the User table when the associated field is queried:

      (function process(/*ResolverEnvironment*/ env) {
      
          var userid = env.getArguments().id != null ? env.getArguments().id : env.getSource();
          var now_GR = new GlideRecord('sys_user');
          gr.addQuery('sys_id', userid);
          gr.queryO;
          return gr;
      
      })(env);
    3. Select Submit.
  7. (Optional) Define the type resolvers for your schema to resolve interfaces and unions into concrete types.
    1. Select the GraphQL Scripted Type Resolvers tab and select New.
    2. Complete the form.
    3. Select Submit.
  8. (Optional) Map the resolver and type resolver records to fields in the schema.
    This mapping tells the system what value to return when a component queries a field.
    1. Select the GraphQL Scripted Resolver Mappings tab and select New.
    2. Complete the form.
    3. Select Submit.