Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeepDive] GraphQL, Apollo, AWS AppSync, Fauna, etc #66

Open
0xdevalias opened this issue Apr 2, 2020 · 0 comments
Open

[DeepDive] GraphQL, Apollo, AWS AppSync, Fauna, etc #66

0xdevalias opened this issue Apr 2, 2020 · 0 comments
Labels
deep-dive A research deep dive/link dump of various things related to a topic. May eventually become a blog.

Comments

@0xdevalias
Copy link
Owner

0xdevalias commented Apr 2, 2020

GraphQL

Apollo GraphQL

AWS AppSync

AppSync is a managed service that uses GraphQL to make it easy for applications to get exactly the data they need.

Example: React + Apollo + AppSync

Note that we alias apollo-client to point to @apollo/client here because aws-appsync-subscription-link has a peerDependency on "apollo-client": "2.x":

package.json

{
  "dependencies": {
    "@apollo/client": "^3.0.0-beta.41",
    "apollo-client": "npm:@apollo/client@^3.0.0-beta.41",
    "aws-amplify": "1.1.31-preview.111",
    "aws-appsync-auth-link": "^2.0.1",
    "aws-appsync-subscription-link": "^2.0.1",
    "graphql": "^15.0.0",
    "graphql-tag": "^2.10.3",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  }
}
import React from 'react'
import ReactDOM from 'react-dom'

import { Auth, Hub } from 'aws-amplify'

import {
  ApolloProvider,
  ApolloClient,
  ApolloLink,
  InMemoryCache,
} from '@apollo/client'

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

// https://aws-amplify.github.io/docs/js/hub
Hub.listen(/.*/, ({ channel, payload }) =>
  console.debug(`[hub::${channel}::${payload.event}]`, payload)
)

// https://aws-amplify.github.io/docs/js/authentication#manual-setup
Auth.configure({
  region: process.env.REACT_APP_AUTH_REGION,
  userPoolId: process.env.REACT_APP_AUTH_USER_POOL_ID,
  userPoolWebClientId: process.env.REACT_APP_AUTH_USER_POOL_CLIENT_ID,

  // Cognito Hosted UI configuration
  oauth: {
    domain: process.env.REACT_APP_AUTH_DOMAIN,
    scope: ['email', 'profile', 'openid'],
    redirectSignIn: document.location.origin,
    redirectSignOut: document.location.origin,
    responseType: 'code',
  },
})

const getAccessToken = async () =>
  Auth.currentSession().then(session => session.getAccessToken().getJwtToken())

const url = process.env.REACT_APP_APPSYNC_URL
const region = process.env.REACT_APP_APPSYNC_REGION

const loggedInAuth = {
  type: 'AMAZON_COGNITO_USER_POOLS',
  jwtToken: getAccessToken,
}

const anonymousAuth = {
  type: 'API_KEY',
  apiKey: process.env.REACT_APP_APPSYNC_API_KEY,
}

const searchParams = new URLSearchParams(window.location.search)
const isClient = searchParams.has('client')

const auth = isClient ? loggedInAuth : anonymousAuth

// https://github.com/awslabs/aws-mobile-appsync-sdk-js#using-authorization-and-subscription-links-with-apollo-client-no-offline-support
const link = ApolloLink.from([
  createAuthLink({ url, region, auth }),
  createSubscriptionHandshakeLink({ url, region, auth }),
])

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

ReactDOM.render(
  <ApolloProvider client={apolloClient}>
    <App />
  </ApolloProvider>,
  document.getElementById('root')
)

Type GraphQL (TypeScript)

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Fauna

The database built for serverless, featuring native GraphQL

Unsorted

See Also

@0xdevalias 0xdevalias added the deep-dive A research deep dive/link dump of various things related to a topic. May eventually become a blog. label Apr 2, 2020
@0xdevalias 0xdevalias changed the title [DeepDive] GraphQL [DeepDive] GraphQL, Apollo, AWS AppSync, Fauna, etc Apr 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deep-dive A research deep dive/link dump of various things related to a topic. May eventually become a blog.
Projects
None yet
Development

No branches or pull requests

1 participant