AWS Appsync Javascript 查询示例和输入语法
AWS Appsync Javascript query example and input syntax
我正在为我正在构建的 React 应用程序使用 Amplify 和 Appsync。现在我正在尝试查询用户并使用 appsync 客户端:
const client = new AWSAppSyncClient({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: awsconfig.aws_appsync_authenticationType,
jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()
},
complexObjectsCredentials: () => Auth.currentCredentials()
});
我已经能够使用放大网站上提供的示例成功运行突变
const result = await client.mutate({
mutation: gql(createTodo),
variables: {
input: {
name: 'Use AppSync',
description: 'Realtime and Offline',
}
}
});
但是当谈到运行使用客户端进行查询时,他们提供的唯一示例是列表操作
const result = await client.query({
query: gql(listTodos)
});
他们没有提供如何通过特定 ID 进行查询的示例,所以我想知道是否有人可以对此语法进行一些说明,提供一个示例,或者为我指出一个方向很好的参考?提前谢谢你。
和mutation的例子没什么区别,请看下面的代码:
const getBlog = `query GetBlog($id: ID!) {
getBlog(id: $id) {
id
title
content
author
}
}
`;
运行 带有参数
的查询
const result = await client.query({
query: gql(getBlog),
variables: { id: '0002b432-157a-4b6a-ad67-6a8693e331d1' }
});
console.log(result.data.getBlog);
或者
const input = { id: '0002b432-157a-4b6a-ad67-6a8693e331d1' }
const result = await client.query({
query: gql(getBlog),
variables: input
});
console.log(result.data.getBlog);
我正在为我正在构建的 React 应用程序使用 Amplify 和 Appsync。现在我正在尝试查询用户并使用 appsync 客户端:
const client = new AWSAppSyncClient({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: awsconfig.aws_appsync_authenticationType,
jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()
},
complexObjectsCredentials: () => Auth.currentCredentials()
});
我已经能够使用放大网站上提供的示例成功运行突变
const result = await client.mutate({
mutation: gql(createTodo),
variables: {
input: {
name: 'Use AppSync',
description: 'Realtime and Offline',
}
}
});
但是当谈到运行使用客户端进行查询时,他们提供的唯一示例是列表操作
const result = await client.query({
query: gql(listTodos)
});
他们没有提供如何通过特定 ID 进行查询的示例,所以我想知道是否有人可以对此语法进行一些说明,提供一个示例,或者为我指出一个方向很好的参考?提前谢谢你。
和mutation的例子没什么区别,请看下面的代码:
const getBlog = `query GetBlog($id: ID!) {
getBlog(id: $id) {
id
title
content
author
}
}
`;
运行 带有参数
const result = await client.query({
query: gql(getBlog),
variables: { id: '0002b432-157a-4b6a-ad67-6a8693e331d1' }
});
console.log(result.data.getBlog);
或者
const input = { id: '0002b432-157a-4b6a-ad67-6a8693e331d1' }
const result = await client.query({
query: gql(getBlog),
variables: input
});
console.log(result.data.getBlog);