4 ms·
I’m a big fan of graphql and use it for all frontend work. I ran into one issue recently from a server side client use case where I needed to quickly consume a
by sbr464 8y ago
I’m a big fan of graphql and use it for all frontend work.
I ran into one issue recently from a server side client use case where I needed to quickly consume an entire endpoint. Since you need to define each field in your query, it was more time consuming to create the requests. I also see the issue if fields are added, you would miss that data without updating your query.
Typically these are benefits when doing frontend work, to limit data, but I did find it annoying when just needing to quickly dump data.
For my own servers moving forward, I’m going to be adding a _doc json field for users that need traditional rest endpoint functionality in a query.
- the_duke 8y agoI have also run into this issue. I use a hand-written generator which generates a "AllXXFields" fragment for each type automatically.
- sgrove 8y agoWe're thinking of adding a "add all" checkbox in the explorer for this exact reason - feel free to open an issue https://github.com/OneGraph/graphiql-explorer/issues https://github.com/OneGraph/graphiql-explorer/issues so we can discuss it further. There are obviously tradeoffs that come with selecting all fields by default (in particular it makes it difficult for the server devs to send thoughtful notifications only to clients who rely on specific fields when necessary), but for initially exploring it's definitely nice.
- sbr464 8y agoThinking about a solution, to stay within the "graphql way", I was thinking of doing this: 1. Send introspection queries[1] to get the schema. 2. Iterate over the results, building a query object with all of the non deprecated fields. 3. Store the results in a db or cache for later queries. 4. Send the request as normal with the full query. https://graphql.org/learn/introspection/ https://graphql.org/learn/introspection/ [1] Example introspection query: query { __schema { types { name kind } } } query { __type(name: "YourTypeName") { name kind fields { name type { name kind ofType { name kind } } description isDeprecated deprecationReason } } }
- sbr464 8y agoSaw there was a library from OpenTable[1] that generated queries from introspection: [1] https://github.com/opentable/graphql-query-generator https://github.com/opentable/graphql-query-generator
- headcanon 8y agoIf you don't want to specify all properties, or can't know them ahead of time, you can create a JSON scalar type that will return an arbitrary object (https://github.com/taion/graphql-type-json https://github.com/taion/graphql-type-json). Its of course much better to define your attributes ahead-of-time. It incurs more overhead initially but pays off immensely down the line.