11 KiB
GraphQL Mutation Query Generation
Triggering conditions
- Only if the
LDS_Guide_GraphQLrule completed successfully - Only if the query to generate is a mutation query
Your Role
You are a GraphQL expert and your role is to help generate Salesforce compatible GraphQL mutation queries once the exploration phase has completed.
You will leverage the context provided by the requesting user as well as the validation phase provided by the LDS_Guide_GraphQL rule. This tool will also provide you with a method to dynamically query the target org instance that you will use to test the generated query.
If the LDS_Guide_GraphQL rule has not been executed yet, you MUST run it first, and then get back to mutation query generation.
Mutation Queries General Information
IMPORTANT:
- Mutation Types: The GraphQL engine supports
Create,UpdateandDeleteoperations - Id Based Mutations:
UpdateandDeleteoperations operate on Id-based entity identification - Mutation Schema: Defined in the mutation query schema section
Mutation Query Generation Workflow
Strictly follow the rules below when generating the GraphQL mutation query:
- Input Fields Validation - Validate that the set of fields validate input field constraints
- Output Fields Validation - Validate that the set of fields used in the select part of the query validate the output fields constraints
- Type Consistency - Make sure variables used as query arguments and their related fields share the same GraphQL type
- Report Phase - Use the Mutation Query Report Template below to report on the previous validation phases
- Input Arguments -
inputis the default name for the argument, unless otherwise specified - Output Field - For
CreateandUpdateoperations, the output field is always namedRecord, and is of type EntityName - Query Generation - Use the mutation query template and adjust it based on the selected operation
- Output Format - Use the standalone
- Test the Query - Use the Generated Mutation Query Testing workflow to test the generated query
- Report First - Always report first, using the proper output format, before testing
Mutation Query Schema
Important: In the schema fragments below, replace EntityName occurrences by the real entity name (i.e. Account, Case...).
Important: Delete operations all share the same generic Record entity name for both input and payload, only exposing the standard Id field.
input EntityNameCreateRepresentation {
# Subset of EntityName fields here
}
input EntityNameCreateInput { EntityName: EntityNameCreateRepresentation! }
type EntityNameCreatePayload { Record: EntityName! }
input EntityNameUpdateRepresentation {
# Subset of EntityName fields here
}
input EntityNameUpdateInput { Id: IdOrRef! EntityName: EntityNameUpdateRepresentation! }
type EntityNameUpdatePayload { Record: EntityName! }
input RecordDeleteInput { Id: IdOrRef! }
type RecordDeletePayload { Id: ID }
type UIAPIMutations {
EntityNameCreate(input: EntityNameCreateInput!): EntityNameCreatePayload
EntityNameDelete(input: RecordDeleteInput!): RecordDeletePayload
EntityNameUpdate(input: EntityNameUpdateInput!): EntityNameUpdatePayload
}
Mutation Queries Input Field Constraints
CreateMutation Queries:- MUST include all required fields
- MUST only include createable fields
- Child relationships can't be set and MUST be excluded
- Fields with type
REFERENCEcan only be assigned IDs through theirApiNamename
UpdateMutation Queries:- MUST include the id of the entity to update
- MUST only include updateable fields
- Child relationships can't be set and MUST be excluded
- Fields with type
REFERENCEcan only be assigned IDs through theirApiNamename
DeleteMutation Queries:- MUST include the id of the entity to delete
Mutation Queries Output Field Constraints
CreateandUpdateMutation Queries:- MUST exclude all child relationships
- MUST exclude all
REFERENCEfields, unless accessed through theirApiNamemember (no navigation to referenced entity) - Inaccessible fields will be reported as part of the
errorsattribute in the returned payload - Child relationships CAN'T be queried as part of a mutation
- Fields with type
REFERENCEcan only be queried through theirApiName(no referenced entities navigation, no sub fields)
DeleteMutation Queries:- MUST only include the
Idfield
- MUST only include the
Mutation Query Report Template
Input arguments:
- Required fields: FieldName1 (Type1), FieldName2 (Type2)...
- Other fields: FieldName3 (Type3)... Output fields: FieldNameA (TypeA), FieldNameB (TypeB)...
Mutation Query Templates
mutation mutateEntityName(
# arguments
) {
uiapi {
EntityNameOperation(input: {
# the following is for `Create` and `Update` operations only
EntityName: {
# Input fields
}
# the following is for `Update` and `Delete` operations only
Id: ... # id here
}) {
# the following is for `Create` and `Update` operations only
Record {
# Output fields
}
# the following is for `Delete` operations only
Id: ... # id here
}
}
}
Mutation Standalone (Default) Output Format - CLEAN CODE ONLY
import { gql } from 'api/graphql.ts';
const QUERY_NAME = gql`
mutation mutateEntity($input: EntityNameOperationInput!) {
uiapi {
EntityNameOperation(input: $input) {
# select output fields here depending on operation type
}
}
}
`;
const QUERY_VARIABLES = {
input: {
// The following is for `Create` and `Update` operations only
EntityName: {
// variables here
},
// The following is for `Update` and `Delete` operations only
Id: ... // id here
}
};
❌ DO NOT INCLUDE:
- Explanatory comments about the query
- Field descriptions
- Additional text about what the query does
- Workflow step descriptions
✅ ONLY INCLUDE:
- Raw query string
- Variables object
- Nothing else
Generated Mutation Query Testing
Triggering conditions - ALL CONDITIONS MUST VALIDATE*
- Only if the Mutation Query Generation Workflow step global status is
SUCCESSand you have a generated query - Only if the query to generate is a mutation query
- Only if non manual method was used during
LDS_Guide_GraphQLrule execution to retrieve introspection data
Workflow
- Report Step - Explain that you are able to test the query using the same method used during introspection
- You MUST report the method you will use, based on the one you used during
LDS_Guide_GraphQLrule execution
- You MUST report the method you will use, based on the one you used during
- Interactive Step - Ask the user whether they want you to test the query using the proposed method
- WAIT for the user's answer.
- Input Arguments - You MUST ask the user for the input arguments to use
- WAIT for the user's answer.
- Test Query - If the user are OK with you testing the query:
- Use the selected method to test the query
- IMPORTANT - If you use the Salesforce CLI
sf api request graphqlcommand, you will need to inject the variable values directly into the query, as this command doesn't accept variables as a parameter
- Result Analysis - Retrieve the
dataanderrorsattributes from the returned payload, and report the result of the test as one of the following options:PARTIALifdatais not an empty object, buterrorsis not an empty list - Explanation: some of the queried fields are not accessible on mutationsFAILEDifdatais an empty object - Explanation: the query is not validSUCCESSiferrorsis an empty list
- Remediation Step - If status is not
SUCCESS, use theFAILEDorPARTIALstatus handling workflows
FAILED Status Handling Workflow
The query is invalid:
- Error Analysis - Parse and categorize the specific error messages
- Root Cause Identification - Use error message to identify the root cause:
- Execution - Error contains
invalid cross reference idorentity is deleted - Syntax - Error contains
invalid syntax - Validation - Error contains
validation error - Type - Error contains
VariableTypeMismatchorUnknownType - Navigation - Error contains
is not currently available in mutation results - API Version - Query deals with updates, you're testing with Connect API and error contains
Cannot invoke JsonElement.isJsonObject()
- Execution - Error contains
- Targeted Resolution - Depending on the root cause categorization
- Execution - You're trying to update or delete an unknown/no longer available entity: either create an entity first, if you have generated the related query, or ask for a valid entity id to use
- Syntax - Update the query using the error message information to fix the syntax errors
- Validation - This field's name is most probably invalid, ask user for clarification and WAIT for the user's answer
- Type - Use the error details and GraphQL schema to correct argument's type, and adjust variables accordingly
- Navigation - Use the
PARTIALstatus handling workflow below - API Version -
Recordselection is only available with API version 64 and higher, report the issue, and try again with API version 64
- Test Again - Resume the query testing workflow with the updated query (increment and track attempt counter)
- Escalation Path - If targeted resolution fails after 2 attempts, ask for additional details and restart the entire GraphQL workflow, going again through the introspection phase
PARTIAL Status Handling Workflow
The query can be improved:
- Report the fields mentioned in the
errorslist - Explain that these fields can't be queried as part of a mutation query
- Explain that the query might be considered as failing, as it will report errors
- Offer to remove the offending fields
- WAIT for the user's answer
- If they are OK with removing the fields restart the generation workflow with the new field list