awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
919 stars 266 forks source link

Feat: Lambda authorizer for apollo v3 links #658

Closed elorzafe closed 3 years ago

elorzafe commented 3 years ago

Description of changes:

Cypress integration test sample code

import {
    ApolloClient,
    InMemoryCache,
    gql,
    ApolloLink,
} from "@apollo/client/core";

import { AUTH_TYPE, createAuthLink } from 'aws-appsync-auth-link';
import { createSubscriptionHandshakeLink } from "aws-appsync-subscription-link";

describe('Lambda authorizer', () => {
    const client = new ApolloClient({
        link: ApolloLink.from([
            createAuthLink({
                url: 'https://xxxxxxxxx.appsync-api.us-west-2.amazonaws.com/graphql',
                auth: {
                    type: AUTH_TYPE.AWS_LAMBDA,
                    token: 'xxxxAuthToken'
                },
                region: 'us-west-2'
            }),

            createSubscriptionHandshakeLink({
                url: 'https://xxxxxxxxx.appsync-api.us-west-2.amazonaws.com/graphql',
                auth: {
                    type: AUTH_TYPE.AWS_LAMBDA,
                    token: "xxxxAuthToken"
                },
                region: 'us-west-2'
            })
        ]),
        cache: new InMemoryCache()
    });

    it('Query', async () => {
        const LIST_BLOGS = gql`
            query {
                listBlogs{
                items {
                    id
                    name
                }
                }
            }`;

        const { data } = await client.query({
            query: LIST_BLOGS
        })
        expect(data.listBlogs.items).to.be.instanceOf(Array);
    })

    it('Mutation', async () => {
        const blogName = `Blog ${Date.now()}`;

        const CREATE_BLOG = gql`
            mutation ($name:String!) {
                createBlog(input: {
                name: $name
                }) {
                id
                name

                }
            }`;

        const { data } = await client.mutate({
            mutation: CREATE_BLOG,
            variables: {
                name: blogName
            }
        })
        expect(data.createBlog.name).to.equal(blogName);
    })

    it('Subscription', (done) => {
        const blogName = `Blog ${Date.now()}`;

        const SUBS_BLOGS = gql`
            subscription {
            onCreateBlog {
                id
                name
            }
            }`;

        const CREATE_BLOG = gql`
            mutation ($name:String!) {
                createBlog(input: {
                name: $name
                }) {
                id
                name

                }
            }
            `;

        client.subscribe({
            query: SUBS_BLOGS
        }).subscribe({
            next: ({data}) => {
                expect(data.onCreateBlog.name).to.equal(blogName)
                done();
            }
        });

        new Promise(res => setTimeout(res, 2000)) //Waiting for subscription to be established
        .then(() => {
            client.mutate({
                mutation: CREATE_BLOG,
                variables: {
                    name: blogName
                }
            });
        })
    });
});

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.