How can i perform batch update in graphQl - graphql

I have a user schema and have a update mutation
mutation MyMutation {
updateUser(input: {Order: 47, id: "981f3cb8-a369-4f0f-8d3d-28d159a3eace"}) {
firstname
id
}
}
Now i have to update multiple list of id with their respective list of order
mutation MyMutation($id: [String!] = ["981f3cb8-a369-4f0f-8d3d-28d159a3eace","0b2cb7d2-8dd9-4b11-846c-6370d003f6f9"], $Order: [Int] =
[47,48]) {
updateUser(input: {id: $id, Order: $Order}) {
firstname
}
}
But i'm getting below error in console
{
"data": {
"updateUser": null
},
"errors": [
{
"path": [
"updateUser"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Value for field '$[key][id][S]' must be text."
}
]
}

Option 1: Use a For Loop
async function update(e) {
for (const item of array) {
try {
await API.graphql({
query: createImage,
variables: {
input: {
id: id, },
},
authMode: "AMAZON_COGNITO_USER_POOLS",
});
} catch (err) {
console.log(err)
}
}
}
}
Option Two, create a custom batch resolver:
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html
https://docs.amplify.aws/cli/graphql/examples-and-solutions/#batch-put-custom-resolver

Related

Graphql $limit variable not returning correct amount of items on AWS Appsync

Running the following query, I get two results:
query x($keyword: String = "Pulp", $limit: Int, $nexttoken: String) {
listProducts(filter: {or: [{name: {contains: $keyword}}, {description: {contains: $keyword}}]}, limit: $limit, nextToken: $nexttoken) {
items {
id
}
nextToken
}
}
Response:
{
"data": {
"listProducts": {
"items": [
{
"id": "ba8048f3-1099-4184-8e78-f0e57b521465"
},
{
"id": "335770ad-f566-4385-a861-7d0dab6c9dd8"
}
],
"nextToken": null
}
}
}
By setting $limit: Int=1, I get an empty array of items:
{
"data": {
"listProducts": {
"items": [],
"nextToken": "xxxxxx"
}
}
}
By setting $limit: Int=2, I get one item:
{
"data": {
"listProducts": {
"items": [
{
"id": "ba8048f3-1099-4184-8e78-f0e57b521465"
}
],
"nextToken": "xxxxxx"
}
}
}
In order to get all 2 two items, I need to set $limit=4. Why?

How to mutate a list of objects in an array as an argument in GraphQL completely

I cannot mutate a list of objects completely, because only the last element of the array will be mutated.
What already works perfectly is, if I put each element ({play_positions_id: ...}) in the array manually like here:
mutation CreateProfile {
__typename
create_profiles_item(data: {status: "draft", play_positions: [{play_positions_id: {id: "1"}}, {play_positions_id: {id: "2"}}]}) {
id
status
play_positions {
play_positions_id {
abbreviation
name
}
}
}
}
Output:
{
"data": {
"__typename": "Mutation",
"create_profiles_item": {
"id": "1337",
"status": "draft",
"play_positions": [
{
"play_positions_id": {
"id": "1",
"abbreviation": "RWB",
"name": "Right Wingback"
}
},
{
"play_positions_id": {
"id": "2",
"abbreviation": "CAM",
"name": "Central Attacking Midfielder"
}
}
],
}
}
}
Since you can add many of those elements, I defined a variable/argument like here
mutation CreateProfile2($cpppi: [create_profiles_play_positions_input]) {
__typename
create_profiles_item(data: {status: "draft", play_positions: $cpppi}) {
id
status
play_positions {
play_positions_id {
id
abbreviation
name
}
}
}
}
Variable object for above:
"cpppi": {
"play_positions_id": {
"id": "1"
},
"play_positions_id": {
"id": "2
}
}
Output:
{
"data": {
"__typename": "Mutation",
"create_profiles_item": {
"id": "1338",
"play_positions": [
{
"play_positions_id": {
"id": "2",
"abbreviation": "CAM",
"name": "Central Attacking Midfielder"
}
}
],
}
}
}
Schema:
input create_profiles_input {
id: ID
status: String!
play_positions: [create_profiles_play_positions_input]
}
input create_profiles_play_positions_input {
id: ID
play_positions_id: create_play_positions_input
}
input create_play_positions_input {
id: ID
abbreviation: String
name: String
}
At the last both snippets, only the last object with the id "2" will be mutated. I need these to use the defined input type from my backend.
I figured it out. I got it wrong with the brackets in the variable. Here the solution:
"cpppi": [
{
"play_positions_id": {
"id": "1"
}
},
{
"play_positions_id": {
"id": "2"
}
}
]

filtering out zero total from nested result in graphql query

I have the following query that can be run against the github graphql API
query userRepositories($cursor: String, $q: String!, $githubId: String!) {
search(query: $q, type: REPOSITORY, first: 100, after: $cursor) {
repositoryCount
pageInfo {
endCursor
startCursor
}
nodes {
... on Repository {
id
name
description
isArchived
isPrivate
nameWithOwner
url
defaultBranchRef {
target {
... on Commit {
history(first: 10, author: {id: $githubId}) {
totalCount
}
}
}
}
}
}
}
}
It returns results like this:
{
"data": {
"search": {
"repositoryCount": 103,
"pageInfo": {
"endCursor": "Y3Vyc29yOjEwMA==",
"startCursor": "Y3Vyc29yOjE="
},
"nodes": [
{
"id": "MDEwOlJlcG9zaXRvcnk2MTg1OTczOA==",
"name": "microstates",
"nameWithOwner": "thefrontside/microstates",
"url": "https://github.com/thefrontside/microstates",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 0
}
}
},
{
"id": "MDEwOlJlcG9zaXRvcnkxNTU5MTUyODc=",
"name": "effection",
"nameWithOwner": "thefrontside/effection",
"url": "https://github.com/thefrontside/effection",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 16
}
}
}
},
I am only interested in the nodes array that has a defaultBranchRef.target.history.totalCount that is greater than 0.
So I am not interested in element 0 of the nodes array but I am of element 1.
Can I filter this in graphql or do I have to do that in code outside of the query?
GraphQL can't filter an array so if the API support filter base on totalCount you can pass this filter otherwise you have to filter in your code. In JS it's very easy:
const filtered = {
...data,
search: {
...data.search,
nodes: data.search.nodes.filter(node => node.defaultBranchRef.target.history.totalCount > 0),
}
};

Incrementing a value in nested attributes in AWS Lambda and DynamoDB

This is my query to add a new field or increment a nested attribute
const params = {
TableName: process.env.DYNAMODB_GAMES_TABLE,
Key: {
id: gameId
},
UpdateExpression: 'set players.#player.#score = players.#player.#score + :s',
ExpressionAttributeNames: {
'#player': playerId,
'#score': 'score'
},
ExpressionAttributeValues: {
':s': 1
},
ReturnValues: "ALL_NEW"
};
This is the error I get
{
"message": "The document path provided in the update expression is invalid for update",
"code": "ValidationException",
"time": "2020-05-21T03:03:14.328Z",
"requestId": "Q04QEP1G3E2LAM43I04ADLM4IRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 27.814212380235393
}
My object looks like
{
"id": "09e7a690",
"players": {
"M3EDJeHtoAMCLJg": [
{
"cardId": "1",
"cardTitle": "test",
"pun": "this is a pun"
}
]
}
}

prismic graphql querying single user

I'm trying to figure out how to query a single user from graphql schema by id. I'm using the graphiql tool and I'm able to get all Users.
{
allPrismicUsers {
edges {
node {
id
data {
name
}
}
}
}
}
Outputs :
{
"data": {
"allPrismicUsers”: {
"edges": [
{
"node": {
"id": "Prismic__User__WzKFwywAABmiZk_4",
"data": {
"name": “John Doe”
}
}
},
{
"node": {
"id": "Prismic__User__WzKDZywAABmiZkYp",
"data": {
"name": “Jane Doe“
}
}
},
{
"node": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": “Cat Doe”
}
}
}
]
}
}
}
I also have prismicUser() on the schema
query {
prismicUser {
id
data {
name
}
}
}
Output:
{
"data": {
"prismicUser": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": "Cat Doe"
}
}
}
}
I'm trying to query a user based on a specific id but not sure if I'm querying the wrong way.
I tried this.
query {
prismicLocation(id: "Prismic__User__WzKDZywAABmiZkYp") {
data {
name
}
}
}
I get an error
{ "errors": [
{
"message": "Argument \"id\" has invalid value \"Prismic__User__WzKDZywAABmiZkYp\".\nExpected
\"prismicUserIdQueryString_2\", found not an object.",
"users": [
{
"line": 25,
"column": 23
}
]
} ] }
How can I call a specific user based on their id ?
According to the Gatsby GraphQL reference, Gatsby allows you to filter results by any field in GraphQL (using operators such as eq, ne, etc.)
The gatsby-source-prismic plugin provides a field called prismicId (i.e. W1syKSIAAAzdN1Jg).
Here is an example query:
{
prismicUser(prismicId:{eq:"WzKDZywAABmiZkYp"}) {
data {
name
}
}
}
But you can also query by id:
prismicUser(id:{eq:"Prismic__User__WzKDZywAABmiZkYp"})

Resources