Previewing GraphQL queries and mutations

You can preview the GraphQL queries that are used to export objects from the source environment before you execute the queries during export operation. You can also preview the import mutations and discovery queries that are used to discover what objects already exist in the destination environment before you execute the mutations during import operation.

About this task

You can refer to the following preview tasks depending on whether you are in the source or destination environment:

Previewing GraphQL queries before export operation

You can preview the GraphQL queries that are used to export objects from the source environment before you execute the queries during export operation.

Procedure

  1. Invoke the generate_queries() method on the Exporter object.
    The generate_queries() method generates the queries that are needed to export the currently selected metadata objects and the objects that are dependencies of the selected metadata object.
    exporter.generate_queries()
    When you call the export() method with the auto_resolve_deps argument set to false, the generated queries are executed to export the currently identified metadata objects. If further dependencies are identified, you need to call the generate_queries() method again to generate the queries for the newly identified dependencies.
    exporter.export(auto_resolve_deps=False)
  2. Navigate through the structure of the DeploymentPackage to review the generated queries.
    print(json.dumps(deployment_package.to_json(), indent=4)) 
    The "exportQueries" section contains the objects that are exported by a particular query. It contains a referenceId that references an object or one of its dependencies that is selected for export. The referenceId and the actual data that is exported for an object are stored under “deploymentPlan” section. For example:
    { 
     "exportSelection": {  
         "classDefinitions": [ 
                { 
                    ... 
                } 
            ], 
    ... 
        }, 
        "exportQueries": [ 
            { 
                "retrievals": [ 
                    "cd001" 
                ], 
                "query": " { cd001: admClassDefinition( ... ) { ... }" 
            }, 
    ... 
    ], 
        "deploymentPlan": { 
        ... 
        }, 
    ... 
    } 

Previewing GraphQL mutations before import operation

You can preview the import mutations and the discovery queries that are used to discover what objects already exist in the destination environment before you execute the mutations during import operation.

Procedure

  1. Invoke the generate_mutations() method on the Importer object.
    The generate_mutations() method generates the create or update mutations that are needed to import the metadata objects that you selected during export operation.
    importer.generate_mutations()
    Note: If an import operation is performed that is partially successful, the import mutations might differ each time you generate them. An object that did not exist previously may exist on a subsequent run and require an update mutation rather than a create mutation.
  2. Navigate through the structure of the discovery_queries and import_mutations data members of the Importer object to observe the generated discovery queries and preview the import mutations.
    print("\nGenerated import discovery queries:") 
    for impqry in importer.discovery_queries: 
        print("--------------------") 
        print("Objects retrieved by query:") 
        for rid in impqry.retrievals: 
            print("    " + rid) 
        print("The query:") 
        print(impqry.query) 
    print("\nGenerated import mutations:") 
    for impmut in importer.import_mutations: 
        print("--------------------") 
        print("Objects modified by mutation:") 
        for rid in impmut.modifications: 
            print("    " + rid) 
        print("Description of mutation: " + impmut.description) 
        print("The mutation:") 
        print(impmut.mutation)