不能访问变量。Graphql Apollo

huangapple go评论55阅读模式
英文:

Cannot acces variables. Graphql Apollo

问题

I use NextJs with Graphql Apollo and mongoose.
我使用NextJs与Graphql Apollo和mongoose。

When i use GraphiQL to make requests direct on back it works well, but when i implementing it on front its doesn work
当我使用GraphiQL直接向后端发出请求时,它运行良好,但当我在前端实现它时,它不起作用

I cannot access variables i have a message -
我无法访问变量,我收到一条消息 -

{"errors":[{"message":"Variable \"$category\" of required type \"String!\" was not provided.","locations":[{"line":1,"column":16}]}]}

Here i defined variables -
在这里,我定义了变量 -

   const url = useRouter();
   const name = url.query.category?.toString().toLowerCase();

   const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : name}});

This is my fethc -
这是我的获取操作 -

export const GET_PRODUCTS = gql`
  query products($category: String!){
    products(category: $category) {
      title
      id
      img
      decsiprion
      brend
      price
      altDescription
      category
      article
    }
  }
`;

This is backend -
这是后端 -

const ProductType = new GraphQLObjectType({
  name: "Product",
  fields: () => ({
    id: { type: GraphQLID },
    img: { type: GraphQLString },
    title: { type: GraphQLString },
    decsiprion: { type: GraphQLString },
    brend: { type: GraphQLString },
    price: { type: GraphQLInt },
    altDescription: { type: GraphQLString },
    category: { type: GraphQLString },
    article: { type: GraphQLInt },
  }),
});

const Rootquery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    products: {
      // take all products by category
      type: new GraphQLList(ProductType),
      args: { category: { type:GraphQLString }},
      resolve(parent, args) {
        return Product.find(args).exec();
      },
    },
    product: {
      // take only one by id
      type: ProductType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return Product.findById(args.id);
      },
    },
  },
});

I tryed to put variables just to see if its works, i did like this -
我尝试放置变量只是为了看它是否有效,我像这样做 -

 const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : 'woundrecovery'}});

And stil got error message.
但仍然收到错误消息。

英文:

I use NextJs with Graphql Apollo and mongoose.
When i use GraphiQL to make requests direct on back it works well, but when i implementing it on front its doesn work

I cannot access variables i have a message -
{"errors":[{"message":"Variable \"$category\" of required type \"String!\" was not provided.","locations":[{"line":1,"column":16}]}]}

Here i defined variables -

   const url = useRouter();
   const name = url.query.category?.toString().toLowerCase();

   const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : name}});

This is my fethc -

export const GET_PRODUCTS = gql`
  query products($category: String!){
    products(category: $category) {
      title
      id
      img
      decsiprion
      brend
      price
      altDescription
      category
      article
    }
  }
`;

This is backend -

const ProductType = new GraphQLObjectType({
  name: "Product",
  fields: () => ({
    id: { type: GraphQLID },
    img: { type: GraphQLString },
    title: { type: GraphQLString },
    decsiprion: { type: GraphQLString },
    brend: { type: GraphQLString },
    price: { type: GraphQLInt },
    altDescription: { type: GraphQLString },
    category: { type: GraphQLString },
    article: { type: GraphQLInt },
  }),
});

const Rootquery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    products: {
      // take all products by category
      type: new GraphQLList(ProductType),
      args: { category: { type:GraphQLString }},
      resolve(parent, args) {
        return Product.find(args).exec();
      },
    },
    product: {
      // take only one by id
      type: ProductType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return Product.findById(args.id);
      },
    },
  },
});

I tryed to put variables just to see if its works, i did like this -

 const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : 'woundrecovery'}});

And stil got error message.

答案1

得分: 2

非常接近,我认为你只需要将"category"提供为变量,就像这样:

 const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { category : 'woundrecovery'}});

或者,在你的查询中,你可以将"category"设为非必需,这样你就不必传递变量:

query products($category: String!) 变成 query products($category: String)

英文:

Very close, I think all you need to do is provide "category" as the variable, so like this:

 const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { category : 'woundrecovery'}});

alternativaly, in your query you can make category not required, so you don't have to pass the variable in

query products($category: String!) becomes query products($category: String)

答案2

得分: 2

GraphQL希望在您传递给请求的变量对象中找到一个名为"category"的键。所以将代码从:

const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : 'woundrecovery'}});

更改为:

const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { category : 'woundrecovery'}});

应该解决您的问题。

英文:

Graphql expects to find a key named "category" in the variables object that you pass to the request. so changing

const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { name : 'woundrecovery'}});

to

const { data, loading, error } = useQuery(GET_PRODUCTS, { variables: { category : 'woundrecovery'}});

should fix your issue

huangapple
  • 本文由 发表于 2023年3月8日 17:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定