Amplify-js: API KEY with Cognito and Multiple Apollo Client

Created on 28 Oct 2019  路  1Comment  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Cognito, AWS AppSync, AppsyncSDK

* What AWS Services are you utilizing? *
Cognito, AWS AppSync

* Provide additional details e.g. code snippets *
I', using Cognito Userpool. In my case our site's access 70% private query (it mean logged users can access this queries) and 30% public query.

So i used public Authorization like this


type Product @model @auth(rules: [{allow: public, provider: apiKey, operations: [read]}]) {
  id: ID!
  name: String!
  category_id: String
  description: String
  SKU: String
  displayPrice: Float!
  available: Boolean!
  images: [ProductImage] @connection(name: "ProductImages")
  categories: [ProductCat] @connection(name: "ProductCategories")
  tags: [String!]!
  reviews: [ProductReview] @connection(name: "ProductReview")
} 

but its not working because i used on client like this.


function createApolloClient(initialState) {
  const client = new AWSAppSyncClient(
    {
      url: config.aws_appsync_graphqlEndpoint,
      region: config.aws_appsync_region,
      auth: {
        type: config.aws_appsync_authenticationType,
        jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()
      }
    },
  );
  return client;
}

so i added multiple apollo client like this.

function createApolloClient(initialState) {
  const client = new AWSAppSyncClient(
    {
      url: config.aws_appsync_graphqlEndpoint,
      region: config.aws_appsync_region,
      auth: {
        type: config.aws_appsync_authenticationType,
        jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()
      }
    },
  );

  const clientPublic = new AWSAppSyncClient(
    {
      url: config.aws_appsync_graphqlEndpoint,
      region: config.aws_appsync_region,
      auth: {type: AUTH_TYPE.API_KEY, apiKey: config.aws_appsync_apiKey},
    }
  );

  if (userCheck() === true) {
    return client;
  }

  return clientPublic;
}

after this everything works fine.

My question:

  1. Why Cognito UserPool and ApiKey not working ?
  2. Do i really need multiple Apollo clients ?
  3. Any other simple way ?
question

>All comments

Hi @anarerdene, they work if you use them separately, but if you use them at the same time, you need multiple Apollo clients. A simpler way would be using Amplify Datastore instead of Apollo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romainquellec picture romainquellec  路  3Comments

guanzo picture guanzo  路  3Comments

shinnapatthesix picture shinnapatthesix  路  3Comments

benevolentprof picture benevolentprof  路  3Comments

cgarvis picture cgarvis  路  3Comments