微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何发送带有查询的 Apollo 请求 (graphql)?

如何解决如何发送带有查询的 Apollo 请求 (graphql)?

我有这样的任务,所以用这些参数发送一个查询

 {operationName:getPostList,variables:{input:{type:post,locale:en,projectId:1}},query:query getPostList($input: PostSearchType) {\n  posts(input: $input,paging: {limit: 12}) {\n    items {\n      id\n    type\n   locale\n  shortDescription\n fullUrl\n   thumbnail\n   tags\n      title\n      publishedAt\n      __typename\n    }\n    __typename\n  }\n}\n"}

但是我不明白如何在 gql 函数(apollo-boost)中指定这些参数 这是我的要求

const GET_MOVIES = gql`
{
    query getPostList($input: PostSearchType){
        posts(
            input:$input,paging : {
                limit:12
            }
            items{
                id,type,locale,shortDescription,fullUrl,thumbnail,tags,title,publishedAt,__typename
            }
            __typename
        )
    }
    "operationName":"getPostList","variables":{
        "input":{
            "type":"post","locale":"en","projectId":1  
        }
    }
}

`

解决方法

请查看示例代码。您可以根据需要进行调整。

/**
 * Sample code to showcase how queries work in React Apollo client
 */

import React from "react";
import gql from "graphql-tag";
import { useQuery } from "@apollo/react-hooks";

const GET_MOVIES = gql`
  query getPostList($input: PostSearchType) {
    posts(input: $input) {
      id
      type
      locale
      shortDescription
      fullUrl
      thumbnail
      tags
      title
      publishedAt
      __typename
    }
  }
`;

const ApolloReactExample = () => {
  const { loading,data,error } = useQuery(GET_MOVIES,{
    variables: { input: "YOUR_INPUT" },});

  return <div></div>;
};

export default ApolloReactExample;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。