0

we are currently studying the stack: cdk, appsync and amplify to migrate our applications.

In our initial tests, we were able to upload a graphql api with only appsync wit relationships and it was very smooth, nice and fast.

When testing to build with cdk, we are having difficulties to create the relationships.

Here my code:

Schema

type Person {
  id: ID!
  name: String!
}
input PersonInput {
  id: ID!
  name: String!
}
input UpdatePersonInput {
  id: ID!
  name: String
}
type Client {
  id: ID!
  type: String!
  personId: String
  # Person: PersonConnection 
  Person: Person @connection(fields: ["personId"])
}
input ClientInput {
  id: ID!
  type: String!
  personId: String!
}
input UpdateClientInput {
  id: ID!
  type: String
  personId: String
}

My function

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
async function list() {
    const params = {
        TableName: process.env.CLIENT_TABLE,
    }
    try {
        const data = await docClient.scan(params).promise()
        return data.Items
    } catch (err) {
        console.log('DynamoDB error: ', err)
        return null
    }
}
export default list;

My table

const clientTable = new dynamodb.Table(scope, 'ClientTable', {
        billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
        partitionKey: {
            name: 'id',
            type: dynamodb.AttributeType.STRING,
        },
    });
    clientTable.addGlobalSecondaryIndex({
        indexName: 'client-by-person-id',
        partitionKey: {
        name: 'personId',
        type: dynamodb.AttributeType.STRING
        },
        sortKey: {
        name: 'createdAt',
        type: dynamodb.AttributeType.STRING
        }
    })

My query

query MyQuery {
  listClients {
    id
    personId
    type
    Person {
      name
    }
  }
}

However, my return to Person connection is null

"listClients": [
      {
        "id": "1",
        "personId": "1",
        "type": "PJ",
        "Person": null
      }
    ]

I would appreciate it if could point out our mistake

Gilvaju
  • 3
  • 1

1 Answers1

0

Solution of the problem based on the response of the Thorsten.

  1. First, add resolver to the Person field in Client

    export const clientResolvers = [{ typeName: "Client", fieldName: "Person" },...]

    clientResolvers.map(((resolver: clientTypeResolver) => dataSource2.createResolver(resolver)))

  2. Map function to the Person field in its lambda function

    type AppSyncEvent = {
        ...
        source: {personId: string,}
        ...
    }
    
    exports.handler = async (event:AppSyncEvent) => {
        switch (event.info.fieldName) {
            ...
            case "Person":
            return await getPerson(event.source.personId);
        }
    }```
    
    
  3. Function to solve the person field

async function getPerson(personId: string) {
    console.log("CONTEXT\n" + JSON.stringify(personId, null, 2))
    // console.log(context.source)
    const params = {
        TableName: process.env.PERSON_TABLE,
        Key: { id: personId }
    }
    try {
        const { Item } = await docClient.get(params).promise()
        console.log("DATA\n" + JSON.stringify(Item, null, 2))
        return Item
    } catch (err) {
        console.log('DynamoDB error: ', err)
    }
Gilvaju
  • 3
  • 1