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.

Dynamic gql queries without arguments, possible?

See original GitHub issue

Is it possible to construct gql string variables with passed variables as such?:

getRules(model: string){
        
        var queryVar = gql`
		query HyperionSchemaQuery {
			rules {
				`+model+` {
				        rules
				}
			}
		}
	`;

	var rulesArray;

	this.apollo.watchQuery({
		query: queryVar
	}).subscribe(({data}) => {
		rulesArray = JSON.parse(data.rules[model].rules);
		console.log(rulesArray);
	});
}

this.getRules(“Day”); gives error:

EXCEPTION: Error in ./ProjectMain class ProjectMain - inline template:191:108 caused by: Syntax Error GraphQL (4:7) Expected Name, found EOF

3: 				rules {
4: 						
         ^

A multiline string variable, without the “gql” logs the following:

var queryVar = `
	query HyperionSchemaQuery {
		rules {
				`+model+` {
			rules
			}
		}
	}
`;

logs:

query HyperionSchemaQuery {
	rules {
			Day {
		rules
		}
	}
}

Which would be how an accepted graphql-query would look like in my case. Is there some way I could construct such a variable and pass it as the query in my watchQuery()? Iv’e tested in many different ways (to construct the gql`` string) without success and can’t find anything in the documentation or in the GitHunt example.

Issue Analytics

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

github_iconTop GitHub Comments

14reactions
kamilkisielacommented, Nov 16, 2016

this should work

gql`
  query HyperionSchemaQuery {
    rules {
      ${model} {
        rules
      }
    }
  }
`;
4reactions
stubailocommented, Nov 16, 2016

@patriknil90 I just want to warn you that this is not a good way of using GraphQL, see here: https://dev-blog.apollodata.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL query best practices
If two otherwise identical queries have different hardcoded argument values, they're considered entirely different operations by your GraphQL server's cache.
Read more >
Compose queries dynamically — gql 3 3.5.0b0 documentation
Arguments ¶. It is possible to add arguments to any field simply by calling it with the required arguments: ds.
Read more >
GraphQL dynamic query building - apollo - Stack Overflow
Most GraphQL tutorials seem to focus on static queries (e.g. where the only thing that is changing is the variables, but not the...
Read more >
Queries and Mutations - GraphQL
This is also in general a good practice for denoting which arguments in our query are expected to be dynamic - we should...
Read more >
Understanding Queries in GraphQL - DigitalOcean
Arguments serve the same purpose as your traditional query parameters or URL segments in a REST API. We can pass them into our...
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