5

I'm trying to make a query in Apollo Client with React without returning a JSX component, just an object (the data object that is received when making a common query to Apollo Server).

I tried using <Query /> component, but it returns me a React Node and I only need an object. The documentation only explain methods that return JSX components at some point, when all that I'm looking to do is send a request and process the response in a callback.

Actually I'm trying this way (I'm using TypeScript in React):

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

const MY_QUERY = gql`
  query MY_QUERY {
    myQuery {
      attr1
      attr2
    }
  }
`;

const res = ({ client }: { client: any }) =>
  client
    .query(MY_QUERY)
    .then((data: { data: any }) => data);

withApollo(res);

console.log(res);

With this, what I'm looking is for a object like

{
  "data": {
    "myQuery": [
      {
        "attr1": "something 1...",
        "attr2": "another thing 1..."
      },
      {
        "attr1": "something 2...",
        "attr2": "another thing 2..."
      },
      {
        "attr1": "something 3...",
        "attr2": "another thing 3..."
      }
    ]
  }
}

But what I'm receiving from the browser is

ƒ WithApollo(props) {
    var _this = _super.call(this, props) || this;

    _this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
    return _this;
}

Any suggestion?

12
  • Please post the code you currently have. stackoverflow.com/help/mcve is necessary for code problems. It's unclear what your code currently looks like but it's likely that a component need to be used any way because that's how things interact in React. Commented Feb 10, 2019 at 20:47
  • Pleas edit your question and format your code inside of it instead of sending it as a comment Commented Feb 10, 2019 at 20:52
  • Ok! I've just put my code Commented Feb 10, 2019 at 20:58
  • That's a really weird gql query syntax, does this works in the GQL playground ? Can you show me the JSX part that uses the result, I'll show you how to use the Query node, it's fairly easy Commented Feb 10, 2019 at 21:01
  • Ok, I've updated the code to the real way I'm doing it. Commented Feb 10, 2019 at 21:07

1 Answer 1

4

Try this instead

class anyComponent extends React.Component<Props> {
   state = {
      results: null,
   }
   componentDidMount = async () => {
       const {client} = this.props;
       res = await client.query({query: MY_QUERY});
       console.log(res);
       this.setState({results: res})
   }
   render() {
       // check if any results exist (just an example)
       return results ? <AnyJsx results={results}/> : null;
   }
}
export default withApollo(anyComponent);

You were console logging the function instead of calling it to get its result You may need some lifecycle functions like componentDidMount to retrieve data

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Rachid! I updated the code with your answer but now TypeScript say me that withApollo is not expecting to receive a promise as parameter
the error is: Argument of type '({ client }: { client: any; }) => Promise<any>' is not assignable to parameter of type 'ComponentType<WithApolloClient<{ client: any; }>>'.
@ClementeSerranoSutil check, I have updated the answer.
@ClementeSerranoSutil let me know if that help. if you want to add ur logic in life cycle methods JSX won't help you. you will need to use the HOC withApollo.
What would client be? I'm using Gatsby which uses graphql at build time and I don't think it has a client to query from, or I think the client might be local?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.