【问题标题】:AWS Appsync Javascript query example and input syntaxAWS Appsync Javascript 查询示例和输入语法
【发布时间】:2020-05-01 17:42:27
【问题描述】:

我正在将 Amplify 和 Appsync 用于我正在构建的 React 应用程序。现在我正在尝试查询用户并使用 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 进行查询的示例,所以我想知道是否有人可以对此语法有所了解,提供示例,或者为我指明一个好的参考方向为了这?提前谢谢你。

【问题讨论】:

    标签: graphql aws-amplify aws-appsync


    【解决方案1】:

    它和变异例子没有太大区别,请看下面的代码:

    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);
    

    【讨论】:

    • 啊,谢谢。我不确定 variables 参数的格式。
    猜你喜欢
    • 2021-02-08
    • 2019-02-04
    • 1970-01-01
    • 2021-01-29
    • 2021-05-08
    • 2019-02-08
    • 2019-02-25
    • 2019-08-09
    • 2019-01-18
    相关资源
    最近更新 更多