api-extensions.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { constantCase, pascalCase } from 'change-case';
  2. import { NewPluginTemplateContext } from '../../types';
  3. export function renderApiExtensions(context: NewPluginTemplateContext) {
  4. if (!context.withApiExtensions) {
  5. return '';
  6. }
  7. if (!context.withCustomEntity) {
  8. return /* language=TypeScript */ `
  9. import gql from 'graphql-tag';
  10. export const shopApiExtensions = gql\`
  11. extend type Query {
  12. exampleShopQuery: String!
  13. }
  14. extend type Mutation {
  15. exampleShopMutation(input: String!): String!
  16. }
  17. \`;
  18. export const adminApiExtensions = gql\`
  19. extend type Query {
  20. exampleAdminQuery: String!
  21. }
  22. extend type Mutation {
  23. exampleAdminMutation(input: String!): String!
  24. }
  25. \`;
  26. `;
  27. } else {
  28. const entityName = context.entity.className;
  29. return /* language=TypeScript */ `
  30. import gql from 'graphql-tag';
  31. export const commonApiExtensions = gql\`
  32. type ${entityName} implements Node {
  33. id: ID!
  34. createdAt: DateTime!
  35. updatedAt: DateTime!
  36. name: String!
  37. }
  38. type ${entityName}List implements PaginatedList {
  39. items: [${entityName}!]!
  40. totalItems: Int!
  41. }
  42. extend type Query {
  43. ${context.entity.instanceName}s(options: ${entityName}ListOptions): ${entityName}List!
  44. ${context.entity.instanceName}(id: ID!): ${entityName}
  45. }
  46. # Auto-generated at runtime
  47. input ${entityName}ListOptions
  48. \`;
  49. export const shopApiExtensions = gql\`
  50. \${commonApiExtensions}
  51. \`;
  52. export const adminApiExtensions = gql\`
  53. \${commonApiExtensions}
  54. extend type Mutation {
  55. create${entityName}(input: Create${entityName}Input!): ${entityName}!
  56. update${entityName}(input: Update${entityName}Input!): ${entityName}!
  57. }
  58. input Create${entityName}Input {
  59. name: String!
  60. }
  61. input Update${entityName}Input {
  62. id: ID!
  63. name: String!
  64. }
  65. \`;
  66. `;
  67. }
  68. }