简体   繁体   English

CombinedErrors graphQLErrors Unknown argument limit on field Query.posts - 尝试使用 urql、Strapi、Next JS 将参数传递给查询

[英]CombinedErrors graphQLErrors Unknown argument limit on field Query.posts - Trying to pass argument to query using urql, Strapi, Next JS

I'm simply trying to use a variable to pass in limit as an arg to my GraphQL query.我只是想使用一个变量将limit作为 arg 传递给我的 GraphQL 查询。 I'm using urql version 3.0.1 and Strapi with its GraphQL plugin in a Next app.我在 Next 应用程序中使用urql版本3.0.1和带有GraphQL 插件的 Strapi。

I want to add a limit variable to limit the rows returned.我想添加一个limit变量来限制返回的行。 I'll provide an example below followed by the full error code.我将在下面提供一个示例,后跟完整的错误代码。

This Works这个作品

import { useQuery } from 'urql'

const GET_RECENT_POSTS_QUERY = `
query {
  posts (sort: "publishedAt:DESC") {
    data {
      attributes {
        publishedAt
        title
      }
    }
  }
}
`

export default function Example() {
  const [res] = useQuery({
    query: GET_RECENT_POSTS_QUERY,
  })

  return (
    <div>
      <h3>Recent Posts</h3>
      <div>{JSON.stringify(res)}</div>
    </div>
  )
}
`

Result结果

Recent Posts
{"fetching":false,"stale":false,"data":{"posts":{"data":[{"attributes":{"publishedAt":"2022-09-21T05:17:29.483Z","title":"Post Number 5","__typename":"Post"},"__typename":"PostEntity"},{"attributes":{"publishedAt":"2022-09-21T05:17:13.087Z","title":"Post Number 3 ","__typename":"Post"},"__typename":"PostEntity"},{"attributes":{"publishedAt":"2022-09-20T20:08:05.611Z","title":"Post Number 4","__typename":"Post"},"__typename":"PostEntity"},{"attributes":{"publishedAt":"2022-09-01T21:04:03.717Z","title":"My Second Post","__typename":"Post"},"__typename":"PostEntity"},{"attributes":{"publishedAt":"2022-09-01T19:22:20.413Z","title":"My First Post","__typename":"Post"},"__typename":"PostEntity"}], ...and so on

I'm attempting to pass in a variable to limit the number of results returned.我试图传入一个变量来限制返回的结果数量。

Doesn't Work - Passing in Variable不起作用 - 传入变量

I've tried various approaches mainly around something like this.我主要围绕这样的事情尝试了各种方法。

const GET_RECENT_POSTS_QUERY = `
query ($limit: Int!){
  posts (limit: $limit, sort: "publishedAt:DESC") {
    data {
      attributes {
        publishedAt
        title
      }
    }
  }
}
`

export default function Example() {
  const [res] = useQuery({
    query: GET_RECENT_POSTS_QUERY,
    variables: { limit: 3 },
  })
  // ...

Result/Error结果/错误

Error starts with:错误开始于:

"error":{"name":"CombinedError","graphQLErrors":[{"message":"Unknown argument \"limit\" on field \"Query.posts\"

Full error:完整错误:

Recent Posts
{"fetching":false,"stale":false,"error":{"name":"CombinedError","graphQLErrors":[{"message":"Unknown argument \"limit\" on field \"Query.posts\".","extensions":{"code":"GRAPHQL_VALIDATION_FAILED","exception":{"stacktrace":["GraphQLError: Unknown argument \"limit\" on field \"Query.posts\"."," at Object.Argument (/path-to-project/backend/node_modules/graphql/validation/rules/KnownArgumentNamesRule.js:46:29)"," at Object.enter (/path-to-project/backend/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/path-to-project/backend/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/path-to-project/backend/node_modules/graphql/language/visitor.js:243:26)"," at validate (/path-to-project/backend/node_modules/graphql/validation/validate.js:69:24)"," at validate (/path-to-project/backend/node_modules/apollo-server-koa/node_modules/apollo-server-core/dist/requestPipeline.js:186:39)"," at processGraphQLRequest (/path-to-project/backend/node_modules/apollo-server-koa/node_modules/apollo-server-core/dist/requestPipeline.js:98:34)"," at processTicksAndRejections (node:internal/process/task_queues:96:5)"," at async processHTTPRequest (/path-to-project/backend/node_modules/apollo-server-koa/node_modules/apollo-server-core/dist/runHttpQuery.js:220:30)"," at async /path-to-project/backend/node_modules/apollo-server-koa/dist/ApolloServer.js:83:59"]}}}],"response":{}},"operation":{"key":2405370831,"query":{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"limit"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}},"directives":[]}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"posts"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"limit"},"value":{"kind":"Variable","name":{"kind":"Name","value":"limit"}}},{"kind":"Argument","name":{"kind":"Name","value":"sort"},"value":{"kind":"StringValue","value":"publishedAt:DESC","block":false}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"attributes"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"publishedAt"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"title"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}}],"loc":{"start":0,"end":115,"source":{"body":"query ($limit: Int!) { posts(limit: $limit sort:\"publishedAt:DESC\") { data { attributes { publishedAt title } } } }","name":"gql","locationOffset":{"line":1,"column":1}}}},"variables":{"limit":3},"kind":"query","context":{"url":"http://localhost:1337/graphql","preferGetMethod":false,"requestPolicy":"cache-first","suspense":false,"meta":{"cacheOutcome":"miss"}}},"hasNext":false}

GraphQL seems to recognizes the variable $limit but it's unclear to me how it should be used. GraphQL 似乎可以识别变量$limit但我不清楚它应该如何使用。 I'm sifting through docs and can't seem to find a relevant example.我正在筛选文档,似乎找不到相关示例。 Perhaps I'm missing something fundamental about GraphQL/Strapi plugin or associated packages, as I haven't used them extensively.也许我缺少一些关于 GraphQL/Strapi 插件或相关包的基本知识,因为我没有广泛使用它们。 Is there some other set up involved to accomplish using variables/arguments like this?是否有一些其他设置涉及使用这样的变量/参数来完成?

For any others stumbling on this, I ended up getting this to work with pagination from the Strapi GraphQL API对于任何其他绊倒此的人,我最终得到了这个与Strapi GraphQL API 的分页一起工作

Solution解决方案

const GET_RECENT_POSTS_QUERY = `
query ($limit: Int!) {
  posts (pagination: {start: 0, limit: $limit}, sort: "publishedAt:DESC") {
    data {
      attributes {
        publishedAt
        title
      }
    }
  }
}
`

export default function Example() {
  const [res] = useQuery({
    query: GET_RECENT_POSTS_QUERY,
    variables: { limit: 3 },
  })
  // ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么graphQl在尝试切片查询时返回“Unknown argument &#39;first&#39; on field...”错误? - Why graphQl returns “Unknown argument 'first' on field…” error when trying to slice on query? Graphql 字段上的未知参数 - Graphql Unknown argument on field 收到错误:类型为“查询” [GraphQL,Relay,React]的字段“用户”上的未知参数“ id” - Getting error : Unknown argument “id” on field “user” of type “Query” [GraphQL, Relay, React] 尝试将第二个参数传递给JS函数时出现语法错误 - Syntax error when trying to pass second argument to JS function 使用findAndModify时,将函数参数作为$ set运算符中字段的名称传递 - Pass function argument as a name of field in $set operator while using findAndModify 在 React.js 中的 `urql` GraphQL 客户端中执行查询时去抖动输入 - Debounce Input while performing a query in `urql` GraphQL Client in React.js js将附加参数传递给函数 - js pass additional argument to function phantomJS - 将参数传递给JS文件 - phantomJS - Pass Argument to the JS File 类型为“ Mutation \\”的字段“ forgotPassword \\”上的“未知参数\\” email \\”。” - “Unknown argument \”email\“ on field \”forgotPassword\“ of type \”Mutation\“.” 如何在mysql查询中重复使用一个参数(NODE.js) - How to use one argument repeatedly in mysql query (NODE.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM