Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use React Apollo Query component for GraphQL query with typescript causing error

I'm trying to query a GraphQl API with Apollo. I'm doing it in React with typescript and I can't get the Query component to stop throwing errors. I'm new to this whole combination, so I might be doing something obviously wrong. Any ideas?

Here's the error:

Type '{ children: (string | (({ loading, error, data }: QueryResult<any, OperationVariables>) => any))[]; query: any; }' is not assignable to type 'Pick<Readonly<QueryProps<any, OperationVariables>> & Readonly<{ children?: ReactNode; }>, "children" | "displayName" | "skip" | "onCompleted" | "onError" | "ssr" | "variables" | ... 6 more ... | "partialRefetch">'.
  Types of property 'children' are incompatible.
    Type '(string | (({ loading, error, data }: QueryResult<any, OperationVariables>) => any))[]' is not assignable to type '((result: QueryResult<any, OperationVariables>) => ReactNode) | (((result: QueryResult<any, OperationVariables>) => ReactNode) & string) | (((result: QueryResult<any, OperationVariables>) => ReactNode) & number) | ... 4 more ... | (((result: QueryResult<...>) => ReactNode) & ReactPortal)'.
      Type '(string | (({ loading, error, data }: QueryResult<any, OperationVariables>) => any))[]' is not assignable to type '((result: QueryResult<any, OperationVariables>) => ReactNode) & ReactNodeArray

Here's my code:

import React from 'react';
import gql from "graphql-tag";
import { Query } from "react-apollo";


export interface LoginProps { 
    data: object; item: string; query: object; loading: string; error: string;
 };

 const getLogin =
    gql`
        {
        viewer {
            login
            }
        }
    `;

export class Test extends React.Component<LoginProps, {}> { 
    <Query query={getLogin}>
            {({ loading, error, data }) => {
                if (loading) return <p>Loading...</p>;
                if (error) return <p>Error!</p>;

                return data.viewer.map( item => {
                    <div key={item.login}>
                        <p>{item.login}</p>
                    </div>
                });
            }};
    </Query>
};

export default Test;
like image 795
Mighty_Muffinn Avatar asked Jun 29 '26 15:06

Mighty_Muffinn


1 Answers

Obviously not relevant, but for those coming here in search of an answer, desctructure from within Query like so:

    <Query query={getLogin}>
        {(result: QueryResult) => {
          const { loading, error, data } = result;

            // ...rest here
            if (loading) return <p>Loading...</p>;
            if (error) return <p>Error!</p>;

            return data.viewer.map( item => {
                <div key={item.login}>
                    <p>{item.login}</p>
                </div>
            });
        }};
</Query>
like image 166
YEVY Avatar answered Jul 01 '26 06:07

YEVY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!