Gatsby: Question: Passing a string to GraphQL queries argument.

Created on 15 Feb 2018  路  2Comments  路  Source: gatsbyjs/gatsby

I would like to have createPages do something like this in my gatsby-node.js

exports.createPages = ({ boundActionCreators, graphql }) => {
  const { createPage } = boundActionCreators;
  create IndexPageA(createPage, graphql, 'A');
  create IndexPageB(createPage, graphql, 'B');
  create IndexPageC(createPage, graphql, 'C');
};

The 'A' , 'B', 'C' need to be passed on to the same graphQL query
I tried something like below, but it didn't work ...

I get the error -> GraphQLError: Variable "$indexPage" of required type "String!" was not provided.
How do I pass the string to the query when I invoke the query in gatsby-node.js?

const createIndexPage = (createPage, graphql, indexPage) => {
  return new Promise((resolve, reject) => {
...
    resolve(
      graphql(
        `
          query TrekByIndex($indexPage: String!)
          {
            allMarkdownRemark(filter: {fields: {slug: { regex: $indexPage }}}) {
              edges {
                node {
...
                }
              }
            }
          }
        `
      ).then(result => {
...
            createPage({
...
            });
      })
   );
  });
}

question or discussion

Most helpful comment

you should be able to pass second paramater to graphql function with context (values available as parameters):

graphql(`<your_query_with_variable>`, { indexPage: <value_of_variable> })

All 2 comments

you should be able to pass second paramater to graphql function with context (values available as parameters):

graphql(`<your_query_with_variable>`, { indexPage: <value_of_variable> })

@pieh Yes it works like a charm! Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andykais picture andykais  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

totsteps picture totsteps  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments