-
Notifications
You must be signed in to change notification settings - Fork 2k
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
build query string programmatically from schema #958
Comments
Why do you want to build |
@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. |
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 |
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
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. 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/ 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 |
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): |
Not trying to peddle my own wares but https://github.com/imranolas/graphql-ast-types was built for broadly the same purposes. |
I am having the same issue, did you figure out what is the workable solution? Thanks! |
I think this will help - https://github.com/atulmy/gql-query-builder |
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:
Is there a way I could build
'{ hello }'
programmatically from the schema?The text was updated successfully, but these errors were encountered: