# Embeddable fields

(This feature is not part of the GraphQL spec. It is based on the requested "composable fields" feature.)

Syntactic sugar for the PQL's composable fields: Resolve a field within an argument for another field from the same type, using syntax {{fieldName}}, and also including arguments, using {{fieldName(fieldArgs)}}.

To make it convenient to use, field echoStr(value: String): String is also added on every type of the schema.

For instance, running this query:

query {
  posts {
    description: echoStr(value: "'{{ title }}' was posted on {{ date(format: \"d/m/Y\") }}")
  }
}

...produces response:

{
  "data": {
    "posts": [
      {
        "description": "'Scheduled by Leo' was posted on 01/01/2020"
      },
      {
        "description": "'COPE with WordPress: Post demo containing plenty of blocks' was posted on 08/08/2019"
      },
      {
        "description": "'A lovely tango, not with leo' was posted on 02/08/2019"
      },
      {
        "description": "'Hello world!' was posted on 02/08/2019"
      },
      {
        "description": "'Markup: HTML Tags and Formatting' was posted on 11/01/2013"
      },
      {
        "description": "'Markup: Image Alignment' was posted on 10/01/2013"
      }
    ]
  }
}
View PQL query
query=
  posts.
    echoStr('{{ title }}' was posted on {{ date(d/m/Y) }})@description

[View query results]

Please notice that the embedded field title needs not be resolved in advance in the query; the GraphQL server already takes care of that.

Embedded fields also work within directive arguments.

For instance, running this query:

query {
  posts {
    title: echoStr(value: "({{ commentCount }}) {{ title }} - posted on {{ date(format: \"d/m/Y\")}}") @include(if: "{{ hasComments }}")
    title @skip(if: "{{ hasComments }}")
  }
}

...produces response:

{
  "data": {
    "posts": [
      {
        "title": "(1) Scheduled by Leo - posted on 01/01/2020"
      },
      {
        "title": "COPE with WordPress: Post demo containing plenty of blocks"
      },
      {
        "title": "A lovely tango, not with leo"
      },
      {
        "title": "(1) Hello world! - posted on 02/08/2019"
      },
      {
        "title": "(2) Markup: HTML Tags and Formatting - posted on 11/01/2013"
      },
      {
        "title": "Markup: Image Alignment"
      }
    ]
  }
}
View PQL query
query=
  posts.
    echoStr(({{ commentCount }}) {{ title }} - posted on {{ date(d/m/Y) }})@title<include({{ hasComments }})>|
    title<skip({{ hasComments }})>

[View query results]

# Examples

We can send a a newsletter defining the to, subject and content data through fields newsletterTo, newsletterSubject and newsletterContent from the Root type:

mutation {
  sendEmail(
    to: "{{ newsletterTo }}",
    subject: "{{ newsletterSubject }}",
    content: "{{ newsletterContent }}"
  )
}

Using nested mutations, we can send an email to several users, personalizing the subject for each, producing "Hey Joe!" and "Hey Rachel!":

mutation {
  users {
    sendEmail(
      subject: "Hey {{ name }}!",
      content: "What's up? Access to the service will expire soon!"
    )
  }
}

Adding arguments on the embedded field, we can add the service expiration date for each user in the content as {{ serviceExpirationDate(format:\"d/m/Y\") }} (the string quotes must be escaped: \"):

mutation {
  users {
    sendEmail(
      subject: "Hey {{ name }}!",
      content: "What's up? You only have until {{ serviceExpirationDate(format:\"d/m/Y\") }} to renew the service."
    )
  }
}

Using directive arguments, we can send a single email to many users, adding them all in the to field:

mutation {
  users {
    id @sendEmail(to: "{{ email }}", subject: "Congratulations team!", content: "You have won the competition!")
  }
}

# Configuration

# Environment variables

Environment variable Description Default
ENABLE_EMBEDDABLE_FIELDS Enable using embeddable fields false
Last Updated: 9/27/2020, 3:09:20 PM