问题
How can I use Lambda functions to query the GraphQL endpoint (AppSync) in Node.js? Which GraphQL Client should I use?
I have seen AWS AppSync JavaScript SDK which seems to be for mobile app or react/frontend.
PS: I am not talking about AWS Lambda Resolvers.
回答1:
We use a simple https package to query graphql. You might need to form all the graphql queries manually in that case. If you want to use deal with automated discovery and query with objects you can apollo-fetch clients. Simple CURL works too.
All of the methods are mentioned in detail here.
https://blog.apollographql.com/4-simple-ways-to-call-a-graphql-api-a6807bcdb355
Hope it helps.
回答2:
If you use AWS_IAM or AMAZON_COGNITO_USER_POOLS as the authentication type, I think AWS AppSync JavaScript SDK is a big help, you just need to set up your code as explained here:
"use strict";
/**
* This shows how to use standard Apollo client on Node.js
*/
global.WebSocket = require('ws');
require('es6-promise').polyfill();
require('isomorphic-fetch');
// Require exports file with endpoint and auth info
const aws_exports = require('./aws-exports').default;
// Require AppSync module
const AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE;
const AWSAppSyncClient = require('aws-appsync').default;
...
Also, you must provide credentials
const AWS = require('aws-sdk');
AWS.config.update({
region: aws_exports.REGION,
credentials: new AWS.Credentials({
accessKeyId: aws_exports.AWS_ACCESS_KEY_ID,
secretAccessKey: aws_exports.AWS_SECRET_ACCESS_KEY
})
});
const credentials = AWS.config.credentials;
or just retrieve Lambda session credentials (don't forget to give permission to Lambda execute GraphQL API).
来源:https://stackoverflow.com/questions/56300077/lambda-functions-to-query-the-graphql-endpoint