question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you use the queried GraphQL data in the <script>?

See original GitHub issue

I’m using a simple query to fetch the site name:

<static-query>
  query {
    metaData {
      siteName
      siteDescription
    }
  }
</static-query>

I want to set this name as the site title. The default template for site titles is %s - Site Title, but for this page, I only want to set it to Site Title. I would like the use the queried data so that I do not have to update it in case I change the value of siteTitle in module.exports.

This is what I tried:

<script>
export default {
  metaInfo: {
    // title: 'Site Title',  <--- not using this because it makes the title "Site Title - Site Title"
    titleTemplate: static.metaData.siteName
  },
}
</script>

But it was giving an error. I also tried putting a $ in front of static, but it returned undefined. Is it possible to do this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
bbughcommented, Mar 24, 2019

Hi @themindstorm, this is a common sticking point with Vue. this is dynamically injected into Vue instances only when the context is evaluated. To have access to “this”, the context needs to be a function. In this case, changing metaInfo from an object to a function with a return will fix your issue:

// metaInfo doesn't have access to the Vue instance
metaInfo: {
  title: 'Index',
  titleTemplate: this.$static.metaData.siteName,  // <-- "this" is undefined
},
// Use a function for metaInfo instead
metaInfo() {
  return {
    title: 'Index',
    titleTemplate: this.$static.metaData.siteName,  // <-- "this" is the Vue instance with $static
  }
},
0reactions
dargmueslicommented, Oct 26, 2019

@bbugh is there any reason to use the function-less approach? Otherwise I’d update the docs to explain the way to do it with a function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use GraphQL with Javascript – GraphQL.js tutorial
This is how we can use plain JavaScript to write GraphQL queries with GraphQL.js. Next steps. Serve GraphQL over HTTP with a Server...
Read more >
GraphQL | A query language for your API
A query language for your API — GraphQL provides a complete description of the data in your API, gives clients the power to...
Read more >
GraphQL - Query - Tutorialspoint
A GraphQL query is used to read or fetch values while a mutation is used to write or post values. In either case,...
Read more >
Querying with GraphQL - Postman Learning Center
Postman can make HTTP calls using GraphQL, an open-source data query and manipulation language for APIs, in addition to REST.
Read more >
Query record data using the GraphQL API framework
Use the @source directive in your schema script. @defer query directive. Defer processing of a GraphQL fragment until later in the query. Use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found