Injecting multiple resources into WP-CLI

In the previous recipe, we only retrieved (and injected into WP-CLI) a single user ID. Now, let's retrieve multiple user IDs while executing a single GraphQL query.

In this GraphQL query:

  • We remove the pagination argument from the query, as to retrieve the list of all the users with Spanish locale
  • We use Multiple Query Execution to export a list of the user IDs, under dynamic variable $userIDs
  • We print the elements of this array with _arrayJoin, joining the items with a space in between, under alias spanishLocaleUserIDs
  • We execute the operation FormatAndPrintData
# This query is stored in file "find-multiple-users-with-spanish-locale.gql"
query RetrieveData {
users(
filter: {
metaQuery: {
key: "locale",
compareBy: {
stringValue: {
value: "es_[A-Z]+"
operator: REGEXP
}
}
}
}
) {
id @export(as: "userIDs", type: LIST)
name
locale: metaValue(key: "locale")
}
}

query FormatAndPrintData @depends(on: "RetrieveData") {
spanishLocaleUserIDs: _arrayJoin(
array: $userIDs,
separator: " "
)
}

🔥 Tips:

With Multiple Query Execution we can execute complex functionality within a single request, and better organize the logic by splitting the GraphQL document into a series a logical/atomic units:

  • There is no limit in how many operations can be added to the pipeline
  • Any operation can declare more than one dependency:
query SomeQuery @depends(on: ["SomePreviousOp", "AnotherPreviousOp"]) {
# ...
}
  • Any operation can depend on another operation, which itself depends on another operation, and so on:
query ExecuteFirst
# ...
}
query ExecuteSecond @depends(on: ["ExecuteFirst"]) {
# ...
}
query ExecuteThird @depends(on: ["ExecuteSecond"]) {
# ...
}
  • We can execute any of the operations in the document:

    • ?operationName=ExecuteThird executes ExecuteFirst > ExecuteSecond > ExecuteThird
    • ?operationName=ExecuteSecond executes ExecuteFirst > ExecuteSecond
    • ?operationName=ExecuteFirst executes ExecuteFirst
  • When @depends receives only one operation, it can receive a String (instead of [String]):

query ExecuteFirst
# ...
}
query ExecuteSecond @depends(on: "ExecuteFirst") {
# ...
}
  • Both query and mutation operations can depend on each other:
query GetAndExportData
# ...
}
mutation MutateData @depends(on: "GetAndExportData") {
# ...
}
query CountMutatedResults @depends(on: "MutateData") {
# ...
}
  • Dynamic variables do not need to be declared in the operation
  • Via input @export(type:) we can select the output of the data exported into the dynamic variable:
    • SINGLE (default): A single field value
    • LIST: An array containing the field value of multiple resources
    • DICTIONARY: A dictionary containing the field value of multiple resources, with key: ${resource ID} and value: ${field value}

The response to this query will be:

{
"data": {
"users": [
{
"id": 3,
"name": "Subscriber Bennett",
"locale": "es_AR"
},
{
"id": 2,
"name": "Blogger Davenport",
"locale": "es_ES"
}
],
"spanishLocaleUserIDs": "3 2"
}
}

When executing the query, the dictionary in the body of the request must indicate the name of the operation to execute ("FormatAndPrintData"):

GRAPHQL_QUERY=$(cat find-multiple-users-with-spanish-locale.gql)
GRAPHQL_BODY="{\"operationName\": \"FormatAndPrintData\", \"query\": \"$(echo $GRAPHQL_QUERY | tr '\n' ' ' | sed 's/"/\\"/g')\"}"
GRAPHQL_RESPONSE=$(curl \
-X POST \
-H "Content-Type: application/json" \
-d $GRAPHQL_BODY \
https://mysite.com/graphql/)

We must also adapt the regex (due to the new alias, the space in between the IDs, and the quotes around this string):

SPANISH_LOCALE_USER_IDS=$(echo $GRAPHQL_RESPONSE \
| grep -E -o '"spanishLocaleUserIDs\":"((\d|\s)+)"' \
| cut -d'
:' -f2- | cut -d'"' -f2- | rev | cut -d'"' -f2- | rev)

Printing the contents of variable SPANISH_LOCALE_USER_IDS, we get all the IDs, separated with a space:

echo $SPANISH_LOCALE_USER_IDS
# Response:
# 3 2

We can now inject all IDs together to the WP-CLI command (if it supports it), or iterate them and execute the command for each of them:

for USER_ID in $(echo $SPANISH_LOCALE_USER_IDS); do wp user update "$(echo $USER_ID)" --locale=fr_FR; done

Extensions referenced in this recipe permalink

(They are all included in “Application Glue & Automator” Bundle, “Content Translation” Bundle, “All Extensions” Bundle)