【问题标题】:How to do a nested mutation resolver with nexus-prisma如何使用nexus-prisma做一个嵌套的变异解析器
【发布时间】:2019-09-13 09:31:03
【问题描述】:

我有以下数据模型:

type Job { 
    // ...
    example: String
    selections: [Selection!]
    // ...
}

type Selection { 
    ...
    question: String
    ...
}

我这样定义我的对象类型:

export const Job = prismaObjectType({
  name: 'Job',
  definition(t) {
    t.prismaFields([
      // ...
      'example',
      {
        name: 'selections',
      },
      // ...
    ])
  },
})

我是这样处理我的解析器的:

t.field('createJob', {
  type: 'Job',
  args: {
    // ...
    example: stringArg(),
    selections: stringArg(),
    // ...
  },
  resolve: (parent, {
    example,
    selections
  }, ctx) => {
    // The resolver where I do a ctx.prisma.createJob and connect/create with example
  },
})

所以现在在解析器中,我可以将选择作为 json 字符串接收,然后对其进行解析并与作业连接/创建。

突变看起来像这样:

mutation {
  createJob(
    example: "bla"
    selections: "ESCAPED JSON HERE"
  ){
    id
  }
}

我想知道是否有更优雅的地方可以做类似的事情:

mutation {
  createJob(
    example: "bla"
    selections: {
       question: "bla"
    }
  ){
    id
  }
}

mutation {
  createJob(
    example: "bla"
    selections(data: {
      // ...
    })
  ){
    id
  }
}

我注意到使用nexus-prisma 你可以做stringArg({list: true}) 但你不能真正做对象。

我的主要问题是最优雅的方式是进行嵌套突变还是将所有内容连接在一起。

【问题讨论】:

    标签: graphql prisma prisma-graphql nexus-prisma


    【解决方案1】:

    您可以使用inputObjectType,如文档中所示:

    export const SomeFieldInput = inputObjectType({
      name: "SomeFieldInput",
      definition(t) {
        t.string("name", { required: true });
        t.int("priority");
      },
    });
    

    确保包含类型作为您传递给makeSchematypes 的一部分。然后你可以用它来定义一个参数,比如

    args: {
      input: arg({
        type: "SomeFieldInput", // name should match the name you provided
      }),
    }
    

    现在,您的解析器可以使用参数值作为常规 JavaScript 对象,而不是字符串。如果您需要输入对象的列表,或者想要使参数成为必需,则可以使用在使用标量时提供的same options——listnullabledescription 等。

    这是一个完整的例子:

    const Query = queryType({
      definition(t) {
        t.field('someField', {
          type: 'String',
          nullable: true,
          args: {
            input: arg({
              type: "SomeFieldInput", // name should match the name you provided
            }),
          },
          resolve: (parent, { input }) => {
            return `You entered: ${input && input.name}`
          },
        })
      },
    })
    
    const SomeFieldInput = inputObjectType({
      name: "SomeFieldInput",
      definition(t) {
        t.string("name", { required: true });
      },
    });
    
    const schema = makeSchema({
      types: {Query, SomeFieldInput},
      outputs: {
        ...
      },
    });
    

    然后像这样查询它:

    query {
      someField(
        input: {
           name: "Foo"
        }
      )
    }
    

    或者使用变量:

    query($input: SomeFieldInput) {
      someField(input: $input)
    }
    

    【讨论】:

    • 这似乎正是我需要审查并可能分配赏金的东西,谢谢!文件不是很清楚还是我?谢谢你!!
    • 是的,nexus 很新,文档也很少。他们接受 PR 以对文档进行任何更正或添加:)
    • FWIW 我用更完整的示例架构更新了答案
    • 嘿!我从来没有真正检查过,因为我已经以另一种方式实现了它,我得到:Error: Expected SomeFieldInput to be a valid output type, saw GraphQLInputObjectType 任何想法?谢谢!抱歉回复晚了!
    猜你喜欢
    • 2020-02-03
    • 2019-10-12
    • 2019-12-12
    • 2012-02-21
    • 2019-06-13
    • 1970-01-01
    • 2012-05-26
    • 2019-10-18
    • 2019-08-03
    相关资源
    最近更新 更多