【问题标题】:How to create nested relationships in laravel lighthouse and vue apollo如何在 laravel lighthouse 和 vue apollo 中创建嵌套关系
【发布时间】:2020-09-29 22:27:39
【问题描述】:

我在 vue apollo 中有以下突变

mutation (
      $title: String!
      $image: String!
      $author: String!
      $description: String!
      $link: String!
      $featured: Boolean
      $category: { connect: Int! }
)
{
  createBook(
    input: {
      title: $title
      image: $image
      author: $author
      description: $description
      link: $link
      featured: $featured
      category: $category
    }
  ) {
    id
    title
    author
  }
}

我只需要在这个对象 {connect: $categoryId} 中带上 categoryId。

我还在 GitHub 上的 laravel lighthouse 上问了我的问题,但不幸的是,答案对我没有多大帮助。

mutation (
      $categoryId: Int!
) {
  createBook(
    input: {
      category: {
        connect: $categoryId
    }
  ) {}
}

如果我的问题不是很清楚,我可以在 Github 上提供完整的 repo here

更新问题就在这里

AddBook.gql

mutation(
  $title: String!
  $image: String!
  $author: String!
  $description: String!
  $link: String!
  $featured: Boolean
  $category: Int!
) {
  createBook(
    input: {
      title: $title
      image: $image
      author: $author
      description: $description
      link: $link
      featured: $featured
      category: { connect: $categoryId } // $categoryId undefined
    }
  ) {
    id
    title
    author
  }
}

我在 AddBook.vue 中使用了这个突变

 <div class="form-group">
        <ApolloQuery
          :query="require('@/graphql/queries/Categories.gql')"
          class="move-down"
        >
          <template slot-scope="{ result: { data, loading } }">
            <!-- Some content -->
            <div v-if="loading">Loading...</div>
            <select v-else v-model="categoryId">
              <option
                v-for="category of data.categories"
                :key="category.id"
                :value="category.id"
              >
                {{ category.name }}
              </option>
            </select>
          </template>
        </ApolloQuery>

在方法中


addBook() {
      this.$apollo
        .mutate({
          // Query
          mutation: addBook,
          // Parameters
          variables: {
            $title: this.title,
            $image: this.image,
            $author: this.author,
            $description: this.description,
            $link: this.link,
            $featured: this.featured,
            $categoryId: this.categoryId,  // here is the problem
          },
        })
        .then((data) => {
          // Result
          console.log(data);
        })
        .catch((error) => {
          // Error
          console.error(error);
        });
    },

提前致谢

【问题讨论】:

    标签: laravel vue-apollo laravel-lighthouse


    【解决方案1】:

    变量应如下所示:

    addBook() {
          this.$apollo
            .mutate({
              // Query
              mutation: addBook,
              // Parameters
              variables: {
                title: this.title,
                image: this.image,
                author: this.author,
                description: this.description,
                link: this.link,
                featured: this.featured,
                categoryId: this.categoryId,  // here is the problem
              },
            })
            .then((data) => {
              // Result
              console.log(data);
            })
            .catch((error) => {
              // Error
              console.error(error);
            });
        }
    

    【讨论】:

    • 它只是返回一本没有分类的新书,完全没有错误?
    • 不,它返回一个错误,因为它找不到$categoryId是什么我也上传了前端,现在你可以看看。
    • 我没有看到任何变化
    • 不工作错误:“GraphQL 错误:变量“$categoryId”类型为“Int!”,用于预期类型“ID”的位置。”我觉得需要身份证!也许
    • 检查 git,提供公关
    猜你喜欢
    • 2020-04-20
    • 2020-08-11
    • 1970-01-01
    • 2021-08-05
    • 2018-06-03
    • 1970-01-01
    • 2014-10-27
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多