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

Default Field for type #459

Closed
BLamy opened this issue Aug 5, 2016 · 6 comments
Closed

Default Field for type #459

BLamy opened this issue Aug 5, 2016 · 6 comments

Comments

@BLamy
Copy link

BLamy commented Aug 5, 2016

I'm unsure of whether this is supported but I couldn't find it anywhere. I have a use case where I am representing a tree of values which looks a little something like this

{
  val1: {
     name: "val1",
     value: "test",
     moreMetaData: "foo"
  },
  val2: {
     name: "val2",
     value: "test",
     moreMetaData: "foo"
  }  
}

95% of the time I only care about getting the 'value' property. Unfortunately, My query most of the time look like this:

query {
   val1 {
      value
   }
   val2 {
      value
   }
}

I would like to query like this and have it just get the .value property by default.

{
   val1
   val2
}

However, If the user wants to get more metadata for a variable they can as so

{
   val1 {
      name
      moreMetaData
   }
   val2
}
@robzhu
Copy link
Contributor

robzhu commented Aug 7, 2016

Hey @BLamy, you can use a query fragment to avoid the repetition. Here's a good tutorial on fragments.

As for the exact behavior you're asking about, GraphQL queries must specify selections down to scalar fields. In your example schema, the type underlying val1/val2 is an intermediate node, so you cannot have it as the terminating field in the selection.

If you would like share more about your use case, I might be able to offer more suggestions.

@BLamy
Copy link
Author

BLamy commented Aug 8, 2016

@robzhu
I'm in a situation where I have a medium tree structure (less than 1000 nodes). Most of my users are non-technical but their job will require them to be pretty familiar with this tree structure. Currently, users must use a tag explorer application, browse through the tags, find the values they want, and enter them into a spreadsheet (they don't enjoy it). I believe that GraphQL will represent their tag explorer closely enough that non-technical people will be able to construct their own reusable queries with minimal training thus speeding up the process.

Most users don't really need to know about the metadata embedded in each field (some might even be confused by it). However, I really feel I would be doing an injustice providing an API which doesn't expose access to it for more advanced users.

It's not the end of the world if I need to tell them to always put { value } but it would have been nice if I could have avoided it. Explaining the concept of fragments to them will probably cause their mind to explode. Whereas early termination would make it conceptually simpler and semantically cleaner.

@stubailo
Copy link
Contributor

stubailo commented Aug 8, 2016

I think you can easily do this yourself by writing a query visitor that uses the schema to add fields where necessary. I think it would be a huge step back for GraphQL if it wasn't required to list all of the fields you want, since that's one of the primary benefits of the platform especially for building tools and client libraries.

@JeSuisNikhil
Copy link

JeSuisNikhil commented Aug 8, 2016

I can think of some hacks.

Make a parallel tree so that you have two root nodes. One which has children and it's fields return {value} implicitly and the other node that provides access to the "advanced" information.

You could also add an additional field to each node for each field that has a {value} e.g. val1Value. One could argue that doing this would mess with the tree structure though.

Ideal solution IMO is to define schema based on user/role. it would require you to add a user/role based check when defining the value resolver for these fields and make it so that field implicitly returns the value for non-technical roles.

@asiandrummer
Copy link
Contributor

@BLamy GraphiQL is already doing something similar - here is the reference. GraphiQL supports automatically completing a query with scalar nodes. I think this is an example of what @stubailo said in terms of writing a query visitor.

@BLamy
Copy link
Author

BLamy commented Aug 9, 2016

@asiandrummer That is actually exactly what I was looking for.

I just needed to add a value case like these special ones:
https://github.com/graphql/graphiql/blob/master/src/utility/fillLeafs.js#L83-L96

@BLamy BLamy closed this as completed Aug 9, 2016
mjmahone added a commit that referenced this issue Jun 8, 2018
Reverts #1277, and implements spec removal from #459. While it's an open question whether this validation is good, we need to provide an upgrade path for schemas that currently do not satisfy this validation rule.
mjmahone added a commit that referenced this issue Jun 8, 2018
* Allow interfaces to have no implementors
Reverts #1277, and implements spec removal from #459. While it's an open question whether this validation is good, we need to provide an upgrade path for schemas that currently do not satisfy this validation rule.

* Update test to accept unimplemented interface
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

5 participants