【问题标题】:How to get URL parameters or send 'Get' request to graphql?如何获取 URL 参数或向 graphql 发送“获取”请求?
【发布时间】:2019-08-21 14:10:22
【问题描述】:

问题来了,我只想使用“Get”请求将参数发送到基于graphql的服务器,因为用户可以将url保存为书签并直接打开它,就像Restful 'www.xxx.com? ID = abc'。但是在本地环境中,我的前端监听 3000 端口,后端(graphql)监听 8000 端口,所以它不能使用 'www.localhost.com:3000/graphql?query={getId{id}}' 发送请求,那又怎样有办法解决吗?

【问题讨论】:

    标签: reactjs graphql


    【解决方案1】:

    您可以使用 Apollo GraphQL https://www.apollographql.com/docs/?no-cache=1

    编写 GraphQL 查询以便传递参数

    示例:(反应示例)

    function apolloFetch(query, variablesS, headers) {
      // http link with base
      const httpLink = ApolloLink.from([
        new HttpLink({
          uri: BASE_URL,
          headers,
        }),
      ]);
    
      const client = new ApolloClient({
        link: logoutLink.concat(httpLink),
        cache: new InMemoryCache(),
      });
    
      const queryGQL = {
        query,
        variables: {
          ...variablesS,
        },
      };
      return client.query(queryGQL).then(res => res);
    }
    

    查询:

    gql`
      query(
        $name: String
        $email: String
      ) {
        patients(
          name: $name
          email: $email
        ) {
          age
          birthDate
          hobbies {
            name
          }
        }
      }
    `;
    

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 1970-01-01
      • 2014-08-17
      • 2011-11-08
      • 1970-01-01
      • 2018-06-26
      • 2015-09-13
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多