cyberhobo / wordpress-geo-mashup

Official repository for Geo Mashup, the plugin that makes WordPress into a GeoCMS. Documentation:
https://github.com/cyberhobo/wordpress-geo-mashup/wiki/Getting-Started
65 stars 16 forks source link

Add Geomashup fields to WP GraphQL #886

Open chawax opened 3 years ago

chawax commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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
        }
      }
    }
  }
}