为什么可能在input_field_definition上不使用指令

发布于 2025-01-18 15:53:15 字数 706 浏览 1 评论 0 原文

我尝试通过指令将长度限制到我的GraphQl架构

const typeDefs = gql`
  directive @length(max: Int!) on FIELD_DEFINITION

  type Book {
    id: ID
    title: String
    author: String
    pages: Float 
  }
  
  input PagesInput {
    pages: Float @length(max: 10)
  }
  
  type Mutation {
    changePages(bookId: ID, pagesInput: PagesInput): Book
  }
`;

后,我使用apollo 指令。 如果我将指令设置为书籍类型(不输入),则此功能正常工作。 但是,如果我将其设置为输入(我真正需要的地方),我会收到一个错误:

Error: Directive "@length" may not be used on INPUT_FIELD_DEFINITION.

为什么呢?如何将指令设置为输入类型?

I try to set length limit to my graphQl schema via directive

const typeDefs = gql`
  directive @length(max: Int!) on FIELD_DEFINITION

  type Book {
    id: ID
    title: String
    author: String
    pages: Float 
  }
  
  input PagesInput {
    pages: Float @length(max: 10)
  }
  
  type Mutation {
    changePages(bookId: ID, pagesInput: PagesInput): Book
  }
`;

after it I made custom function lengthDirective using apollo instructions.
This function works properly if I set directive to Books type (not to input).
But if I set it to input (where I really need it), I receive an error:

Error: Directive "@length" may not be used on INPUT_FIELD_DEFINITION.

Why it that? How can I set up directive to input type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文