Comments (7)
To answer my own question, here's how I solved this:
Assume you have the query:
query {
companies {
id,
name
}
}
And you want to know whether the companies
fields contain id
or name
or other fields.
Use the following func
func getSelectedFields(selectionPath []string,
resolveParams graphql.ResolveParams) []string {
fields := resolveParams.Info.FieldASTs
for _, propName := range selectionPath {
found := false
for _, field := range fields {
if field.Name.Value == propName {
selections := field.SelectionSet.Selections
fields = make([]*ast.Field, 0)
for _, selection := range selections {
fields = append(fields, selection.(*ast.Field))
}
found = true
break
}
}
if !found {
return []string{}
}
}
var collect []string
for _, field := range fields {
collect = append(collect, field.Name.Value)
}
return collect
}
And invoke it like this:
getSelectedFields([]string{"companies"}, resolveParams)
// this will return []string{"id", "name"}
In case you have a "path" you want to select from, e.g.
query {
a {
b {
x,
y,
z
}
}
}
Then you'd call it like this:
getSelectedFields([]string{"a", "b"}, resolveParams)
// Returns []string{"x", "y", "z"}
For context, this problem had also been discussed here: graphql/graphql-js#19 as well as mentioned here http://pcarion.com/2015/09/26/graphql-resolve/
from graphql.
Thanks! I'm going to use this solution. But I still think the library should provide a straightforward way to access this.
from graphql.
look at https://godoc.org/github.com/graphql-go/graphql#ResolveParams
you can access graphql query variables through Args
which is map[string]interface{}
, keyed with parameter name.
from graphql.
Thanks @bsr203 but unfortunately this isn't what I'm looking for.
Yes, I can access the query variables, but what I want is to access the list of requested fields (it's the first time I use graphql so I hope my terminology is right...)
So if I had a query:
query {
companies(arg1: 5.4) {
id,
name
}
}
What you're suggesting is being able to access arg1
, which indeed is required.
But what I'm asking, on top of that, is being able to access the list of requested fields, e.g. I want to know that the user asked for the fields id
and name
but did not ask for other fields.
My motivation is that I'm sending a query to a database and in this query I request a list of fields. I want to know what fields to include in the select
statement.
from graphql.
thank you for posting the solution. I too may want to use it one day, but not there yet. cheers.
from graphql.
Just an FYI for anyone reading this, I used the same solution, and it breaks if the client uses fragments:
[interface conversion: ast.Selection is *ast.FragmentSpread, not *ast.Field]
I'll post here when I find a solution to this.
from graphql.
I've posted my modified solution here, which works for queries that include fragments: #157 (comment)
from graphql.
Related Issues (20)
- Potential goroutine leak in TestContextDeadline HOT 1
- Enums with trailing white space cause error.
- Playground
- make union input type ? it just have output union type
- With fiber HOT 1
- Printing GraphQL documents is slow HOT 1
- Bug with underscore and same name in keys from json response
- MongoDB _id field
- Printer returns invalid SDL if Block String comment contains double-quote
- When using custom scalar types, it's crucial to provide error feedback to users when issues arise with their submitted data. However, triggering exceptions within the ParseValue and ParseLiteral methods can lead to program crashes when using the graphql.Do method. This prevents the necessary error messages from being delivered to users.
- String type no longer recongised HOT 1
- Is it possible to go from `*graphql.Schema` to an ast node? HOT 1
- Error handling HOT 1
- Disabling Field Suggestions
- Is possible to typing typeConfig of graphql.Object after Object done.
- RootObjectFn request body always empty
- Unable to pass {{....}} as part of query or mutation string
- Redefining scalar ID
- How to support scalar JSON type? HOT 1
- Accessing parent from nested child resolver HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql.