Empty data in AWS AppSync GraphQL subscription event - graphql

I have an application that uses a GraphQL API, provided by the AWS AppSync service. It uses GraphQL subscriptions to send messages between different clients in near real time. There is a mutation pushItems that's configured with a resolver, which has a 'none' data source and forwards the request data unmodified to the subscription onItemChange.
The app hasn't been used for a couple of years, and now when I attempt to trigger a subscription event, I get an error on the subscribed client. Previously this worked without issues.
{
"data": {
"onItemChange": null
},
"errors": [
{
"message": "Cannot return null for non-nullable type: 'ID' within parent 'Item' (/onItemChange/id)",
"path": [
"onItemChange",
"id"
]
}
]
}
The error message suggests that the property id within the Item object is null, however when I send a mutation from the AWS AppSync web console with a hard-coded string for the item ID, I still get the same issue, even though the mutation response contains the correct data that should be forwarded to the subscribed client.
I've created a minimal configuration in AWS AppSync to reproduce the issue, which is detailed below. Is it possible that the AppSync service has changed the way it handles subscription data in the last few years?
GraphQL schema
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Query {
getItem(id: ID!): Item
}
type Mutation {
pushItems(
items: [ItemInput]
): [Item]}
type Subscription {
onItemChange: Item
#aws_subscribe(
mutations: [
"pushItems"
]
)
}
type Item {
id: ID!
}
input ItemInput {
id: ID!
}
Resolver mappings for mutation pushItems
Request:
{
"version" : "2017-02-28",
"payload": $util.toJson($context.arguments.items)
}
Response:
$util.toJson($context.result)
Example queries
The following queries can be used to reproduce the issue in the AWS AppSync web console. First, subscribe to onItemChange:
subscription MySubscription {
onItemChange {
id
}
}
Then (in a different browser tab) send some data to pushItems:
mutation MyMutation {
pushItems(items: [{id: "foo"}]) {
id
}
}

You are returning Set of items in the mutation so you should update subscription return type to [Item]
type Subscription {
onItemChange: [Item] <--- HERE
#aws_subscribe(mutations: ["pushItems"])
}

Related

Removing data without primary index DynamoDB

currently i am using dynamodb for storing connection id's of aws websockets, and i am using them as primary index for my documents, overall my schema looks like this
{
ID: connectionId,
userId: userId,
domainName,
stage,
}
everything is okay with this schema, just one problem, i have an sns topic that dispatches user id to this api endpoint, and i need to delete every connection with that userId, i was looking into batchWrite but it requires me to use userId as primary index rather than connectionId, i chose this schema type because it is flexible, i can easily find documents with connection id when user disconnects and delete with one command, and add it as well, is there option for me to batchwrite without primary key? second option is to transform schema as this
{
ID: userId,
connections: [
{
connectionId: connectionId,
stage,
domainName
}
],
}
which i am not so keen of, is this the only other option?
You need to change the DB schema by the following:
For the primary index
connectionId: partition key
Create global secondary index:
userId: partition key
First scenario:
When you need to delete all connections belonging to userId you need to query using userId and then run batchWrite command to delete all rows
Query using GSI:
const items = ddb.query({
TableName: "connections",
IndexName: "globalSecondaryIndexNameHere",
KeyConditionExpression: "userId = :userId",
ExpressionAttributeValues: {
":userId": "abc"
}
})
Then loop throw items and make batchWrite request to delete:
ddb.batchWrite({
RequestItems: {
"connections": [
{
DeleteRequest: {
Key: {
"connectionId": "connectionId1"
}
}
},
{
DeleteRequest: {
Key: {
"connectionId": "connectionId2"
}
}
},
// ...
]
}
})
Second scenario:
When you need to delete one row by connectionId
Delete:
ddb.deleteItem({
TableName: "connections",
Key: {
"connectionId": "connectionId1"
}
})
NOTE: I recommend using AWS AppSync instead of API Gateway, since appsync manages your connectionIds instead of saving them in DynamoDB plus many other reasons stated HERE

Updating meta fields in Shopify with GrapQL

I've never used GraphQL before so I am really lacking knowledge on how to go about this. I'm wanting to update product meta fields on Shopify and it appears this is the only way. What I've done so far is really fumbling...
My JSON is:
{
"input": {
"id": "gid://shopify/Product/749521178847",
"metafields": [
{
"id": "gid://shopify/Metafield/2223333",
"value": "Training Grounds"
}
]
}
}
I've minified this to:
{"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}
And am then using an HTTP request to:
https://MYSTORE.myshopify.com/api/2021-10/graphql.json?query={"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}
I get the error:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
I don't know if any of this is correct. If it is, I don't know if ?query= is the right variable to pass it through on.
I recommend you start using Postman, thunder client, or similar to write your graphql queries first, you will learn a lot about how graphql works and the error msgs will be a lot more useful.
To easily connect with Shopify on this stage, go to a store and create a private app, now you can use this for authenticating your API calls.
After that the Shopify graphql works on POST, you can't write your request on GET mode.
It needs to be a POST and you are missing type of operation mutation in this case and what it is.
Postman has https://www.postman.com/lively-moon-541169/workspace/purego-apis/example/16545848-bf0d1589-09b1-4ec6-ba63-a65a56b500eb examples of how to do the calls which can help you.
Also you can check GraphiQL app on shopify to test all the queries before making the programmatic queries
Updating an existing metafield:
mutation {
metafieldsSet(metafields: [
{namespace: "YOURNAMESPACE", ownerId: "gid://shopify/Customer/CUSTOMER_ID", type: "single_line_text_field", key: "YOURKEY", value: "THIS IS NEW VALUE"}
]) {
metafields {
key
value
}
userErrors {
field
message
}
}
}
Creating new metafield:
mutation {
customerUpdate(input: {
id: "gid://shopify/Customer/CUSTOMER_ID",
metafields: [
{key: "newkey", value: "some value", type: "single_line_text_field", namespace: "some namespace"},
]
}) {
userErrors {
field
message
}
}
}

How to create GraphQL Child Resolvers with Lambda

I'm trying to create a mutation that calls a child resolver in addition to the parent resolver if an optional parameter is sent in.
I'm using AWS AppSync to sent my queries to Lambda. AppSync creates and sends an AppSyncEvent to my resolver file that looks something like this:
{
"info": {
"parentTypeName": "Mutation",
"selectionSetList": [
...
],
"selectionSetGraphQL": "...",
"fieldName": "updateUser",
"variables": {}
}
}
This event gets passed to my lambda function where, based on the fieldName and parentTypeName, I call my updateUser function.
I have the below schema
schema {
query: Query
mutation: Mutation
}
type Query {
getUser(id: ID!): User
}
type Mutation {
updateUser(name: String, email: String, bookRead: BookReadInput): User
}
type User {
name: String
email: String
booksRead: [Book]
}
type Book {
title: String
author: String
}
type BookReadInput {
title: String
author: String
}
I want that if the mutation gets passed bookRead then it will know to call a child resolver called addBook besides for the regular updateUser resolver.
I've seen various articles about implementing child resolvers but I can't figure out how they can work with lambda and the way my resolvers work.
The lambda could inspect the selectionSetList and decide what to do with the BookReadInput fields.
See https://aws.amazon.com/blogs/mobile/appsync-and-the-graphql-info-object/
You could also go with pipeline resolvers to first update the user, and then add the book.
I don't think there is a way to have it automated. You need to set it up, one way or the other.

GraphQL | How to implement conditional nesting?

Please consider the following GraphQL schema:
type User {
id: ID!
events: [Event]
}
type Event {
id: ID!
user: User!
asset: Asset!
}
type Asset {
id: ID
price: Number!
name: String!
}
GraphQL is a fantastic framework for fetching nested objects, but I'm struggling to understand how conditional nesting is implemented.
Example:
I want to retrieve all events for a specific user where asset.price is greater than x.
Or
I want to retrieve all events for an asset that belongs to a list of users [].
Question: Is conditional nesting a concept in GraphQL and how is it implemented?
Side note: I use AWS AppSync and resolvers are fetching data from AWS DynamoDB.
You can define a filter/condition on any GraphQL query such as:
query {
users(permission: 'ADMIN') {
...
}
}
The permission param is passed to your resolver (say DynamoDb VTL template, Lambda etc) to be handled however you want - to GQL this is just another parameter.
You can carry this concept into nested field by creating an events resolver and you'd then call it like this:
query {
user(id: '123') {
name
events(minPrice: 200) {
nodes: {
id
eventName
eventDate
}
}
dob
...
}
}
In above case I am using a simple minPrice param but you could do more complex things such price ranges, even pass operators (eq, gt, ...). It's all irrelevant to GraphQL - all gets passed to the resolver.
How you implement that on backend depends on your setup. I use AppSync without Amplify and write my own VTL templates and build the DynamoDb request using the provided GQL fields.
Here is an SO post that shows how to create a date filter.

Is it possible for vue-apollo to return different results from the Playground?

I have a GraphQL query called myAccounts which returns an array of accounts. When I go to the Playground and call the query:
{
accounts {
email
}
}
I get this result:
"data": {
"accounts": [
{
"email": "zach#email-one.com",
},
{
"email": "zach#email-two.com",
}
]
}
However, when I am in my Component, vue-apollo returns two items in the array, but seems to overwrite the second item with the first. Here is the query (in MyAccounts.gql):
query myAccounts {
accounts: myAccounts {
email
}
}
and here is the Apollo query in the component:
import MY_ACCOUNTS_QUERY from '~/apollo/queries/MyAccounts'
...
apollo: {
accounts: {
query: MY_ACCOUNTS_QUERY,
result(data) {
console.log(JSON.stringify(data))
}
}
}
and here is what vue-apollo logs out through the result:
{
"data":{
"accounts":[
{
"email":"zach#email-one.com",
"__typename":"Account"
},
{
"email":"zach#email-one.com",
"__typename":"Account"
}
]
},
"loading":false,
"networkStatus":7,
"stale":false
}
Expected behavior
I would expect the data returned in the Playground to be identical to what vue-apollo is fetching.
Versions
vue: 2.6.10
vue-apollo: #nuxtjs/apollo: 4.0.0-rc18
Additional context
I thought the result hook would be the best way to debug, but any other suggestions gladly welcomed. I assumed that this was a bug in our code, but I cannot figure out what could be causing the repetition (and mismatch).
Apollo normalizes its cache based on the __typename and the id (or _id) field. You need to include an id or _id field in your selection set alongside email. Failing to do so results in both objects being assigned the same key. If you don't have an id field to request, you'll need to provide a custom dataIdFromObject function as shown here.
From Guillaume Chau (https://github.com/Akryum):
This is because the Apollo Client cache can't compute a different ID
for the two items, so you endup with Account:undefined (or similar)
for both. Open the Apollo devtools and look at the myAccounts key in
the cache.
Learn more:
https://www.apollographql.com/docs/react/caching/cache-configuration/

Resources