stub.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getBuiltInForStub = exports.isNamedStub = exports.createStub = exports.createNamedStub = void 0;
  4. const graphql_1 = require("graphql");
  5. function createNamedStub(name, type) {
  6. let constructor;
  7. if (type === 'object') {
  8. constructor = graphql_1.GraphQLObjectType;
  9. }
  10. else if (type === 'interface') {
  11. constructor = graphql_1.GraphQLInterfaceType;
  12. }
  13. else {
  14. constructor = graphql_1.GraphQLInputObjectType;
  15. }
  16. return new constructor({
  17. name,
  18. fields: {
  19. _fake: {
  20. type: graphql_1.GraphQLString,
  21. },
  22. },
  23. });
  24. }
  25. exports.createNamedStub = createNamedStub;
  26. function createStub(node, type) {
  27. switch (node.kind) {
  28. case graphql_1.Kind.LIST_TYPE:
  29. return new graphql_1.GraphQLList(createStub(node.type, type));
  30. case graphql_1.Kind.NON_NULL_TYPE:
  31. return new graphql_1.GraphQLNonNull(createStub(node.type, type));
  32. default:
  33. if (type === 'output') {
  34. return createNamedStub(node.name.value, 'object');
  35. }
  36. return createNamedStub(node.name.value, 'input');
  37. }
  38. }
  39. exports.createStub = createStub;
  40. function isNamedStub(type) {
  41. if ('getFields' in type) {
  42. const fields = type.getFields();
  43. // eslint-disable-next-line no-unreachable-loop
  44. for (const fieldName in fields) {
  45. const field = fields[fieldName];
  46. return field.name === '_fake';
  47. }
  48. }
  49. return false;
  50. }
  51. exports.isNamedStub = isNamedStub;
  52. function getBuiltInForStub(type) {
  53. switch (type.name) {
  54. case graphql_1.GraphQLInt.name:
  55. return graphql_1.GraphQLInt;
  56. case graphql_1.GraphQLFloat.name:
  57. return graphql_1.GraphQLFloat;
  58. case graphql_1.GraphQLString.name:
  59. return graphql_1.GraphQLString;
  60. case graphql_1.GraphQLBoolean.name:
  61. return graphql_1.GraphQLBoolean;
  62. case graphql_1.GraphQLID.name:
  63. return graphql_1.GraphQLID;
  64. default:
  65. return type;
  66. }
  67. }
  68. exports.getBuiltInForStub = getBuiltInForStub;