相关疑难解决方法(0)

如何从使突变失败的graphql响应中删除`__typename`字段

我尝试在 GraphQL 的 Apollo 客户端中更改 addTypeName: false

apollo.create({
  link: httpLinkWithErrorHandling,
  cache: new InMemoryCache({ addTypename: false }),
  defaultOptions: {
    watchQuery: {
      fetchPolicy: 'network-only',
      errorPolicy: 'all'
    }
  }
Run Code Online (Sandbox Code Playgroud)

但它可以工作,并且会在控制台中抛出以下消息

fragmentMatcher.js:26 You're using fragments in your queries, but either don't have the addTypename:true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.Please turn on the addTypename option and include __typename when writing fragments so that Apollo Clientcan accurately match fragments.
Run Code Online (Sandbox Code Playgroud)

,

Could not …
Run Code Online (Sandbox Code Playgroud)

node.js apollo graphql apollo-client express-graphql

16
推荐指数
1
解决办法
2万
查看次数

对象:深度省略

有没有办法_.omit在嵌套对象属性上使用?

我希望这发生:

schema = {
  firstName: {
    type: String
  },
  secret: {
    type: String,
    optional: true,
    private: true
  }
};

schema = _.nestedOmit(schema, 'private');

console.log(schema);
// Should Log
// {
//   firstName: {
//     type: String
//   },
//   secret: {
//     type: String,
//     optional: true
//   }
// }
Run Code Online (Sandbox Code Playgroud)

_.nestedOmit显然不存在,只是_.omit不影响嵌套属性,但应该清楚我在寻找什么.

它也不必是下划线,但根据我的经验,它通常只会使事情变得更短更清晰.

javascript underscore.js

7
推荐指数
1
解决办法
2278
查看次数

在GraphQL中删除之前删除不必要的字段

我有一个Article在我的架构中调用的类型:

type Article {
  id: ID!
  updated: DateTime
  headline: String
  subline: String
}
Run Code Online (Sandbox Code Playgroud)

对于它的更新,有一个updateArticle(id: ID!, article: ArticleInput!)突变使用的相应输入类型:

input ArticleInput {
  headline: String
  subline: String
}
Run Code Online (Sandbox Code Playgroud)

突变本身看起来像这样:

mutation updateArticle($id: ID!, $article: ArticleInput!) {
  updateArticle(id: $id, article: $article) {
    id
    updated
    headline
    subline
  }
}
Run Code Online (Sandbox Code Playgroud)

文章始终保存为一个整体(而不是单个字段逐个),所以当我通过了一篇文章,该突变我以前牵强,它会抛出这样的错误Unknown field. In field "updated",Unknown field. In field "__typename"Unknown field. In field "id".这些有根本原因,那些字段没有在输入类型上定义.

根据规范,这是正确的行为:

(...)此无序映射不应包含任何名称未由此输入对象类型的字段定义的条目,否则应抛出错误.

现在我的问题是处理这些场景的好方法是什么.我应该将应用代码中输入类型允许的属性列入白名单吗?

如果可能的话我想避免这种情况,并且可能有一个实用程序功能将它们切换为我,它知道输入类型.但是,由于客户端不了解架构,因此必须在服务器端进行.因此,不必要的属性将转移到那里,我想这是他们不应该首先转移的原因.

有没有比白名单更好的方法?

我正在使用apollo-client,react-apollo而且graphql-server-express.

javascript graphql graphql-js react-apollo apollo-client

7
推荐指数
1
解决办法
3734
查看次数