微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

变量 '$directions' 不能是非输入类型 '[Step!]!' GraphQL apollo 客户端

如何解决变量 '$directions' 不能是非输入类型 '[Step!]!' GraphQL apollo 客户端

我是实施 gql 的初学者,所以没有任何可能导致此问题的基本错误

我有一个通过 Netlify 函数向 FaunaDB 触发的突变,该函数返回上述错误(以下为完整错误)。将查询错误结合起来使我相信问题出在

我尝试在 apollo-client 选项中定义 typeDefs 选项,经过几轮 Shotgun 调试后,我的 client.js 目前看起来像这样

const { API_URL } = require("./constants");
const fetch = require("cross-fetch");
require("dotenv").config();

const typeDefs = gql`
  extend type Ingredient {
    name: String!
    amount: String!
    unit: String!
  }

  extend type Step {
    text: String!
    timer: Int!
  }
`;

const cleanTypeName = new ApolloLink((operation,forward) => {
  if (operation.variables) {
    const omitTypename = (key,value) => (key === "__typename" ? undefined : value);
    operation.variables = JSON.parse(JSON.stringify(operation.variables),omitTypename);
  }
  return forward(operation).map((data) => {
    return data;
  });
});

const getClient = ({ method = "POST" } = {}) => {
  const client = new ApolloClient({
    link: new HttpLink({
      cleanTypeName,uri: API_URL,fetch,headers: {
        Authorization: `Bearer ${process.env.FAUNADB_SERVER_SECRET}`,},method,}),cache: new InMemoryCache({ addTypename: false }),typeDefs,});
  return client;
};
module.exports = { getClient };

我的 Netlify 函数看起来像这样

const { CREATE_RECIPE } = require("./utils/queries");
const { getClient } = require("./utils/client");
exports.handler = async (event,context,callback) => {
  try {
    const client = getClient({ method: "PUT" });
    let { data } = await client.mutate({
      mutation: CREATE_RECIPE,variables: { ...JSON.parse(event.body) },});
    const result = data.createRecipe;
    return {
      statusCode: 200,body: JSON.stringify(result),};
  } catch (error) {
//return actual error for Now
    throw error;
    return {
      statusCode: 500,body: JSON.stringify("Something went wrong while crating the cookbook. Try again later."),};
  }
};

最后的突变看起来像这样


const CREATE_RECIPE = gql`
  mutation CreateRecipe(
    $ownerid: ID!
    $cookbookid: [ID]!
    $name: String!
    $tags: [String]!
    $rating: Int!
    $categories: [String]!
    $images: [String]!
    $directions: [Step!]!
    $ingredients: [Ingredient!]!
  ) {
    createRecipe(
      data: {
        owner: { connect: $ownerid }
        cookbooks: { connect: $cookbookid }
        name: $name
        tags: $tags
        rating: $rating
        categories: $categories
        images: $images
        directions: $directions
        ingredients: $ingredients
      }
    ) {
      name
      owner {
        email
      }
    }
  }

Fauna端相关的schema.gql是这样的

  owner: User! @relation
  cookbooks: [Cookbook!]! @relation
  name: String!
  ingredients: [Ingredient!]!
  directions: [Step!]!
  tags: [String]!
  rating: Int!
  categories: [String]!
  images: [String]
}

type Ingredient @embeded {
  name: String!
  amount: String!
  unit: String!
}

type Step @embeded {
  text: String!
  timer: Int!
}

编辑:完全错误

mutation CreateRecipe($ownerid: ID!,$cookbookid: [ID]!,$name: String!,$tags: [String]!,$rating: Int!,$categories: [String]!,$images: [String]!,$directions: [Step!]!,$ingredients: [Ingredient!]!) {
                                                                                                                                                                   ^
Variable '$ingredients' cannot be non input type '[Ingredient!]!'. (line 1,column 188):
mutation CreateRecipe($ownerid: ID!,$ingredients: [Ingredient!]!) {
                                                                                                                                                                                           ^
  ation CreateRecipe($ownerid: ID!,$ingredients: [Ingredient!]!) {
  
  iable '$ingredients' cannot be non input type '[Ingredient!]!'. (line 1,column 188):
  ation CreateRecipe($ownerid: ID!,$ingredients: [Ingredient!]!) {
  
  new ApolloError (C:\Users\phili\Documents\Projects\cookbook\node_modules\@apollo\client\errors\errors.cjs.js:31:28)
  Object.next (C:\Users\phili\Documents\Projects\cookbook\node_modules\@apollo\client\core\core.cjs.js:1043:53)
  notifySubscription (C:\Users\phili\Documents\Projects\cookbook\node_modules\zen-observable\lib\Observable.js:135:18)
  onNotify (C:\Users\phili\Documents\Projects\cookbook\node_modules\zen-observable\lib\Observable.js:179:3)
  SubscriptionObserver.next (C:\Users\phili\Documents\Projects\cookbook\node_modules\zen-observable\lib\Observable.js:235:7)
  C:\Users\phili\Documents\Projects\cookbook\node_modules\@apollo\client\utilities\utilities.cjs.js:946:68
  Array.forEach (<anonymous>)
  iterateObserveRSSafely (C:\Users\phili\Documents\Projects\cookbook\node_modules\@apollo\client\utilities\utilities.cjs.js:946:25)
  Object.next (C:\Users\phili\Documents\Projects\cookbook\node_modules\@apollo\client\utilities\utilities.cjs.js:1020:21)
  notifySubscription (C:\Users\phili\Documents\Projects\cookbook\node_modules\zen-observable\lib\Observable.js:135:18)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。