thanks for the awesome library!
I see a lot of example of execution, validation, but all the examples of queries use pre-built strings, e.g. from the readme:
var query = '{ hello }'
Is there a way I could build '{ hello }' programmatically from the schema?
Why do you want to build { hello } programmatically?
@robzhu i have a backend that doesn't have pre-defined types like user, blog post, etc. Instead, the application developer creates data models, which get converted to GraphQL schema, tables get created dynamically etc. For a generic data browser on the front end, I need to generate queries from data models. I can implement that myself, I just don't want to reinvent the wheel if such functionality exists.
which get converted to GraphQL schema
I need to generate queries from data models
Once you generate a GraphQL schema from the user provided data models, you can create a GraphiQL endpoint using that schema, from which your users can easily author and execute queries. Is that insufficient? In your programmatically generated queries, how are you determining which fields to include?
@mvayngrib You mean to write something like queryBuilder.hello({ fields: ['a'] }) and generate { hello { a } } ?
@robzhu
Once you generate a GraphQL schema from the user provided data models, you can create a GraphiQL endpoint using that schema, from which your users can easily author and execute queries.
true, but I want to be able to provide a generic data browser on the front end without knowing what models the users will create ahead of time
In your programmatically generated queries, how are you determining which fields to include?
think of it like browsing data excel style. The default view would use some heuristic to show a limited set of columns, e.g. the fields marked "required" in the data model, but the end user can add columns from the remaining field set, via the UI. Same for filtering/sorting.
@dotansimha
yep, something like that. Does it already exist?
I don't know of anything that does exactly what you're describing, but it can be done with an introspection query. You can take a look at how GraphiQL pulls down the schema and automatically fills out the selection if you don't specify it.
@robzhu ok, will do, thanks :)
@mvayngrib I recently added custom templates support for my code generator https://github.com/dotansimha/graphql-code-generator/
You can use the schema context and generate custom code from your schema.
I guess that the generated result should me something like that:
class Query {
constructor() {
this.fields = [];
}
hello(fields) {
this.fields.push(...fields);
return this;
}
}
I guess that generating a class for each type, and probably find an easy way to declare a selection set because it recursive and we can't just chain fields easily.
@dotansimha cool, i'll take a look :)
There is a printSchema function that does this (not sure when it was added; I just discovered it):
http://graphql.org/graphql-js/utilities/#printschema
Actually that only prints the schema itself; it doesn't generate any queries. So I guess this doesn't answer the OP.
This library could be used for the output part (but you'd have to program it to read from the schema since it's just a generic query builder):
https://github.com/wix/graphql-query-builder
Not trying to peddle my own wares but https://github.com/imranolas/graphql-ast-types was built for broadly the same purposes.
@robzhu
I am having the same issue, did you figure out what is the workable solution? Thanks!
Most helpful comment
Not trying to peddle my own wares but https://github.com/imranolas/graphql-ast-types was built for broadly the same purposes.