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.

[Discussion] Allow substitution in graphql string templates

See original GitHub issue

I’m new to graphql, but I find a lot of my queries are very long and repetitive, and being able to interpolate repeated sections would make my code a lot easier to read and update.

Here’s one section of graphql I have on one of my templates:

      # SUBNAV PROPERTIES - CATEGORY
      overviewNavTitle
      categorySlug: slug
      subcategories {
        description: navDescription
        id
        slug
        title: navTitle
      }

      # SUBNAV PROPERTIES - NESTED ARTICLE
      parentCategory: contentpage {
        categoryTitle: navTitle
        categoryDescription: navDescription
        overviewNavTitle
        categorySlug: slug
        subcategories {
          description: navDescription
          id
          slug
          title: navTitle
        }
      }

I wish I could write it like this:

      # SUBNAV PROPERTIES - CATEGORY
      ${commonNavProps}

      # SUBNAV PROPERTIES - NESTED ARTICLE
      parentCategory: contentpage {
        categoryTitle: navTitle
        categoryDescription: navDescription
        ${commonNavProps}
      }

But I get an error triggered by this code: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/babel-plugin-extract-graphql.js#L10

This code does not run on queries in my gatsby-node file, so I’ve been taking advantage of interpolation there and it’s made things a lot easier.

I tried to look into a solution for this myself, but I’m a bit out of my element dealing with babel ASTs, so I thought I’d pause and see:

  • Is this something other people are interested in? (Maybe even people who know how to fix the issue?)
  • Is there a way to get around the problem? My best idea so far is to do more of my queries in gatsby-node and send the results to my templates using templateContext, but that feels wrong 😕

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
sarahatworkcommented, Dec 12, 2017

Thanks @calcsam. I had tried those before (and I’m using inline fragments elsewhere) but I guess I had the formatting wrong because I couldn’t get them to work. Your comment inspired me to try them again and now I have it working. Fragments are more flexible than I thought. I guess that solves the issue!

2reactions
sarahatworkcommented, Dec 12, 2017

Yeah in my case it wound up being:

export const pageQuery = graphql`
  fragment commonNavProps on ContentfulContentPage {
    overviewNavTitle
    categorySlug: slug
    subcategories {
      description: navDescription
      id
      slug
      title: navTitle
    }
  }

  query contentPageQuery($id: String!) {
    contentfulContentPage(id: { eq: $id }) {
      # SUBNAV PROPERTIES - CATEGORY
      ...commonNavProps

      # SUBNAV PROPERTIES - NESTED ARTICLE
      parentCategory: contentpage {
        categoryTitle: navTitle
        categoryDescription: navDescription
        ...commonNavProps
      }
  }
`;

Still can’t do everything I want, like a fragment containing inline fragments, but still reducing some repetition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use GraphQL Vars In regex - Discuss Dgraph
I Want to Do Use GQL variables in DQL query inside a regex filter What I Did query find_user($term: string){ find_user(func: type(User)) ...
Read more >
GraphQL variables in simple terms - LogRocket Blog
In GraphQL, you can use variables to reuse the same query/mutations written by the client, with different arguments. To get started, let's look ......
Read more >
Queries and Mutations - GraphQL
On this page, you'll learn in detail about how to query a GraphQL server. Fields#. At its simplest, GraphQL is about asking for...
Read more >
Deep-dive into GraphQL in JMeter - QAInsights
But I have to substitute an integer value. If I use “”, it is passing as a string and the request fails. Could...
Read more >
String interpolation is not allowed in graphql tag. (Gatsby)
You should use query variable instead. Variables can be added to page queries (but not static queries) through the context object that is...
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