#How to use GraphQLService from within endpoints

11 messages · Page 1 of 1 (latest)

smoky spoke
#

Hello,

I want to execute a graphql query from within a endpoint, but can't make it work.

What I want to do:

module.exports = function registerEndpoint(
  router: Router,
  { services, env, exceptions },
) {
  const { ServiceUnavailableException } = exceptions;

  router.get("/test", async (req, res, next) => {
    const { ItemsService, GraphQLService } = services;

    const { schema, accountability } = req as Request & {
      schema: any;
      accountability: any;
    };

          const graphqlService: GraphQLService = new GraphQLService({
            schema: schema,
            accountability: accountability,
            scope: "items",
          });

          //@ts-ignore
          const data = await graphqlService.execute({
            query: `query blabla`,
            variables: { id: "bla"},
          });
  });
};

Does anyone have an idea on how to make this work?

river fox
#

You don’t use graphql from endpoints. You use the itemsService from useServices, which is from the @directus/extensions module

smoky spoke
#

what do you mean that I don't use it from endpoint? I want to execute a query from within the endpoint to fetch some data, I don't want to use the ItemsService

twin sky
#

You can but i believe the query will need to be parsed before handing it over to the graphql service

river fox
#

If you use graphql API, it would have to be authenticated. Yes, you can use it, but this is not the intended way if you want to access certain things. However, make sure that your query is proper. In my opinion, it is more complicated to use the graphql service like this.

twin sky
#

It is for sure a bunch of overhead to eventually use the ItemsServices anyway (thats what the graphql resolvers do)

#
module.exports = function registerEndpoint(
  router: Router,
  { services, env, exceptions },
) {
  const { ServiceUnavailableException } = exceptions;
  const { parseGraphQL } = require('directus/middleware/graphql');
  router.get("/test", async (req, res, next) => {
    const { ItemsService, GraphQLService } = services;

    const { schema, accountability } = req as Request & {
      schema: any;
      accountability: any;
    };

          const graphqlService: GraphQLService = new GraphQLService({
            schema: schema,
            accountability: accountability,
            scope: "items",
          });

          let response = {};
          await parseGraphQL({
                body: {
                query: `query {}`,
                variables: {},
          }, response);

          //@ts-ignore
          const data = await graphqlService.execute(response.locals.graphqlParams);
  });
};```
should make your endpoint accept graphql
smoky spoke
#

yes I know it's a bunch of overhead, but I can't find a way to properly exclude properties with the rest api filter and my structure is very nested and doing this in the code is a lot of overhead too

smoky spoke
twin sky
#

ah then you'll have to abuse the middleware a little

smoky spoke
#

hmm, I think I will use a graphql client with authorization in order to achieve this