graphql-spec icon indicating copy to clipboard operation
graphql-spec copied to clipboard

Query all fields of a type in a simple expression [wildcard]

Open AhmadAudeh opened this issue 10 years ago • 137 comments

Let us say this is the type :

type Human {
  id: String
  name: String
  homePlanet: String
}

Then to query all the fields the query is :

query HumanNameQuery {
   Human {
     id
     name
     homeplanet
    }
 }

Should be something like this exist ??

query HumanNameQuery {
   Human {
     *
   }
 }

AhmadAudeh avatar Dec 10 '15 14:12 AhmadAudeh

I think this was left out of GraphQL on purpose. Although it seems convenient at first, it may actually lead to some maintainability troubles down the road. Consider some possibilities:

  • A field is removed.

    Let's say the database changes and Human.homeplanet doesn't exist anymore. Any query Human { * } would execute successfully, but the client may encounter an error because it depends on homeplanet, which is gone now.

    By requiring an explicit list of fields, you get notified right away that a query is invalid -- you don't have to trace the error down through the client or UI.

  • A field is added.

    Imagine that Human.friends is added, which returns a list of other Human objects. Does Human { * } include those objects? If so, any client which requests Human { * } but doesn't need friends has caused a lot of waste!

    It's better to request exactly which fields you need so that the server doesn't do any extra work. This also allows backend developers to implement resource-intensive fields without the fear that they would be requested by accident.

For those two reasons, it's better to make the client request fields explicitly!

rmosolgo avatar Dec 10 '15 15:12 rmosolgo

Yep, this was indeed deliberately omitted for the reasons @rmosolgo mentioned!

In places where we've needed things like this (a good example might be a Date type where you pretty much always want to fetch year, month, and day), we could create a fragment DateFields on Date { year, month, day } and use that everywhere that we would have used * before.

dschafer avatar Dec 10 '15 18:12 dschafer

@rmosolgo : thanks for your explanation, it makes sens. @dschafer : seems a usable solution, I will try that, thanks.

AhmadAudeh avatar Dec 11 '15 07:12 AhmadAudeh

Interesting reasoning to remove the "*". It's nice to explicitly mention the search fields but it wouldn't hurt to have an "wild card" option just to explore the fields. To me this is just limiting the possibilities with search and doesn't make sense to me.

amunna avatar Jan 04 '18 20:01 amunna

@amunna I agree that it's awkward sometimes to issue a bunch of queries to explore an API, rather than have an easy way to see a sample of all of the data. But this seems to me like an issue that should be solved in clients like GraphiQL. It could automatically expand a * to be all fields that can be called without arguments. Which, by the way, is another wrinkle. GraphQL doesn't currently assign any special meaning to fields with or without arguments, so it's a little arbitrary that introducing a wildcard in the language itself would create that difference.

acjay avatar Jan 05 '18 14:01 acjay

@rmosolgo Your point#2 (adding a field) is a red herring. If the client cares about performance, they can still be explicit in their fields. If it is just an exploratory client who doesn't care about performance, they could use "*" (were it supported). Exactly how SQL has done it for a long time.

Your point#1 has some validity, but, again, if the client really cared, they could be explicit.

MorganConrad avatar Jan 08 '18 20:01 MorganConrad

One more to disagree with the absence of the possibility of a wildcard like '*' to retrieve all fields. This feature is very useful if you are exploring an API.

fsc7 avatar Aug 03 '18 14:08 fsc7

I want a wildcard so that I can fetch a dictionary from the server. This is not exploratory, it's a real case that comes up when internationalizing a web application.

I don't know the names of the fields, so this is not solvable using fragments.. Converting the structure to an array of key:value on the server and then back into a dictionary on the client is painful when using apollo tooling, and also makes it much clearer that I'm just working around arbitrary shortcomings of GQL semantics.

query {
  user {
    firstname
    lastname
    preferredLanguage
  }
  
  languages {
    code
    label
  }
  
  strings {
    * // I want strings to be a dictionary of key:value pairs
  }
}

tomprogers avatar Aug 09 '18 21:08 tomprogers

@tomprogers for those cases you might want to model your strings field as a list of key-value pairs

juancampa avatar Aug 09 '18 21:08 juancampa

We need to return some very deep, fully hydrated, object graphs. Without this feature that task is very laborious. This could even be a deal breaker for our current project where we were planning to use GraphQL.

PSAustin avatar Aug 10 '18 17:08 PSAustin

I think this is a duplicate of: https://github.com/facebook/graphql/issues/101

The ideal solution here IMO is to introduce a way to return objects, rather than make all objects able to be queried without specifying fields.

stubailo avatar Aug 10 '18 19:08 stubailo

GraphQL currently has the property that the client is explicit in everything it queries. This suggestion would mean we lose this property. Thus, it has to be done with a lot of care: clients using * will eventually open themselves up to getting lots of data they didn't ask for. Also, the server has no way of knowing which fields might be good candidates for deprecation, if the * query is used.

You can always define scalar JSON and just return a blob of JSON in place of a query. You cannot query inside the structure, but it can be arbitrary, deeply nested objects returned from it.

Currently, the best bet is to model it with a parameter to the query:

query Q($locale : LocaleCode) {
    user(login: "foo") {
        givenName(locale: $locale)
        familyName(locale: $locale)
        fullName(locale: $locale)
    }
}

Personally, I think what is needed is some kind of "implicit parameters", so you can define an implicit binding over a Selection Set. That means, for every subquery, you have access to the implicit parameter, if it has not been overridden. In turn, you can set a locale high up the query and then have everything under in scope pick up the implicit param naturally.

This is akin to digging out a Accept-Language header from the HTTP query and stuffing it into the context of the GraphQL execution, using it as a default value.

As I've said elsewhere: GraphQL is a programming language. You send small programs to the server for it to interpret, and that language could as well contain a let-bind construct of some kind.

jlouis avatar Aug 11 '18 12:08 jlouis

@jlouis

As I've said elsewhere: GraphQL is a programming language. You send small programs to the server for it to interpret, and that language could as well contain a let-bind construct of some kind.

^ This.

I wrote about this in detail: http://artsy.github.io/blog/2018/05/08/is-graphql-the-future/

acjay avatar Aug 11 '18 17:08 acjay

you might want to model your strings field as a list of key-value pairs

@juancampa No, I don't want to model my strings that way. I thought I made that clear in my original post.

tomprogers avatar Aug 11 '18 19:08 tomprogers

It would probably be more productive to add a concrete proposal on a new issue rather than re-hashing a nearly three year old, closed thread. Especially as it sounds like there are ~3-4 different potential problems and solutions that have come up here:

  • Having "*" somehow be used for "explore available fields": I don't know what that looks like as part of the language specification (as opposed to a client utility), so I'd be interested in seeing a concrete idea around that. That idea should be careful not to compromise forwards-compatibility (i.e. as the server adds or removes fields, the response shape shouldn't change: this would indicate it should be tooling built on top of GraphQL rather than core to GraphQL's specification, I think).
  • Figuring out performance vs. developer ergonomics around a "wildcard" approach.
  • Fetching a dictionary from a server. This may or may not be best solved with a wildcard: you can use things like a "Dictionary" scalar field to do this. Additionally, there may be room to clarify in the specification how to resolve this (for example using key-value objects in a sorted array, which is isomorphic to a dictionary), but again, that seems like a different request than this issue.
  • Defining variable bindings somehow (maybe this would involve fragment variable definitions?)

Each of these all sound like interesting ideas that should be discussed! I'd be interested in digging deeper into each of these proposals, but it's difficult to do without having the discussion derailed when there are many different threads pulling apart on a closed, only-loosely-related issue.

mjmahone avatar Aug 11 '18 21:08 mjmahone

This is somehow nonsense. When we are developing and we need to inspect a table on a database, for example, we don't write the 20 columns a table may have, we use "*"? ^^ Here should be the same.

Danilo-Araujo-Silva avatar Nov 03 '18 01:11 Danilo-Araujo-Silva

Plus one. Learning that a fields wildcard isn't possible, and that the thread to request has even been closed is a disappointment. I understand the reasoning for wanting to keep it explicit, but there are times, especially when one is testing or wanting to develop quickly, that it would be no less than a god send to be able to say "just gimme all the fields". The fragment doesn't help with this use case.

What about a possible solution of a flag on the server that enables support? The default could even be not to accept if you wanted to keep it strict.

unleashit avatar Dec 22 '18 01:12 unleashit

I was searching for a wildcard option as well, sad it isn't yet available.

Jaikant avatar Dec 22 '18 07:12 Jaikant

To be honest, you can make fragments work which leads to better organization of your code. 🤷‍♂️

brian-1901 avatar Dec 24 '18 01:12 brian-1901

No need to lecture friend. Sometimes better organization isn't the goal. Sometimes speed is. Read my comment again. If you feel like over engineering should be enforced on everyone and for all use cases, you have the right to your opinion but we disagree.

unleashit avatar Dec 24 '18 01:12 unleashit