Make requests for a list of objects in Graphql response - graphql

Let's say that I have the following query schema:
schema {
query: Query
}
type Query {
money(id: Long!): Money
coin(id: Long!): Coin
}
type Money {
id: Long!
coins: [Coin!]!
}
type Coin {
id: Long!
name: String!
history: [DateVal!]
}
type DateVal {
date: String!
val: Long!
}
I have two endpoints: /money and /coin, both of which take an id. When I make a request to /money I get back a response like below:
{
"id": 5,
"coins": [
{ "id" : 1, "name" : "N1", "history": null},
{ "id" : 2, "name" : "N2", "history": null},
]
}
If I call the coin endpoint with the coin id, I get the full history for the coin.
Is there a way to make GraphQL take a response like this and recursively call the /coin endpoint using the id of each coin in order to fill the history? I tried the following the following query but it didn't recursively call the coin endpoint.
{
money(id: 1) {
id
coins {
id
name
history {
date
val
}
}
}
}

Related

How to wire nested queries in Graph QL?

I would like to include the token names in results when querying the Uniswap v3 Subgraph, using the following query:
{
pools (top: 10) {
id,
feesUSD,
token0 {
id // do something like Token(id: token0.id) {symbol, name},
},
token1 {
id
}
}
}
Renders data like this:
{
"data": {
"pools": [
{
"feesUSD": "0.001849193372604300017804758202164034",
"id": "0x0001fcbba8eb491c3ccfeddc5a5caba1a98c4c28",
"token0": {
"id": "0xbef81556ef066ec840a540595c8d12f516b6378f"
},
"token1": {
"id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
}
},
The token0 and token1 hash IDs are returned and would like to return the Token.symbol values by passing in the token ID.
I only see single-level type queries on the Uniswap Subquery Examples page. How can this be accomplished?
Response from Graph QL is that this feature has been requested and is being considered.

multiple select in graphql query

I am started learn graphql and use SpaceX api (https://api.spacex.land/graphql/)
I have theese query:
{
shipsResult(
limit: 5,
offset: 0,
find: {
type: "Cargo",
home_port: "Port Canaveral"
}) {
result {
totalCount
}
data {
id
name
type
home_port
}
}
}
I need select more than one value in home_port parameter, like array ["Port Canaveral", "Port of Los Angeles"]
What i shall do?

How to adapt query to API?

I'm trying to wrap my head around GraphQL.
Right now I'm just playing with the public API of Artsy (an art website, playground at https://metaphysics-production.artsy.net). What I want to achieve is following:
I want to get all node types entities without declaring them by hand (is there a shortcut for this)?
I want every node with a field type from which I can read the type, without parsing through imageUrl etc. to fint that out.
What I constructed as of right now is this:
{
search(query: "Berlin", first: 100, page: 1, entities: [ARTIST, ARTWORK, ARTICLE]) {
edges {
node {
displayLabel
imageUrl
href
}
}
}}
Very primitive I guess. Can you guys help me?
TL;DR:
1) There is no shortcut, it's not something GraphQL offers out of the box. Nor is it something I was able to find via their Schema.
2) Their returned node of type Searchable does not contain a property for type that you're looking for. But you can access it via the ... on SearchableItem (union) syntax.
Explanation:
For question 1):
Looking at their schema, you can see that their search query has the following type details:
search(
query: String!
entities: [SearchEntity]
mode: SearchMode
aggregations: [SearchAggregation]
page: Int
after: String
first: Int
before: String
last: Int
): SearchableConnection
The query accepts an entities property of type SearchEntity which looks like this:
enum SearchEntity {
ARTIST
ARTWORK
ARTICLE
CITY
COLLECTION
FAIR
FEATURE
GALLERY
GENE
INSTITUTION
PROFILE
SALE
SHOW
TAG
}
Depending on what your usecase is, if you're constructing this query via some code, then you can find out which SearchEntity values they have:
{
__type(name: "SearchEntity") {
name
enumValues {
name
}
}
}
Which returns:
{
"data": {
"__type": {
"name": "SearchEntity",
"enumValues": [
{
"name": "ARTIST"
},
{
"name": "ARTWORK"
},
...
}
}
}
then store them in an array, omit the quotation marks from the enum and pass the array back to the original query directly as an argument.
Something along the lines of this:
query search($entities: [SearchEntity]) {
search(query: "Berlin", first: 100, page: 1, entities: $entities) {
edges {
node {
displayLabel
imageUrl
href
}
}
}
}
and in your query variables section, you just need to add:
{
"entities": [ARTIST, ARTWORK, ...]
}
As for question 2)
The query itself returns a SearchableConnection object.
type SearchableConnection {
pageInfo: PageInfo!
edges: [SearchableEdge]
pageCursors: PageCursors
totalCount: Int
aggregations: [SearchAggregationResults]
}
Digging deeper, we can see that they have edges, of type SearchableEdge - which is what you're querying.
type SearchableEdge {
node: Searchable
cursor: String!
}
and finally, node of type Searchable which contains the data you're trying to access.
Now, the type Searchable doesn't contain type:
type Searchable {
displayLabel: String
imageUrl: String
href: String
}
But, if you look at where that Searchable type is implemented, you can see SearchableItem - which contains the property of displayType - which doesn't actually exist in Searchable.
You can access the property of SearchableItem and get the displayType, like so:
{
search(query: "Berlin", first: 100, page: 1, entities: [ARTIST, ARTWORK, ARTICLE]) {
edges {
node {
displayLabel
imageUrl
href
... on SearchableItem {
displayType
}
}
}
}
}
and your result will look like this:
{
"data": {
"search": {
"edges": [
{
"node": {
"displayLabel": "Boris Berlin",
"imageUrl": "https://d32dm0rphc51dk.cloudfront.net/CRxSPNyhHKDIonwLKIVmIA/square.jpg",
"href": "/artist/boris-berlin",
"displayType": "Artist"
}
},
...

Passing array on GraphQL

I've this kind of mutation:
mutation updateProducts($input1 : ProductInput!, $id1: ID!, $input2 : ProductInput!, $id2: ID!){
p1: updateProduct(productInput: $input1, id: $id1){
product{
id
showcaseOrder
}
}
enter code here
p2: updateProduct(productInput: $input2, id: $id2){
product{
id
showcaseOrder
}
}
}
I am setting this variables:
{
"input1": {
"showcaseOrder": 2
},
"id1": "363",
"input2": {
"showcaseOrder": 1
},
"id2": "364"
}
I would like to pass an array with all information to the query instead of pass one by one.
You could define an input type for the mutation which takes all the values. Please take a look at this cheat sheet.
Or for example this post

Add or delete from connection depending on response

Essentially, I have users who can have payments, and theses payments can be filtered with an arg.
Here is my schema, simplified :
type User {
payments($filter: PaymentsFilter): PaymentsConnection,
}
enum PaymentsFilter {
MissingDetails,
}
type PaymentsConnection {
edges { ... }
pageInfo { ... }
}
type Payment {
id
description
}
The MissingDetails filter returns only the Payment who are missing a description.
For example, if I have 2 Payment :
[
{ id: 1, description: null },
{ id: 2, description: 'A great payment' },
]
A query as such :
query {
loggedUser {
payments(filter: MissingDetails) {
...
}
}
}
Would return only the first Payment, with id: 1.
I want to achieve an UpdatePaymentMutation, that would update the payment and depending on if the description is set or not in the response, I would RANGE_ADD it to the connection with the filter MissingDetails, or RANGE_DELETE it.
How can I achieve that ?

Resources