Passing array on GraphQL - 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

Related

How to add graphql pagination on nested object

I am writing a graphql api endpoint where I have customer details as below.
{
customer_name:
customer_address:
[
{
address_1:
},
{
address_2:
},
]
}
I need to apply pagination on customer_address which is a list.
Is this possible? Or I can do it only at top level record? Please let me know what would be the best way to do it?
You can possible by using resolver like following
input PageInput{
limit: Int!
page: Int!
}
type CustomerAddressPage {
totalCount: Int!
edges: [CustomerAddress!]!
}
type CustomerAddress {
address: String
}
type Customer {
customerName: String
customerAddress(input: PageInput!): CustomerAddressPage
}
I don't know what kind of framework you use, in nestjs you can be done as follows.
#Resolver(() => Customer)
export class CustomerResolver {
#ResolveField(() => CustomerAddressPage)
customerAddress(
#Parent() customer: Customer,
#Args('input') input: PageInput,
): Promise<CustomerAddressPage> {
return {
};
}
}

Error Cannot return null for non-nullable type: 'String' within parent MyModelType' (/createMyModelType/id)

I am trying to trigger a mutation in the aws console. I have linked my resolver function to a None type data source.
However, when I define my mutation with an input type as a parameter, the error " Error Cannot return null for non-nullable type: 'String' within parent MyModelType' (/createMyModelType/id)." occurs. Everything is fine though if I replace the input type with key word arguments.
I am certain it has to do with my resolver mapping template.
Just if you're wondering why I am using a None type, I want to be able to trigger a subscription without making real database changes or mutations.
I am not sure how to make it work with input types. Here is my code for the template:
{
"version": "2017-02-28",
"payload": $util.toJson($context.args)
}
My Schema:
input CreateMyModelType5Input {
title: String
}
type Mutation {
createMyModelType5(input: CreateMyModelType5Input!): MyModelType5
}
type MyModelType5 {
id: ID!
title: String
}
type Subscription {
onCreateMyModelType5(id: ID, title: String): MyModelType5
#aws_subscribe(mutations: ["createMyModelType5"])
}
Query I am trying to run:
mutation createMyModelType($createmymodeltypeinput: CreateMyModelTypeInput!) {
createMyModelType(input: $createmymodeltypeinput) {
id
title
}
}
Query Variables for the mutation query
{
"createmymodeltype5input": {
"title": "Hello, world!"
}
}
So I have been working on passing my arguments in the graphql mutation and using the input type seemed the only straight forward way around.
However, I have been able to do it with this way:
mutation = """mutation CreateMyModelType($id: String!, $title: String!){
createMyModelType(id: $id, title: $title){
id
title
}
}
"""
input_params = {
"id": "34",
"title": "2009-04-12"
}
response = app_sync.createMyModelType(mutation, input_params)
this can be a good guide

graphql - Refer to other fields in mutation

I want to create 2 related objects, e.g. 1 Location and 1 Place where Place has a reference to Location like so:
type Location {
id: String
name: String
}
type Place {
id: String
locationId: String
}
Is it possible to do this with 1 mutation request? Currently I'm doing this with 2 separate mutation requests like below:
mutation ($locationName: String!) {
insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
}
//in another request, use the id returned from the request above
mutation ($locationId: String!) {
insert_Place(objects: {locationId: $locationId}) {
returning {
id
}
}
}
I'm aware it's possible to have multiple fields in a mutation so I could create 2 Locations in 1 mutation request like below.
mutation ($locationName: String!) {
location1: insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
location2: insert_Location(objects: {name: $locationName}) {
returning {
id
}
}
}
However if I wanted to do this to create 1 Location and 1 Place, is there a way to retrieve the created Location Id and pass it to the 2nd field to create the Place?
For future reference:
As #Xetera pointed out, because the 2 types have a foreign key relationship you can do a nested insert mutation where hasura would handle setting the foreign key value. In my case it would look something like:
mutation ($locationName: String!) {
insert_Place(
objects: {
Location: {data: {name: $locationName}}, //hasura will create Location and assign the id to Place.locationId
}
) {
returning {
id
}
}
}
Docs here for further reading: https://hasura.io/docs/1.0/graphql/manual/mutations/insert.html#insert-an-object-along-with-its-related-objects-through-relationships

Pass deep prop to shallow input

If I have a query user and it takes uuid but my variables are deep user.uuid can I pass uuid to the user query?
query User($user.uuid: string) {
user (uuid:$user.uuid) {
createdAt
}
}
Query Variables:
{
"user":{
"uuid": "abc"
}
}
No, the current GraphQL spec does not support referencing properties of a variable if that variable is an object. You may pass in object variables, but they may only be used where an input object is expected, for example, given a schema like:
type Mutation {
createUser(input: UserInput!): User!
}
input UserInput {
email: String!
}
we can do
mutation ($input: UserInput!) {
createUser(input: $input) {
email
}
}
However, if the expected input is a scalar:
type Mutation {
createUser(email: String!): User!
}
we must provide the scalar as a separate variable:
mutation ($email: String!) {
createUser(email: $email) {
email
}
}

Make requests for a list of objects in Graphql response

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
}
}
}
}

Resources