[Discussion] Allow substitution in graphql string templates
See original GitHub issueI’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 usingtemplateContext
, but that feels wrong 😕
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
Yeah in my case it wound up being:
Still can’t do everything I want, like a fragment containing inline fragments, but still reducing some repetition.