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

build query string programmatically from schema #958

Closed
mvayngrib opened this issue Jul 19, 2017 · 16 comments
Closed

build query string programmatically from schema #958

mvayngrib opened this issue Jul 19, 2017 · 16 comments

Comments

@mvayngrib
Copy link

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?

@robzhu
Copy link
Contributor

robzhu commented Jul 20, 2017

Why do you want to build { hello } programmatically?

@mvayngrib
Copy link
Author

@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.

@robzhu
Copy link
Contributor

robzhu commented Jul 20, 2017

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?

@dotansimha
Copy link
Member

dotansimha commented Jul 20, 2017

@mvayngrib You mean to write something like queryBuilder.hello({ fields: ['a'] }) and generate { hello { a } } ?

@mvayngrib
Copy link
Author

@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?

@robzhu
Copy link
Contributor

robzhu commented Jul 20, 2017

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.

@mvayngrib
Copy link
Author

@robzhu ok, will do, thanks :)

@dotansimha
Copy link
Member

@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.

@mvayngrib
Copy link
Author

@dotansimha cool, i'll take a look :)

@mbrowne
Copy link

mbrowne commented Nov 17, 2017

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

@mbrowne
Copy link

mbrowne commented Nov 17, 2017

Actually that only prints the schema itself; it doesn't generate any queries. So I guess this doesn't answer the OP.

@mbrowne
Copy link

mbrowne commented Nov 20, 2017

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

@imranolas
Copy link

Not trying to peddle my own wares but https://github.com/imranolas/graphql-ast-types was built for broadly the same purposes.

@leebyron leebyron closed this as completed Dec 7, 2017
@Mrcyberwarri0r
Copy link

@miaomiaoyy
Copy link

@robzhu

I am having the same issue, did you figure out what is the workable solution? Thanks!

@dkhrunov
Copy link

I think this will help - https://github.com/atulmy/gql-query-builder

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

9 participants