【问题标题】:GraphQL Nested Data in Mutation突变中的 GraphQL 嵌套数据
【发布时间】:2018-08-22 19:09:55
【问题描述】:

我在 GraphQL 中获得突变工作时遇到了一些困难,其中模式中的类型包括嵌套类型。假设我有一个预订数据类型:

const BookingType = new GraphQLObjectType({
    name: 'Booking',
    fields: () => ({
        id: { type: GraphQLInt },
        Date: { type: GraphQLString },
        Venue: { type: GraphQLString }
    })
});

在架构文件中,我还有一个根突变,如下所示:

createBooking: {
  type: BookingType,
  args: {
    Date: { type: new GraphQLNonNull(GraphQLString) },
    Venue: { type: new GraphQLNonNull(GraphQLString) }
  },
  resolve(parentValue, args){
    return axios.post('http://localhost:3000/booking', args)
      .then(resp => resp.data);             
  }
}

我可以在 GraphiQL 中编写一个突变来为预订创建数据,这没问题:

mutation {
  createBooking(
    Date: "2018-03-12",
    Venue: "Some place",
  ) {
    id
    Date
    Venue
  }
}

到目前为止一切顺利。现在,我需要在原始预订对象中添加一个嵌套类型,以记录分配给预订的工作人员。所以我为工作人员添加了类型(输入和输出类型)并将它们添加到 Booking 类型和突变:

// output type
const AssignedStaffType = new GraphQLObjectType({
    name: 'AssignedStaff',
    fields: () => ({
        id: { type: GraphQLInt },
        Name: { type: GraphQLString }
    })
});

// input type
const AssignedStaffInputType = new GraphQLInputObjectType({
    name: 'AssignedStaffInput',
    fields: () => ({
        id: { type: GraphQLInt },
        Name: { type: GraphQLString }
    })
});

预订类型变为:

const BookingType = new GraphQLObjectType({
    name: 'Booking',
    fields: () => ({
        id: { type: GraphQLInt },
        Date: { type: GraphQLString },
        Venue: { type: GraphQLString },
        Staff: { type: new GraphQLList(AssignedStaffType) }
    })
});

而根突变变为:

createBooking: {
  type: BookingType,
  args: {
    Date: { type: new GraphQLNonNull(GraphQLString) },
    Venue: { type: new GraphQLNonNull(GraphQLString) },
    Staff: { type: new GraphQLList(AssignedStaffInputType) }
  },
  resolve(parentValue, args){
    return axios.post('http://localhost:3000/booking', args)
      .then(resp => resp.data);             
  }
}

我不知道现在如何在 GraphiQL 中制定突变,特别是使用什么作为 Staff 的值:

mutation {
  createBooking(
    Date: "2018-03-14",
    Venue: "Some place",
    Staff: // ??? <--- What goes here??
  ) {
    id
    Venue
    Date
    Staff
  }
}

我尝试给它一个对象,或者一个与 AssignedStaffInputType 具有相同结构的对象数组,但我只是得到一个错误('expecting AssignedStaffInputType')。客户端(在本例中为 GraphiQL)对架构中定义的 AssignedStaffInputType 一无所知,所以我不明白 a)如何在客户端中使用此输入类型,或者 b)我将如何填充这样的输入所需的数据。

请帮忙!

【问题讨论】:

    标签: graphql apollo graphiql graphql-tag


    【解决方案1】:

    没关系,我想通了。事实上,我可以以正确的格式(在模式中的输入类型中指定)传递一个对象(或对象数组)并且它工作正常。我遇到问题的原因是输入类型中的一个字段的标量类型错误,这引发了错误。客户端不需要知道它看起来的模式中指定的类型。所以,上面有问题的突变其实应该是这样写的:

    mutation {
      createBooking(
        Date: "2018-03-14",
        Venue: "Some place",
        Staff: [{staffId: 1}]
      ) {
        id
        Venue
        Date
        Staff{
           Name
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2020-04-24
      • 2018-08-24
      • 2021-04-09
      • 2017-07-28
      • 2019-04-10
      • 2018-09-19
      • 2016-02-07
      相关资源
      最近更新 更多