Sorry in advance if this turns out to be such a simple question. I'm just starting out with GraphQL. I went here to learn about it:
https://graphql.org/learn/queries/
It's cool because the examples are in interactive editors. Under the Arguments subheading, for example, you can add id to the query, and then the output will contain "id": "1000". What's throwing me, though, is that in the subsequent sections (e.g., Aliases) there's an argument episode that's used. But when I try and add episode to the query, it doesn't work, and it isn't in the "auto complete" list of attributes of a hero. It seems strange that you could use an argument to filter on something but not use that field in the query. See screenshot.
I totally understand this is a simple page showing trivial examples for the purpose of learning. Is it perhaps that there's just a bug in this documentation? Or is episode there and I'm just not understanding something?
Thanks!
-Brian
The query for hero is just a function that does something. Its arguments are completely made up by whoever wrote the query, and are not related to the properties of the object it returns. For example, if you have some e-commerce website, you could allow a search for all customers who have purchased a specific product like:
type Query {
customersFromProductPurchased(productId: String!): [Customer]
}
and that doesn't mean that the Customer object is going to have a property called productId.
FWIW, the hero query returns a Character type. From just the autocomplete on that page, I can tell you that the type definition for Character looks like this:
type Character {
id: ID!
name: String!
friends: [Character]
friendsConnection: FriendsConnection!
appearsIn: [Episode]!
}
Related
I have a query that looks like this:
mutation update_single_article($itm: String, $changes: roles_set_input!) {
update_roles(_set: $changes, where: {role_id: {_eq: $itm}}) {
returning {
}
}
}
I am not sure where the type roles_set_input comes from. If I change it to something else I get an error saying did you mean... with a list of different values. Where does this value come from? Is it a graphql predefined type? Was it defined somewhere? I tried searching google for this but I wasn't able to get any results probably because I am not sure what to search for.
If this value was defined somewhere is it possible to see it in Hasura?
Hasura automatically generates your GraphQL schema based on your Postgres database. You can run queries against your schema under the GraphiQL tab in the console.
You can explore the schema using either the "Explorer" panel on the left or by clicking the "Docs" link on the right. In addition to the description and return type for each field, the docs will also show any arguments available on each field, including the type for that argument.
I'm creating a project to implement GraphQL around an existing API, and I'm trying to add new Scalar types (id & title respectively) to provide structure around my query.
This is the query schema I'm aiming for:
query{
allPlaylists {
id
title
}
}
I've read the gem's documentation regarding Scalar Types, but it's very unclear where I should place new ScalarType definitions.
Here is a snippet of my query, my full repo is also saved on GitHub:
// query_type.rb
Types::QueryType = GraphQL::ObjectType.define do
name "Query"
# Add root-level fields here.
# They will be entry points for queries on your schema.
field :allPlaylists, types.String do
description "Playlists that belong to the user"
resolve ->(obj, args, ctx) {
Playlist.all.map { |x| x }
}
end
end
As you can see, I currently only have the allPlaylists root-level query defined.
Though the documentation is fairly unclear about this, I do plan to submit a PR to help clarify this once I strike a solution. Alternatively, if there are any working examples I can refer to it'd be very much welcomed.
Per some clarification from the gem contributors— defining an Object Type is the correct tool for this job, rather than a Scaler Type.
I have a GraphQl API for listing a bunch of items, and I can query it perfectly etc.
But now I'd like to query for a subset of that list where one property can have 'all possible values except one specific one'.
For example, I want to query something like this:
{
items(status: !"Unwanted"){
id
}
}
That exclamation mark obviously doesn't work, but it illustrates what I am after.
Can't find any information about this online.
Does anybody know of a way to do this?
I would really hate having to enumerate all possible wanted values instead of just excluding the one unwanted value. This would be really bad design and is not scalable.
Thanks.
Use ne :
{
items(filter: {status: {ne: "Unwanted"}}){
id
}
}
If you can define the schema (implement the server) then you can add a second argument like statusExcept to the items field. Then in the resolve method check if status or statusExcept was set and deliver the items according to that.
It the server API is fixed there is afaik nothing that you can do except getting everything and filter on the client.
Using GraphQL version 4.3.2 the syntax has changed and you will need to use notIn
{
items(filter: {status: {notIn: "Unwanted"}}){
id
}
}
Pardon the naive question, but I've looked all over for the answer and all I've found is either vague or makes no sense to me. Take this example from the GraphQL spec:
query getZuckProfile($devicePicSize: Int) {
user(id: 4) {
id
name
profilePic(size: $devicePicSize)
}
}
What is the point of naming this query getZuckProfile? I've seen something about GraphQL documents containing multiple operations. Does naming queries affect the returned data somehow? I'd test this out myself, but I don't have a server and dataset I can easily play with to experiment. But it would be good if something in some document somewhere could clarify this--thus far all of the examples are super simple single queries, or are queries that are named but that don't explain why they are (other than "here's a cool thing you can do.") What benefits do I get from naming queries that I don't have when I send a single, anonymous query per request?
Also, regarding mutations, I see in the spec:
mutation setName {
setName(name: "Zuck") {
newName
}
}
In this case, you're specifying setName twice. Why? I get that one of these is the field name of the mutation and is needed to match it to the back-end schema, but why not:
mutation {
setName(name: "Zuck") {
...
What benefit do I get specifying the same name twice? I get that the first is likely arbitrary, but why isn't it noise? I have to be missing something obvious, but nothing I've found thus far has cleared it up for me.
The query name doesn't have any meaning on the server whatsoever. It's only used for clients to identify the responses (since you can send multiple queries/mutations in a single request).
In fact, you can send just an anonymous query object if that's the only thing in the GraphQL request (and doesn't have any parameters):
{
user(id: 4) {
id
name
profilePic(size: 200)
}
}
This only works for a query, not mutation.
EDIT:
As #orta notes below, the name could also be used by the server to identify a persistent query. However, this is not part of the GraphQL spec, it's just a custom implementation on top.
We use named queries so that they can be monitored consistently, and so that we can do persistent storage of a query. The duplication is there for query variables to fill the gaps.
As an example:
query getArtwork($id: String!) {
artwork(id: $id) {
title
}
}
You can run it against the Artsy GraphQL API here
The advantage is that the same query each time, not a different string because the query variables are the bit that differs. This means you can build tools on top of those queries because you can treat them as immutable.
I am confused about wiring up my own datasource to the Kendo Scheduler. I looked at their API and I'm still confused. For instance, I see it says the start and the end are required, but are they the names of the fields in my dataset? They can't be mapped to another name?
My dataset has some other details as well as a date in the format, "2016-10-20T00:00:00." Is this going to work?
Can someone tell me if the actual field names from the DB/JSON are literally the same as in Telerik's docs? For instance, my date field isn't called "Start" and End. It's something else, and I don't even have an end, and I don't have starttimezone and endtimezone, are these all needed?
Another question I have is: I'm not limited to just the fields from their documentation am I? I have a datasource that has other things, for instance, we don't have "title," we have something else. And riding off that question, I'm hoping that when I call the pop up when the user double clicks on a time/day or event, that I can customize what fields I want to appear.
The other question is: What is the "from" attribute/property for? For instance
end: { type: "date", from: "End" },
Thanks
My two cents on this:
I think the options "Title", "Start" and "End" are the ones that are always needed on the dataSource. "startTimezone" and "endTimezone" are not always needed.
I also think that the "from" attribute/property is a reference to from where you are getting that data. I mean, which field from the database guards a specific information.
You can also have your own custom fields to the model, as long as they came from a valid field from the database and as long as they have an correspondent and valid datatype value.
At last but not the least, you can also customize what fields you can make appear once you do double-click and the pop-up window shows up. For that, you must use your own template for a custom pop-up editor.
Hope this gives you some insight.
Thanks for the help. I figured out the core issue with the error. It was a matter of me tweaking my schema model.