简体   繁体   English

GraphQL 阿波罗读单

[英]GraphQL Apollo Read a single

I can read array of records but when query for single row, I can't.我可以读取记录数组,但是当查询单行时,我不能。 I have spent several days trying to resolve this issue on my own.我花了几天时间试图自己解决这个问题。 I had issues reading the array originally.我最初在读取数组时遇到了问题。 It was because my interface was corrupted some how and it had to be pointed out to me.这是因为我的界面在某些方面被损坏了,必须向我指出。 That being said I hoping that this will be a similar problem.话虽如此,我希望这将是一个类似的问题。 Result of the query is an object that I can not read for some reason.查询的结果是一个 object,由于某种原因我无法读取。 Any suggestions or any direction will be greatly appreciated.任何建议或任何方向将不胜感激。

implementation执行

export const GET_PT_CLIENT = gql`
  query pt_Client($clientid: Int!) {
    pt_Client(clientid: $clientid) { 
      clientid
      isActive  
      clientname 
    }
  }
`;


interface pt_ClientProps {
  clientid: Number
  index: Number
} 

const ClientDetails: React.FC<pt_ClientProps> = ({ clientid, index }) => {  
  const {
    data,
    loading,
    error
  } = useQuery< pt_ClientsTypes.pt_Clients_pt_Client >(GET_PT_CLIENT, {
    errorPolicy: 'all',
    fetchPolicy: 'network-only',
    variables: { clientid: clientid},
  });

  if (loading)
    return <Loading />;

  if (error)
    return <p>{error.toString()}</p>;

  if (!data)
    return <p>Not found</p>;
 
    console.log('==================================================');
    console.log(data);
    console.log('==================================================');
  
  return (
    <Fragment> 
      <div className="ClientDetails"> 
        <h4>{data.clientname}</h4>        
      </div>
    </Fragment>
  );
}

export default ClientDetails;

my codegen file types/pt_Clients.ts我的代码生成文件类型/pt_Clients.ts

/* tslint:disable */
/* eslint-disable */
// @generated
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL query operation: pt_Clients
// ====================================================

export interface pt_Clients_pt_Client {
  __typename: "pt_Client";
  id: number,
  clientid: number; 
  status: number | null;
  clientname: string | null; 
}

export interface pt_Clients {
  pt_Clients: (pt_Clients_pt_Client | null)[]; 
}

Playground Results操场结果

{
  "data": {
    "pt_Clients": [
      {
        "id": 1,
        "clientname": "Client A",
        "__typename": "pt_Client"
      },
      {
        "id": 2,
        "clientname": "Client B",
        "__typename": "pt_Client"
      },
      {
        "id": 3,
        "clientname": "Client C",
        "__typename": "pt_Client"
      },
      {
        "id": 4,
        "clientname": "Client D",
        "__typename": "pt_Client"
      } 
    ]
  },
  "loading": false,
  "networkStatus": 7,
  "stale": false
}

Thank you for taking the time to check into this issue for me.感谢您抽出时间为我检查此问题。

The results should be available in the pt_Client (same as the query name) property of the results data property.结果应该在结果data属性的pt_Client (与查询名称相同)属性中可用。

<Fragment> 
  <div className="ClientDetails"> 
    <h4>{data.pt_Client.clientname}</h4>        
  </div>
</Fragment>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM