Lezione 7: Adattare il contenuto in blocco
Questa lezione del tutorial adatta il contenuto in blocco, aggiornando il titolo, il contenuto e l'estratto di più articoli con una singola richiesta GraphQL.
Affinché questa query GraphQL funzioni, la Configurazione dello schema applicata all'endpoint deve avere le Mutazioni Annidate abilitate
La query GraphQL qui sotto recupera i dati di più articoli, esegue una ricerca e sostituzione sui campi title, content e excerpt per ciascuno di essi, li adatta come input per la mutazione, ed esporta un'unica variabile dinamica $postInputs con tutti i risultati sotto forma di dizionario, nel formato:
{
"${post ID}": {
"title": "${adapted post title}",
"excerpt": "${adapted post excerpt}"
},
// repeat for all other posts ...
}Nell'operazione mutation, ciascuna di queste voci viene poi recuperata tramite _objectProperty (usando ${post ID} come chiave) e passata come input per aggiornare l'articolo:
query TransformAndExportData(
$limit: Int! = 5,
$offset: Int! = 0,
$replaceFrom: [String!]!
$replaceTo: [String!]!
) {
posts: posts(
pagination: { limit: $limit, offset: $offset }
sort: { by: ID, order: ASC }
) {
rawTitle
rawContent
rawExcerpt
@strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
affectAdditionalFieldsUnderPos: [1, 2]
)
@deferredExport(
as: "postAdaptedSources"
type: DICTIONARY
affectAdditionalFieldsUnderPos: [1, 2]
)
}
}
query AdaptDataForMutationInput
@depends(on: "TransformAndExportData")
{
postInputs: _echo(value: $postAdaptedSources)
@underEachJSONObjectProperty(
passValueOnwardsAs: "adaptedSource",
affectDirectivesUnderPos: [1, 2, 3, 4]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawTitle"
}
},
passOnwardsAs: "adaptedTitle"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawExcerpt"
}
},
passOnwardsAs: "adaptedExcerpt"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawContent"
}
},
passOnwardsAs: "adaptedContent"
)
@applyField(
name: "_echo",
arguments: {
value: {
title: $adaptedTitle,
excerpt: $adaptedExcerpt,
contentAs: {
html: $adaptedContent
}
}
},
setResultInResponse: true
)
@export(as: "postInputs")
}
mutation UpdatePost(
$limit: Int! = 5,
$offset: Int! = 0
)
@depends(on: "AdaptDataForMutationInput")
{
adaptedPosts: posts(
pagination: { limit: $limit, offset: $offset }
sort: { by: ID, order: ASC }
) {
id
postInput: _objectProperty(
object: $postInputs,
by: { key: $__id }
) @remove
update(input: $__postInput) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
title
content
excerpt
}
}
}
}- L'estensione Field on Field fornisce la direttiva
@applyFieldche, invocata con_objectProperty, estrae le proprietà da ciascun elemento dell'oggetto JSON (passato come$adaptedSource), e poi con_echo, crea il corrispondente input JSON con quelle proprietà - Oltre ai campi di funzione, l'estensione PHP Functions via Schema fornisce anche funzionalità tramite le corrispondenti "direttive di funzione", come
@strReplaceMultiple - Quando le Direttive Multi-Campo sono abilitate, possiamo applicare una direttiva a più di un campo, indicando la posizione relativa dei campi aggiuntivi tramite l'argomento
affectAdditionalFieldsUnderPos - Quando si applica una direttiva a un campo e poi si esporta il suo valore, è necessario usare
@deferredExportal posto di@export - Quando si usano le Direttive Multi-Campo con
@export(o@deferredExport), il valore esportato è un oggetto JSON contenente tutti i campi - La mutazione
Post.updateè disponibile nello schema solo quando la funzionalità Mutazioni Annidate è abilitata