Remove unnecessary fields before mutation in GraphQL

In my schema there is a type called Article:

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

To update it, there is an appropriate input type that is used by the mutation updateArticle(id: ID!, article: ArticleInput!):

input ArticleInput {
  headline: String
  subline: String
}

The mutation itself is as follows:

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

The article is always saved in its entirety (not separate fields one after another), and therefore, when I pass the article to the mutation that I previously selected, it produces errors such as Unknown field. In field "updated", Unknown field. In field "__typename"and Unknown field. In field "id". They have the main reason that these fields are not defined in the input type.

This is the correct behavior as per specification :

(...) This unordered card must not contain entries with names, it is not determined by the field of this type of input object, otherwise an error must be thrown.

, . , ?

, , , , . , , . , , , , , .

, ?

apollo-client, react-apollo graphql-server-express.

+7
2

, , - , . graphql-anywhere , .

:

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 .

+7

, @amann , . , () - ?

typesafe-joi stripUnknown .

, .

joi - , " ", , .

0

Source: https://habr.com/ru/post/1671623/


All Articles