useQuery的奇怪问题:未读取查询参数

问题描述 投票:6回答:2

我有一个将字符串(userToFetch)传递为参数化查询中的变量参数的组件。该组件如下所示:

// pages/index.jsx

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

const GET_USERS = gql`
  query users ($limit: Int!, $username: String!) {
    users (limit: $limit, where: { username: $username }) {
      username
      firstName
    }
  }
`;

const Home = () => {
  const userToFetch = 'jonsnow';

  const {
    loading,
    error,
    data,
  } = useQuery(
    GET_USERS,
    {
      variables: { limit: 2, username: userToFetch },
      notifyOnNetworkStatusChange: true,
    },
  );

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error) {
    return <p>Error: {JSON.stringify(error)}</p>;
  }
  return (
    <div>
      <ul>
        {data.users.map(user => {
          return <li>{user.username} {user.firstName}</li>;
        })}
      </ul>
    </div>
  );
};

export default Home;

这就是我配置Apollo客户端的方式:

// /apollo-client.js

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import withApollo from 'next-with-apollo';
import { createHttpLink } from 'apollo-link-http';
import fetch from 'isomorphic-unfetch';

const GRAPHQL_URL = 'https://dev.schandillia.com/graphql';

const link = createHttpLink({
  fetch, // Switches between unfetch & node-fetch for client & server.
  uri: GRAPHQL_URL
});

// Export a HOC from next-with-apollo
// Docs: https://www.npmjs.com/package/next-with-apollo
export default withApollo(
  // You can get headers and ctx (context) from the callback params
  // e.g. ({ headers, ctx, initialState })
  ({ initialState, ctx }) => {
    console.log('initialState', initialState);
    console.log('ctx', ctx);

    return new ApolloClient({
      link: link,
      cache: new InMemoryCache()
        //  rehydrate the cache using the initial data passed from the server:
        .restore(initialState || {})
    })
  }
);

数据库是以下users的集合:

"users": [
      {
        "username": "negger",
        "firstName": "Arnold",
        "lastName": "Schwarzenegger"
      },
      {
        "username": "jonsnow",
        "firstName": "Jon",
        "lastName": "Snow"
      },
      {
        "username": "tonystark",
        "firstName": "Tony",
        "lastName": "Stark"
      }
    ]
  }

现在,尽管这应该工作(当我在https://dev.schandillia.com/graphql的graphql游乐场中运行查询时,它确实可以运行,但代码的运行就像where子句不存在一样!它只返回所有结果,就好像正在运行的查询是:

users {
  _id
  username
  firstName
}

为了重现问题,请访问https://www.schandillia.com。该页面应该显示仅包含一个由匹配的username-firstName值组成的元素的列表:jonsnow Jon,但它返回两个条目,negger Arnoldjonsnow Jon(指定为limit,但完全忽略了where)。现在,使用jonsnow作为where中的https://dev.schandillia.com/graphql参数运行相同的查询:

{
  users(where: { username: "jonsnow" }) {
    _id
    username
    firstName
  }
}

结果将完全符合预期:

{
  "data": {
    "users": [
      {
        "_id": "5d9f261678a32159e61018fc",
        "username": "jonsnow",
        "firstName": "Jon",
      }
    ]
  }
}

我俯瞰什么?

P.S。:回购已在https://github.com/amitschandillia/proost/tree/master/apollo-nextjs供参考。

UPDATE:为了追踪根本原因,我尝试在apollo-client.js中记录一些值:

console.log('initialState', initialState);

奇怪的是,输出显示正确的查询以及要传递的变量,但结果错误:

...
ROOT_QUERY.users({"limit":2,"where":{"username":"jonsnow"}}).0:
  firstName: "Arnold"
  username: "negger"
  __typename: "UsersPermissionsUser"
...

UPDATE:这是我的Apollo Client Developer Tools中结果的屏幕截图:

enter image description here

next.js apollo-client
2个回答
1
投票

由Strapi生成的模式为where属性提供了一个JSON类型,因此您必须将查询变量中的整个where部分作为JSON传递,因为没有注入变量。

# Write your query or mutation here
query users($where: JSON) {
  users(where: $where) {
    username
    firstName
  }
}

和变量看起来像:

{"where": {"username": "jonsnow"}}

0
投票

您能否共享Apollo服务器存储库以供参考?另外,从wrbapp和游乐场查询时,我将检查devtools.network.panel中的http请求。请分享输出。最后一件事,我将删​​除缓存以查看是否产生差异。

© www.soinside.com 2019 - 2024. All rights reserved.