wordpress-geo-mashup icon indicating copy to clipboard operation
wordpress-geo-mashup copied to clipboard

Add Geomashup fields to WP GraphQL

Open chawax opened this issue 4 years ago • 3 comments

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

chawax avatar Dec 28 '20 22:12 chawax

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.

cyberhobo avatar Dec 29 '20 19:12 cyberhobo

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 avatar Dec 30 '20 10:12 chawax

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

chawax avatar Dec 31 '20 09:12 chawax