问题描述
我是实施 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 (将#修改为@)