Apollo GraphQL传递对象
创始人
2024-09-09 03:00:26
0

在Apollo GraphQL中传递对象有几种解决方法,下面是其中两种常见的方法:

  1. 使用GraphQL输入对象类型(Input Object Type): 首先,定义一个输入对象类型,例如:

    input UserInput {
      name: String!
      age: Int!
    }
    

    在GraphQL API的相应查询或变异中使用这个输入对象类型:

    mutation createUser($user: UserInput!) {
      createUser(user: $user) {
        id
        name
        age
      }
    }
    

    在客户端调用Apollo Client时,可以将对象作为变量传递:

    const user = {
      name: "John",
      age: 25
    };
    
    const createUserMutation = gql`
      mutation createUser($user: UserInput!) {
        createUser(user: $user) {
          id
          name
          age
        }
      }
    `;
    
    client.mutate({
      mutation: createUserMutation,
      variables: {
        user: user
      }
    }).then(result => {
      // 处理结果
    });
    
  2. 使用GraphQL标量类型: 如果对象是GraphQL标量类型(如String、Int、Boolean等),可以直接将它们作为参数传递。例如:

    mutation createUser($name: String!, $age: Int!) {
      createUser(name: $name, age: $age) {
        id
        name
        age
      }
    }
    

    在客户端调用Apollo Client时,可以将对象的属性作为变量传递:

    const name = "John";
    const age = 25;
    
    const createUserMutation = gql`
      mutation createUser($name: String!, $age: Int!) {
        createUser(name: $name, age: $age) {
          id
          name
          age
        }
      }
    `;
    
    client.mutate({
      mutation: createUserMutation,
      variables: {
        name: name,
        age: age
      }
    }).then(result => {
      // 处理结果
    });
    

这些解决方法可以根据具体的需求和数据类型来选择使用。

相关内容

热门资讯

玩家必看(微扑克网页版)外挂透... 玩家必看(微扑克网页版)外挂透明挂辅助插件(辅助挂)其实真的有挂(2023已更新)(哔哩哔哩);科技...
发现玩家(Wepoke针对)外... 发现玩家(Wepoke针对)外挂透明挂辅助工具(辅助挂)竟然真的有挂(2020已更新)(哔哩哔哩);...
盘点一款(wepoke德州扑克... 盘点一款(wepoke德州扑克)外挂透明挂辅助神器(透视)发牌机制(确实有挂)-哔哩哔哩1、很好的工...
透视了解(cloudpoker... 透视了解(cloudpoker辅助透视)外挂透明挂辅助工具(辅助挂)其实真的有挂(2021已更新)(...
实测揭晓(微扑克wpk)外挂透... 实测揭晓(微扑克wpk)外挂透明挂辅助器安装(辅助挂)软件透明挂(有挂解密)-哔哩哔哩准备好在微扑克...
专业讨论(德扑网上设置)外挂透... 专业讨论(德扑网上设置)外挂透明挂辅助器安装(透视)透视辅助(有挂教程)-哔哩哔哩1、点击下载安装,...
7分钟了解(Wepoke德州)... 7分钟了解(Wepoke德州)外挂透明挂辅助挂(透视)原来真的有挂(2021已更新)(哔哩哔哩)1、...
实操分享!Wepoke苹果版本... 实操分享!Wepoke苹果版本(wePokE)外挂透明挂辅助app(透视)分享教程(有挂细节)-哔哩...
1分钟了解(Wepokeapp... 1分钟了解(Wepokeapp)外挂透明挂辅助工具(辅助挂)辅助透视(有挂秘笈)-哔哩哔哩;科技详细...
透明挂透视(weopke真的有... WePoker透视辅助版本解析‌,透明挂透视(weopke真的有挂)外挂透明挂辅助工具(辅助挂)德州...