-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Json & DateTime custom scalar support (#9)
closes #8
- Loading branch information
Jason Kuhrt
authored
Mar 8, 2021
1 parent
4f83b26
commit df51143
Showing
20 changed files
with
979 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,9 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Facades | ||
scalars.d.ts | ||
scalars.js | ||
plugin.js | ||
plugin.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Module Facades | ||
* | ||
* This script builds the modules that will be consumed publically. They front the actual code inside ./dist. | ||
* The problem being solved here is that it allows consumers to do e.g. this: | ||
* | ||
* Import { ... } from 'nexus/testing' | ||
* | ||
* Instead of: | ||
* | ||
* Import { ... } from 'nexus/dist/testing' | ||
* | ||
* Whatever modules are written here should be: | ||
* | ||
* 1. ignored in .gitignore. | ||
* 2. added to the package.json files array | ||
*/ | ||
|
||
import * as fs from 'fs-jetpack' | ||
import * as lo from 'lodash' | ||
import * as os from 'os' | ||
import * as path from 'path' | ||
import { PackageJson } from 'type-fest' | ||
|
||
generateModuleFacades([ | ||
['scalars.d.ts', "export * from './dist/scalars'"], | ||
['scalars.js', "module.exports = require('./dist/scalars')"], | ||
|
||
['plugin.d.ts', "export * from './dist/plugin'"], | ||
['plugin.js', "exports.plugin = reuqire('./dist/plugin')"], | ||
]) | ||
|
||
function generateModuleFacades(facades: ModuleFacade[]) { | ||
// Write facade files | ||
|
||
for (const facade of facades) { | ||
fs.write(facade[0], facade[1] + os.EOL) | ||
} | ||
|
||
// Handle package.json files array | ||
|
||
const packageJsonPath = path.join(__dirname, '..', 'package.json') | ||
const packageJson = fs.read(packageJsonPath, 'json') as PackageJson | ||
|
||
packageJson.files = lo.uniq([...(packageJson.files ?? []), ...facades.map((facade) => facade[0])]) | ||
|
||
const packageJsonString = JSON.stringify(packageJson, null, 2) + os.EOL | ||
|
||
fs.write(packageJsonPath, packageJsonString) | ||
} | ||
|
||
type ModuleFacade = [filePath: string, fileContents: string] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { GraphQLScalarType } from 'graphql' | ||
import { DateTimeResolver } from 'graphql-scalars' | ||
import { asNexusMethod } from 'nexus' | ||
|
||
export const dateTimeScalar = asNexusMethod(new GraphQLScalarType(DateTimeResolver), 'dateTime') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GraphQLScalarType } from 'graphql' | ||
import { JSONObjectResolver } from 'graphql-scalars' | ||
import { asNexusMethod } from 'nexus' | ||
|
||
export const jsonScalar = asNexusMethod( | ||
new GraphQLScalarType({ | ||
...JSONObjectResolver, | ||
// Override the default 'JsonObject' name with one that matches what Nexus Prisma expects. | ||
name: 'Json', | ||
}), | ||
'json' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './DateTime' | ||
export * from './Json' |
Oops, something went wrong.