在GraphQL中删除之前删除不必要的字段 [英] Remove unnecessary fields before mutation in GraphQL

查看:210
本文介绍了在GraphQL中删除之前删除不必要的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的架构中有一个名为 Article 的类型:

I've got a type called Article in my schema:

type Article {
  id: ID!
  updated: DateTime
  headline: String
  subline: String
}

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

For updates to it, there's a corresponding input type that is used by a updateArticle(id: ID!, article: ArticleInput!) mutation:

input ArticleInput {
  headline: String
  subline: String
}

突变本身如下所示:

mutation updateArticle($id: ID!, $article: ArticleInput!) {
  updateArticle(id: $id, article: $article) {
    id
    updated
    headline
    subline
  }
}

文章始终保存作为一个整体(不是逐个单独的字段)所以当我将一篇文章传递给我之前获取的那个变异时,它会抛出像 Unknown字段这样的错误。在字段已更新未知字段。在字段__typename未知字段中。在字段id中。这些有根本原因,那些字段没有在输入类型上定义。

The article is always saved as a whole (not individual fields one by one) and so when I pass an article to that mutation that I've previously fetched, it throws errors like Unknown field. In field "updated", Unknown field. In field "__typename" and Unknown field. In field "id". These have the root cause, that those fields aren't defined on the input type.

根据规范


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

(…) This unordered map should not contain any entries with names not defined by a field of this input object type, otherwise an error should be thrown.

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

Now my question is what a good way to deal these kinds of scenarios is. Should I whitelist properties that are allowed on the input type in my app code?

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

If possible I'd like to avoid this and maybe have a utility function slice them off for me which knows about the input type. However, since the client doesn't know about the schema, this would have to happen on the server side. Thus, the unnecessary properties would be transferred there, which I suppose is the reason why they shouldn't be transferred in the first place.

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

Is there a better way than a whitelist?

我正在使用 apollo-client react-apollo graphql-server-express

推荐答案

所以我能想到的最优雅的方法是使用查询片段,其中包括数据的所有可变字段。该片段可以由过滤器实用程序 graphql-anywhere使用在发生突变之前删除所有不需要的数据。

So the most elegant way I could think of is using a fragment for the query, which includes all mutable fields of the data. That fragment can be used by the filter utility of graphql-anywhere to remove all unwanted data before the mutation happens.

要点是:

const ArticleMutableFragment = gql`
fragment ArticleMutable on Article {
  headline
  subline
  publishing {
    published
    time
  }
}
`

const ArticleFragment = gql`
fragment Article on Article {
  ...ArticleMutable
  id
  created
  updated
}
${ArticleMutableFragment}
`;

const query = gql`
query Article($id: ID!) {
  article(id: $id) {
    ...Article
  }
}
${ArticleFragment}
`;

const articleUpdateMutation = gql`
mutation updateArticle($id: ID!, $article: ArticleInput!) {
  updateArticle(id: $id, article: $article) {
    ...Article
  }
}
${ArticleFragment}
`;

...

import {filter} from 'graphql-anywhere';

...

graphql(articleUpdateMutation, {
  props: ({mutate}) => ({
    onArticleUpdate: (id, article) =>
      // Filter for properties the input type knows about
      mutate({variables: {id, article: filter(ArticleMutableFragment, article)}})
  })
})

...

ArticleMutable 片段现在也可以重复用于创建新文章。

The ArticleMutable fragment can now also be reused for creating new articles.

这篇关于在GraphQL中删除之前删除不必要的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆