Skip to content

Commit

Permalink
scalars support
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Jan 12, 2024
1 parent 8555eb1 commit 2d6807f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
6 changes: 5 additions & 1 deletion examples/olympus/.graphql.demeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const config = {
},
}
},
scalars: {},
scalars: {
JSON:{
values: [JSON.stringify({__metadata:"blalbalbla"}), JSON.stringify({__secret:"dsajd89u98"})]
}
},
};
module.exports = config
4 changes: 2 additions & 2 deletions examples/olympus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "olympus",
"version": "1.1.5",
"version": "1.1.6",
"description": "Example of using GraphQL Demeter",
"private": true,
"main": "index.js",
Expand All @@ -10,6 +10,6 @@
"author": "Aexol <aexol@aexol.com> (http://aexol.com)",
"license": "ISC",
"dependencies": {
"graphql-demeter": "^1.0.2"
"graphql-demeter": "^1.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/graphql-demeter-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-demeter-core",
"version": "1.0.2",
"version": "1.0.3",
"private": false,
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion packages/graphql-demeter-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const mockObjectValue = (f: FieldType, required = false): unknown => {

export const createFakeResolvers = (schemaString: string, fakerConfig?: FakerConfig) => {
const tree = Parser.parse(schemaString);
const scalars = tree.nodes.filter((n) => n.data.type === TypeDefinition.ScalarTypeDefinition).map((n) => n.name);
const resolvers = Object.fromEntries(
tree.nodes
.filter((n) => n.data.type === TypeDefinition.ObjectTypeDefinition)
Expand All @@ -46,7 +47,10 @@ export const createFakeResolvers = (schemaString: string, fakerConfig?: FakerCon
return [
a.name,
() => {
const resolverValues = fakerConfig?.objects?.[n.name]?.[a.name];
const isCustomScalar = scalars.find((s) => s === tName);
const resolverValuesScalar = isCustomScalar && fakerConfig?.scalars?.[isCustomScalar];
const resolverValues = resolverValuesScalar || fakerConfig?.objects?.[n.name]?.[a.name];

const valueField = resolverValues && 'values' in resolverValues ? resolverValues : undefined;
if (valueField?.values.length) {
return mockValue(a.type.fieldType, () => {
Expand All @@ -68,6 +72,7 @@ export const createFakeResolvers = (schemaString: string, fakerConfig?: FakerCon
return valueFromFaker;
});
}

return mockObjectValue(a.type.fieldType);
},
];
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-demeter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-demeter",
"version": "1.0.2",
"version": "1.0.3",
"private": false,
"scripts": {
"start": "tspc --watch",
Expand All @@ -26,7 +26,7 @@
"dependencies": {
"cross-fetch": "^3.0.4",
"graphql": "15.4.0",
"graphql-demeter-core": "^1.0.2",
"graphql-demeter-core": "^1.0.3",
"graphql-yoga": "^5.1.1",
"yargs": "^16.1.1"
},
Expand Down

0 comments on commit 2d6807f

Please sign in to comment.