Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two REST API calls inside withAPIData? #6528

Closed
mrleemon opened this issue May 1, 2018 · 6 comments
Closed

Two REST API calls inside withAPIData? #6528

mrleemon opened this issue May 1, 2018 · 6 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@mrleemon
Copy link
Contributor

mrleemon commented May 1, 2018

Issue Overview

I'm developing a dynamic block that retrieves post data using the REST API. I'm wondering if one can make two REST API calls at the same time inside withAPIData:

registerBlockType( 'my-plugin/my-post-block', {
    title: 'My Post Block',
    icon: 'megaphone',
    category: 'widgets',
    attributes: {
        post: {
            type: 'string',
            default: '0',
        },
    },

    edit: withAPIData( ( props ) => {
        return {
            posts: '/wp/v2/posts',
            singlepost: `/wp/v2/posts/${ props.attributes.post }`
        };
    } )( ( props ) => {
        if ( ! props.posts.data ) {
            return "loading !";
        }
        if ( ! props.singlepost.data ) {
            return "loading !";
        }
        if ( props.singlepost.data === '0' ) {
            return "No post selected";
        }

        ....

    } )
} );

Right now I'm getting a permanent "loading !" message instead of data from both endpoints. Is this supported? Do I have to make two separate REST API calls? If so, how?

Thanks in advance

@danielbachhuber danielbachhuber added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label May 1, 2018
@jasonbahl
Copy link

You could use WPGraphQL to request multiple resources in a single request. 😀

@mrleemon
Copy link
Contributor Author

mrleemon commented May 2, 2018

Is this related maybe? #6277

@mrleemon
Copy link
Contributor Author

mrleemon commented May 3, 2018

I installed 2.8 and the issue is mitigated somehow. Now, from time to time, one of the REST API calls stalls indefinitely. Hope that the #6303 fix gets into 2.9.

@mrleemon
Copy link
Contributor Author

Updated to 2.9 and I'm still having problems.

@mrleemon
Copy link
Contributor Author

mrleemon commented Jun 6, 2018

Tried again with GB 3.0.1 and the issue still persists. Right now, it's impossible to call two endpoints one after another without the risk of one of them failing. I hope you can find a fix for #6303 soon.

@mrleemon
Copy link
Contributor Author

As withAPIData will be deprecated in favor of withSelect in the near future, I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

3 participants