Urigo / SOFA

The best way to create REST APIs - Generate RESTful APIs from your GraphQL Server

Home Page:https://www.the-guild.dev/graphql/sofa-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Route Tag and Descriptions are missing

yuraxdrumz opened this issue · comments

Hey!

Thanks for this library,

I have an issue where I try to generate tags and description for each endpoint but they do not get generated.

I noticed that fets allows passing tags and description like so:

createRouter().route({
  /* OpenAPI specific information */
  operationId: 'getUsers',
  description: 'Get all users',
  tags: ['users'],
 
  /* Regular configuration */
  method: 'GET',
  path: '/users'
  /* .. */
})

However, when sofa adds a route to the router it does not set the tags and description fields
https://github.com/Urigo/SOFA/blob/e28a837a9f051413c9b79e7af4bb5ccc299b7ed8/src/router.ts#L249C3-L249C9

  router.route({
    path: route.path,
    method: route.method,
    schemas: getRouteSchemas({
      method: route.method,
      path: route.path,
      info,
      sofa,
      responseStatus: route.responseStatus,
    }),
    handler: useHandler({ info, route, fieldName, sofa, operation }),
  })

On top of that, I see you return the route from the createQueryRoute and createMutationRoute, but you call it and ignore the return values altogether.

Example of createMutationRoute return statement

  return {
    document: operation,
    path: route.path,
    method: route.method.toUpperCase() as HTTPMethod,
    tags: routeConfig?.tags ?? [],
    description: routeConfig?.description ?? field.description ?? '',
  };

Usage of createQueryRoute and createMutationRoute

  if (queryType) {
    Object.keys(queryType.getFields()).forEach((fieldName) => {
      createQueryRoute({ sofa, router, fieldName });
    });
  }

  if (mutationType) {
    Object.keys(mutationType.getFields()).forEach((fieldName) => {
      createMutationRoute({ sofa, router, fieldName });
    });
  }

Thanks

Just noticed this #1351

FIY, I managed to add tag, description and operationId until the PRs are merged

This is how I managed to add the fields above

Example routes

  const routes = {
    'Query.user': {
      method: 'GET',
      path: '/sites/:siteGuid/users/:userGuid',
      tags: ['Users'],
      description: 'Allows getting a user by siteGuid and userGuid',
    },
    'Mutation.createUser': {
      method: 'POST',
      path: '/sites/:siteGuid/users',
      tags: ['Users'],
      description: 'Allows creating a user under a site guid',
    },
    'Mutation.updateUser': {
      method: 'PUT',
      path: '/sites/:siteGuid/users',
      tags: ['Users'],
      description: 'Allows updating a user under a site guid',
    },
}

Sofa OpenAPI variable

  const openAPIDocument = sofa.openAPIDocument;
  for (const path of Object.keys(openAPIDocument.paths)) {
    const pathDefinition = openAPIDocument.paths[path];
    const pathMethods = Object.keys(pathDefinition);
    for (const method of pathMethods) {
      // console.log('method ->', method);
      const methodDefinition = pathDefinition[method];
      // console.log('methodDefinition ->', methodDefinition);
      // console.log('path ->', path);

      for (const routeKey of Object.keys(routes)) {
        const route = routes[routeKey];
        const semiReplacedPath = path.replace(/}/g, '');
        const replaced = semiReplacedPath.replace(/{/g, ':');
        if (route.path === replaced && route.method === method.toUpperCase()) {
          methodDefinition.tags = route.tags;
          methodDefinition.description = route.description;
          methodDefinition.operationId = `${route.description}`;
        }
      }
    }
  }

End result
Screenshot 2023-07-04 at 11 54 17