notum-cz / strapi-plugin-location

This plugin allows users to create location inputs and store latitude and longitude values as geometry types in a PostGIS database. It also provides functionality to filter items based on their location.
MIT License
26 stars 13 forks source link

GraphQL support #46

Open omikulcik opened 1 year ago

omikulcik commented 1 year ago

Feature request - GraphQL support

A GraphQL interface will be developed to grant access to the filtering and searching API.

nicolasburtey commented 1 year ago

is the graphql interface meant to be update for filtering and searching, but one basic (without filtering and searching) already exist?

omikulcik commented 1 year ago

Yes, I assume the graphql interface without filtering and searching is created by Strapi by default

JannikZed commented 6 months ago

Hi guys, We would like to take over this issue and create a PR to it - could you guide us to the right direction/ better describe the expected result and implementation basics so that we can start with it ?

IronHeartDan commented 2 months ago

Hi, I am wanting to use GraphQL and I have added my own resolver but how do I use this plugin to handle the actual search and return the data to my resolver. I know I can use raw Query but is there already a way that this plugin provides ?

  register({ strapi }) {
    const extensionService = strapi.service("plugin::graphql.extension");
    extensionService.use(({ strapi }) => ({
      typeDefs: `
            type Query {
              getNearbyRestaurants(lat: Float!, lng: Float!, range: Float!): [Restaurant]
            }
          `,
      resolvers: {
        Query: {
          getNearbyRestaurants: {
            resolve: async (parent, args, context) => {
              console.log(args);
              return []; // here 
            },
          },
        },
      },
      resolversConfig: {
        "Query.getNearbyRestaurants": {
          auth: false,
        },
      },
    }));
  },
IronHeartDan commented 2 months ago

If anyone needs raw query this is how it can be done

 register({ strapi }) {
    const extensionService = strapi.service("plugin::graphql.extension");
    extensionService.use(({ strapi }) => ({
      typeDefs: `
            type Query {
              getNearbyRestaurants(lat: Float!, lng: Float!, range: Float!): [Restaurant]
            }
          `,
      resolvers: {
        Query: {
          getNearbyRestaurants: {
            resolve: async (parent, args, context) => {
              const { lat, lng, range } = args;
              const restaurants = await strapi.db.connection.raw(
                `SELECT * FROM restaurants r WHERE
                  ST_DWithin(
                    r.location_geom::geography,
                    ST_SetSRID(ST_MakePoint(?, ?), 4326),
                    ?
                  )
                `,
                [lng, lat, range]
              );
              return restaurants.rows;
            },
          },
        },
      },
      resolversConfig: {
        "Query.getNearbyRestaurants": {
          auth: false,
        },
      },
    }));
  },