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

Add Geomashup fields to WP GraphQL #886

Open
chawax opened this issue Dec 28, 2020 · 3 comments
Open

Add Geomashup fields to WP GraphQL #886

chawax opened this issue Dec 28, 2020 · 3 comments

Comments

@chawax
Copy link

chawax commented Dec 28, 2020

Hi,

I try to use Gatsby with an existing Wordpress site using Geomashup. But it looks like Geomashup fields (latitude and longitude) are not available in WP GraphQL which is used by latest Gatsby Wordpress source plugins. Is there a plan to include Geomashup in GraphQL ?

Thanks for your help

@cyberhobo
Copy link
Owner

I don't have a plan to implement this, but would certainly accept pull requests.

A workaround might be to use the Settings / Geo Mashup / Copy Geodata Meta Fields feature to get the geodata into WordPress metadata. That copying only happens when the geodata is saved, so turning that on does not immediately add it to existing geolocated objects.

@chawax
Copy link
Author

chawax commented Dec 30, 2020

Thanks for your response. I had set the setting to copy geodata into post metas, so I will try to get them this way.

@chawax
Copy link
Author

chawax commented Dec 31, 2020

I could make it work using graphql_register_types action.

I added this piece of code in my theme. Any idea where I could add this in GeoMashup to send a pull request ?
It only works for posts for the moment. And you can't query on geo field for the moment (can't find how to do this).
But I will try to improve it.

add_action('graphql_register_types', function () {

    register_graphql_object_type('Geo', array(
        'description' => __('Geo Mashup coordinates associated with the object.', 'GeoMashup'),
        'fields' => array(
            'latitude' => array(
                'type' => float,
                'description' => __('The decimal latitude in the WGS 84 datum.', 'GeoMashup'),
            ),
            'longitude' => array(
                'type' => float,
                'description' => __('The decimal longitude in the WGS 84 datum.', 'GeoMashup'),
            ),
            'description' => array(
                'type' => string,
                'description' => __('An address or general description of the location.', 'GeoMashup'),
            ),
        ),
    ));

    register_graphql_field('Post', 'geo', array(
        'type' => 'Geo',
        'description' => __('Geo Mashup coordinates associated with the post', 'GeoMashup'),
        'resolve' => function ($post) {
            $location = GeoMashupDB::get_object_location('post', $post->ID);
            if (empty($location)) {
                return null;
            }
            return array(
                'latitude' => (float) $location->lat,
                'longitude' => (float) $location->lng,
                'description' => $location->address,
            );
        },
    ));

});

And you can use it this way :

query MyQuery {
  posts {
    edges {
      node {
        title
        geo {
          latitude
          longitude
          description
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants