Skip to content

Commit

Permalink
feat: Add union types for prop type meta
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 15, 2020
1 parent b6d7198 commit 2a43b79
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions plugins/gatsby-source-newrelic-sdk/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,109 @@
const loadSdk = require('./src/loadSdk');
const { getComponentDoc, getApiDoc } = require('./src/docInfo');

const hasOwnProperty = (obj, name) =>
Object.prototype.hasOwnProperty.call(obj, name);

exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;

const propTypeMeta = schema.buildUnionType({
name: 'NewRelicSdkPropTypeMeta',
types: [
'NewRelicSdkPropTypeArrayOfMeta',
'NewRelicSdkPropTypeEnumMeta',
'NewRelicSdkPropTypeFunctionMeta',
'NewRelicSdkPropTypeShapeMeta',
'NewRelicSdkPropTypeUnionMeta',
],
resolveType: (value) => {
if (
hasOwnProperty(value, 'types') &&
hasOwnProperty(value.types[0], 'type')
) {
return 'NewRelicSdkPropTypeShapeMeta';
}

if (hasOwnProperty(value, 'types')) {
return 'NewRelicSdkPropTypeUnionMeta';
}

if (hasOwnProperty(value, 'returnValue')) {
return 'NewRelicSdkPropTypeFunctionMeta';
}

if (hasOwnProperty(value, 'itemTypes')) {
return 'NewRelicSdkPropTypeArrayOfMeta';
}

if (hasOwnProperty(value, 'constants')) {
return 'NewRelicSdkPropTypeEnumMeta';
}
},
});

const typeDefs = `
type NewRelicSdkComponent implements Node {
propTypes: [NewRelicSdkPropTypeDefinition!]!
}
type NewRelicSdkPropTypeDefinition @dontInfer {
name: String!
type: NewRelicSdkPropTypeDefinitionType!
}
type NewRelicSdkPropTypeDefinitionType @dontInfer {
name: String!
raw: String!
meta: NewRelicSdkPropTypeMeta
}
type NewRelicSdkPropTypeFunctionMeta @infer {
returnValue: NewRelicSdkFunctionReturnValue!
params: [NewRelicSdkFuncionParam!]!
}
type NewRelicSdkPropTypeEnumMeta {
constants: [String!]!
}
type NewRelicSdkPropTypeArrayOfMeta {
itemTypes: NewRelicSdkPropTypeDefinitionType!
}
type NewRelicSdkPropTypeUnionMeta {
types: [NewRelicSdkPropTypeDefinitionType!]!
}
type NewRelicSdkPropTypeShapeMeta {
types: [NewRelicSdkPropTypeDefinition!]!
}
type NewRelicSdkFunctionReturnValue {
description: String
type: String!
}
type NewRelicSdkFuncionParam {
description: String
name: String!
type: String!
}
`;

createTypes([propTypeMeta, typeDefs]);
};

exports.createResolvers = ({ createResolvers }) => {
createResolvers({
NewRelicSdkFunctionReturnValue: {
type: {
resolve: (source) => source.promiseType || source.type,
},
},
});
};

exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest },
{ release, components = [], apis = [] }
Expand Down

0 comments on commit 2a43b79

Please sign in to comment.