未提供 graphql 端点
Posted
技术标签:
【中文标题】未提供 graphql 端点【英文标题】:No graphql endpoint provided 【发布时间】:2021-05-08 18:19:12 【问题描述】:我在使用来自 AWS 的 graphQL 时遇到问题。
我可以成功登录并使用 Cognito 确认用户,正如我在控制台中看到的那样。
使用 Cognito 登录:
async signIn(email, password)
try
//sign in the user
const user = await Auth.signIn(email, password);
AlertHelper.show('info', 'yups', user);
//If not a user then display message
catch (error)
AlertHelper.show("warn", "User not found", "Please create an account");
返回一条成功消息,然后我调用另一个函数来调用 graphQL api:
async check()
try
const userInfo = await Auth.currentAuthenticatedUser(bypassCache:true)
console.log("User info: ",userInfo)
//If there is a user
if(userInfo)
const userData = await API.graphql(graphqlOperation(getUser));
console.log("User Data: ",userData);
catch (err)
console.log('Error: ', err)
即使用户信息正确,也会出现以下错误:
errors: [ [GraphQLError: No graphql endpoint provided.] ]
这是我的 AWS 导出:
const awsmobile =
"aws_project_region": "eu-west-1",
"aws_cognito_identity_pool_id": "eu-west-1:xxxxxxxxxxxxxxxxxxxxxx",
"aws_cognito_region": "eu-west-1",
"aws_user_pools_id": "eu-west-1_xxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"oauth":
;
export default awsmobile;
获取用户:
export const getUser = /* GraphQL */ `
query GetUser($id: ID!)
getUser(id: $id)
id
name
coins
description
createdAt
updatedAt
`;
我把头发扯掉了,请帮忙!!
【问题讨论】:
【参考方案1】:好吧,我想通了:
-
将此添加到您的 aws-exports:
"aws_appsync_graphqlEndpoint": "https://ENDPOINT_GRAPHQL.com/graphql", “aws_appsync_region”:“eu-west-1”, “aws_appsync_authenticationType”:“AMAZON_COGNITO_USER_POOLS”,
-
这是您查询的方式:
进口:
import * as mutations from '../../../graphql/mutations';
import * as queries from '../../../graphql/queries';
功能:
async check()
try
const userInfo = await Auth.currentAuthenticatedUser(bypassCache:true)
console.log("User info: ",userInfo)
//If there is a user
if(userInfo)
const oneTodo = await API.graphql( query: queries.getUser, variables: id: '111' );
console.log("one todos: ",oneTodo); // result: "data": "listTodos": "items": [/* ..... */]
catch (err)
console.log('error: ', err)
【讨论】:
【参考方案2】:在我的例子中,一个简单的 amplify pull
更新了 aws-exports.js
文件并添加了必要的 GraphQL 端点和其他必要的配置:
const awsmobile =
//...
aws_appsync_graphqlEndpoint: "https://xxx.appsync-api.ap-southeast-2.amazonaws.com/graphql",
aws_appsync_region: "ap-southeast-2",
aws_appsync_authenticationType: "API_KEY",
aws_appsync_apiKey: "xxx"
;
然后在我的前端它正常工作:
import createUserDataItems from "../graphql/mutations";
import API, Amplify from "aws-amplify";
import config from "../aws-exports";
Amplify.configure(config);
【讨论】:
感谢您的贡献!我也会试试的以上是关于未提供 graphql 端点的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Apollo Server 端点获得完整的 GraphQL 架构?