vite-plugin-admin-api-schema.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { GraphQLTypesLoader } from '@nestjs/graphql';
  2. import {
  3. getConfig,
  4. getFinalVendureSchema,
  5. resetConfig,
  6. runPluginConfigurations,
  7. setConfig,
  8. VENDURE_ADMIN_API_TYPE_PATHS,
  9. } from '@vendure/core';
  10. import {
  11. buildSchema,
  12. GraphQLList,
  13. GraphQLNonNull,
  14. GraphQLObjectType,
  15. GraphQLSchema,
  16. GraphQLType,
  17. isEnumType,
  18. isInputObjectType,
  19. isObjectType,
  20. isScalarType,
  21. } from 'graphql';
  22. import { Plugin } from 'vite';
  23. import { ConfigLoaderApi, getConfigLoaderApi } from './vite-plugin-config-loader.js';
  24. export type FieldInfoTuple = readonly [
  25. type: string,
  26. nullable: boolean,
  27. list: boolean,
  28. isPaginatedList: boolean,
  29. ];
  30. export interface SchemaInfo {
  31. types: {
  32. [typename: string]: {
  33. [fieldname: string]: FieldInfoTuple;
  34. };
  35. };
  36. inputs: {
  37. [typename: string]: {
  38. [fieldname: string]: FieldInfoTuple;
  39. };
  40. };
  41. scalars: string[];
  42. enums: {
  43. [typename: string]: string[];
  44. };
  45. }
  46. const virtualModuleId = 'virtual:admin-api-schema';
  47. const resolvedVirtualModuleId = `\0${virtualModuleId}`;
  48. export function adminApiSchemaPlugin(): Plugin {
  49. let configLoaderApi: ConfigLoaderApi;
  50. let schemaInfo: SchemaInfo;
  51. return {
  52. name: 'vendure:admin-api-schema',
  53. configResolved({ plugins }) {
  54. configLoaderApi = getConfigLoaderApi(plugins);
  55. },
  56. async buildStart() {
  57. const vendureConfig = await configLoaderApi.getVendureConfig();
  58. if (!schemaInfo) {
  59. this.info(`Constructing Admin API schema...`);
  60. resetConfig();
  61. await setConfig(vendureConfig ?? {});
  62. const runtimeConfig = await runPluginConfigurations(getConfig() as any);
  63. const typesLoader = new GraphQLTypesLoader();
  64. const finalSchema = await getFinalVendureSchema({
  65. config: runtimeConfig,
  66. typePaths: VENDURE_ADMIN_API_TYPE_PATHS,
  67. typesLoader,
  68. apiType: 'admin',
  69. output: 'sdl',
  70. });
  71. const safeSchema = buildSchema(finalSchema);
  72. schemaInfo = generateSchemaInfo(safeSchema);
  73. }
  74. },
  75. resolveId(id) {
  76. if (id === virtualModuleId) {
  77. return resolvedVirtualModuleId;
  78. }
  79. },
  80. load(id) {
  81. if (id === resolvedVirtualModuleId) {
  82. return `
  83. export const schemaInfo = ${JSON.stringify(schemaInfo)};
  84. `;
  85. }
  86. },
  87. };
  88. }
  89. function getTypeInfo(type: GraphQLType) {
  90. let nullable = true;
  91. let list = false;
  92. let isPaginatedList = false;
  93. // Unwrap NonNull
  94. if (type instanceof GraphQLNonNull) {
  95. nullable = false;
  96. type = type.ofType;
  97. }
  98. // Unwrap List
  99. if (type instanceof GraphQLList) {
  100. list = true;
  101. type = type.ofType;
  102. }
  103. if (type instanceof GraphQLObjectType) {
  104. if (type.getInterfaces().some(i => i.name === 'PaginatedList')) {
  105. isPaginatedList = true;
  106. }
  107. }
  108. return [type.toString().replace(/!$/, ''), nullable, list, isPaginatedList] as const;
  109. }
  110. function generateSchemaInfo(schema: GraphQLSchema): SchemaInfo {
  111. const types = schema.getTypeMap();
  112. const result: SchemaInfo = { types: {}, inputs: {}, scalars: [], enums: {} };
  113. Object.values(types).forEach(type => {
  114. if (isObjectType(type)) {
  115. const fields = type.getFields();
  116. result.types[type.name] = {};
  117. Object.entries(fields).forEach(([fieldName, field]) => {
  118. result.types[type.name][fieldName] = getTypeInfo(field.type);
  119. });
  120. }
  121. if (isInputObjectType(type)) {
  122. const fields = type.getFields();
  123. result.inputs[type.name] = {};
  124. Object.entries(fields).forEach(([fieldName, field]) => {
  125. result.inputs[type.name][fieldName] = getTypeInfo(field.type);
  126. });
  127. }
  128. if (isScalarType(type)) {
  129. result.scalars.push(type.name);
  130. }
  131. if (isEnumType(type)) {
  132. result.enums[type.name] = type.getValues().map(v => v.value);
  133. }
  134. });
  135. return result;
  136. }