Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use variables in certain queries #36

Open
RyanCross opened this issue Oct 27, 2021 · 2 comments
Open

Cannot use variables in certain queries #36

RyanCross opened this issue Oct 27, 2021 · 2 comments

Comments

@RyanCross
Copy link

Hello! This might just be me being obtuse, but I cannot get the following query to work with variables

Here is my config object with the query

{
      resolve: `gatsby-source-github-api`,
      options: {
        token: process.env.GITHUB_ACCESS_TOKEN,
        graphQLQuery: `
        {
          search(type: REPOSITORY, query: "user:$userName topic:$topicName", last: 50) {
            repos: edges {
              repo: node {
                ... on Repository {
                  url
                  openGraphImageUrl
                  name
                  description
                }
              }
            }
          }
          user(login: $userName) { 
            pinnedItems(first: 10) {
              nodes {
                ... on Repository {
                  id
                  name
                  description
                  watchers {
                    totalCount
                  }
                  languages(first: 2) {
                    nodes {
                      color
                      name
                    }
                  }
                }
              }
            }
          }
        }        
          `,
        variables: {
          userName: "RyanCross",
          topicName: "game-jam"
        }
      }
    }

The first part of the query works just fine (tested on its own), but this second piece fails
user(login: $userName) {

I believe this has to do with the surrounding quotes, if I replace it with the original value "RyanCross" the query executes fine.

Tried wrapping the variable with quotes in the template string and tried something like:

{ 
variables: {
          userName: "RyanCross",
          userName2: "\"RyanCross\""
        
}

Query still failed on build though :(. Any ideas?

@ldd
Copy link
Owner

ldd commented Oct 30, 2021

Your query is malformed, and doesn't work in the Explorer.

You are missing this on the first line:
query($userName:String!){

reference: graphql/graphiql#156 (comment)

query($userName:String!){
          search(type: REPOSITORY, query: "user:$userName topic:$topicName", last: 50) {
            repos: edges {
              repo: node {
                ... on Repository {
                  url
                  openGraphImageUrl
                  name
                  description
                }
              }
            }
          }
      
          user(login: $userName) { 
            pinnedItems(first: 10) {
              nodes {
                ... on Repository {
                  id
                  name
                  description
                  watchers {
                    totalCount
                  }
                  languages(first: 2) {
                    nodes {
                      color
                      name
                    }
                  }
                }
              }
            }
          }
        
}       

I'm not 100% sure if this fixes your problem, but I hope it helps!

@ldd
Copy link
Owner

ldd commented Nov 13, 2021

@RyanCross did my suggestion work? I sure hope so. I'll close this issue in about ~3 months if I don't hear back from you or anyone else, as I believe it was an issue of how to format the query rather than an actual issue with the plugin

(see my previous answer for more info)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants