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 apollov2 #657

Closed elorzafe closed 3 years ago

elorzafe commented 3 years ago

Description of changes:

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

Cypress integration test sample code

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

import { ApolloLink } from 'apollo-link';
import { createHttpLink } from 'apollo-link-http';
import ApolloClient from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import gql from 'graphql-tag'

const url = 'https://xxxxxxxxx.appsync-api.us-west-2.amazonaws.com/graphql';
const region = 'us-west-2';
const auth = {
    type: AUTH_TYPE.AWS_LAMBDA,
    token: 'xxxxAuthToken',
};

const httpLink = createHttpLink({ uri: url });

const link = ApolloLink.from([
    createAuthLink({ url, region, auth }),
    createSubscriptionHandshakeLink({ url, auth, region })
]);

const client = new ApolloClient({
    link,
    cache: new InMemoryCache()
})

describe('Apollo V2 links test', () => {
    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
                    }
                });
            })
    });
});