<p>I was hoping to write a graphql browser in go, but I can't figure out how to deal with not knowing the queries of responses in advance to create the requisite structs.</p>
<p>Am I thinking about this wrong, or am I missing an obvious solution?</p>
<hr/>**评论:**<br/><br/>sh41: <pre><p>What do you have in mind when you say a GraphQL browser? Can you describe it in more detail... do you mean like <a href="https://github.com/graphql/graphiql" rel="nofollow">Graph<em>i</em>QL</a>, or something else?</p></pre>wy100101: <pre><p>More or less exactly like GraphiQL.</p></pre>snail_34: <pre><p>Isn't GraphiQL 100% frontend?</p></pre>ematvey: <pre><p>If I understand you correctly, you're thinking about creating Go types for GraphQL schema types. It is not possible with statically typed languages like Go, unless you know schema at the compile time (then you'd be able to do some codegen).</p>
<p>Instead, you have to treat GraphQL schema types as data.</p></pre>wy100101: <pre><p>OK, that is sort of what I expected, but I wanted to make sure I wasn't missing something obvious. thanks.</p></pre>014a: <pre><p>Not 100% sure what you're asking, but:</p>
<p>All GraphQL servers support <a href="http://graphql.org/learn/introspection/" rel="nofollow">an introspection query</a> to determine the types, queries, mutations, etc that it offers.</p>
<p>Forming structs based on the generic response of a GraphQL server is impossible, even if you know all of the types ahead of time. Main reason being that queries allow you to remap field names at will. You can certainly form structs based on the responses of specific pre-compiled queries.</p></pre>wy100101: <pre><p>I was looking for something that would be able to work generically with any GraphQL server where I would just startup my client with the GraphQL endpoint and then explore the queries it supports and play with those. That feels like it needs to be able to create queries and handle responses dynamically.</p></pre>014a: <pre><p>If you're writing this because you think it doesn't exist, I'd point you to <a href="https://github.com/graphcool/graphql-playground" rel="nofollow">Graphql Playground</a> or <a href="https://github.com/skevy/graphiql-app" rel="nofollow">Graphiql-app</a>.</p>
<p>If its just for fun or practice; you're gonna have to do something like</p>
<ol>
<li><p>Load all of the types, queries, and mutation the server supports into structures so you know how to parse responses, via introspection.</p></li>
<li><p>Use map[string]interface{} and traverse it via the structures you have from (1) at runtime.</p></li>
</ol></pre>wy100101: <pre><p>OK, I kind of assumed it was something along those lines, but I wanted to make sure I was missing a language feature.</p></pre>