Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ESM support * add example using mjs * update ez examples deps * test-esm script * update ez example * update bob
- Loading branch information
Showing
11 changed files
with
843 additions
and
506 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-modules': minor | ||
--- | ||
|
||
ESM support |
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 |
---|---|---|
|
@@ -39,3 +39,6 @@ jobs: | |
|
||
- name: Test | ||
run: yarn test | ||
|
||
- name: Test ESM | ||
run: node scripts/test-esm.mjs |
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,32 @@ | ||
{ | ||
"name": "example-graphql-ez", | ||
"version": "0.0.1", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev:cjs": "tsup --watch src --onSuccess \"node dist/index.js\"", | ||
"dev:mjs": "tsup --watch src --onSuccess \"node dist/index.mjs\"" | ||
}, | ||
"dependencies": { | ||
"@envelop/graphql-modules": "1.0.0", | ||
"@graphql-ez/fastify": "^0.8.2", | ||
"@graphql-ez/plugin-graphiql": "^0.5.0", | ||
"fastify": "3.20.1", | ||
"graphql-ez": "^0.11.0", | ||
"graphql-modules": "*" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "16.6.1", | ||
"tsup": "4.13.1" | ||
}, | ||
"tsup": { | ||
"entryPoints": [ | ||
"src/index.ts" | ||
], | ||
"format": [ | ||
"cjs", | ||
"esm" | ||
], | ||
"target": "es2019", | ||
"silent": true | ||
} | ||
} |
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,48 @@ | ||
import { | ||
createModule, | ||
gql, | ||
InjectionToken, | ||
Scope, | ||
CONTEXT, | ||
} from 'graphql-modules'; | ||
|
||
interface AuthenticatedUser { | ||
_id: number; | ||
username: string; | ||
} | ||
const AuthenticatedUser = new InjectionToken<AuthenticatedUser>( | ||
'authenticated-user' | ||
); | ||
|
||
export const AuthModule = createModule({ | ||
id: 'auth', | ||
typeDefs: gql` | ||
type Query { | ||
me: User | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
me(_root: {}, _args: {}, context: GraphQLModules.Context) { | ||
return context.injector.get(AuthenticatedUser); | ||
}, | ||
}, | ||
}, | ||
providers: [ | ||
{ | ||
provide: AuthenticatedUser, | ||
scope: Scope.Operation, | ||
deps: [CONTEXT], | ||
useFactory(ctx: GraphQLModules.GlobalContext) { | ||
const authHeader = ctx.request.headers.authorization; | ||
|
||
console.log({ authHeader }); | ||
|
||
return { | ||
_id: 1, | ||
username: 'me', | ||
}; | ||
}, | ||
}, | ||
], | ||
}); |
15 changes: 15 additions & 0 deletions
15
examples/ez/src/app/social-network/social-network.module.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createModule, gql } from 'graphql-modules'; | ||
|
||
export const SocialNetworkModule = createModule({ | ||
id: 'social-network', | ||
typeDefs: gql` | ||
extend type User { | ||
friends: [User] | ||
} | ||
`, | ||
resolvers: { | ||
User: { | ||
friends: (user: any) => user.friends, | ||
}, | ||
}, | ||
}); |
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,17 @@ | ||
import { createModule, gql } from 'graphql-modules'; | ||
|
||
export const UserModule = createModule({ | ||
id: 'user', | ||
typeDefs: gql` | ||
type User { | ||
id: String | ||
username: String | ||
} | ||
`, | ||
resolvers: { | ||
User: { | ||
id: (user: any) => user._id, | ||
username: (user: any) => user.username, | ||
}, | ||
}, | ||
}); |
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,34 @@ | ||
import 'reflect-metadata'; | ||
|
||
import Fastify from 'fastify'; | ||
import { createApplication } from 'graphql-modules'; | ||
|
||
import { useGraphQLModules } from '@envelop/graphql-modules'; | ||
import { CreateApp } from '@graphql-ez/fastify'; | ||
import { ezGraphiQLIDE } from '@graphql-ez/plugin-graphiql'; | ||
|
||
import { AuthModule } from './app/auth/auth.module'; | ||
import { SocialNetworkModule } from './app/social-network/social-network.module'; | ||
import { UserModule } from './app/user/user.module'; | ||
|
||
const server = Fastify({ | ||
logger: true, | ||
}); | ||
const modulesApp = createApplication({ | ||
modules: [UserModule, AuthModule, SocialNetworkModule], | ||
}); | ||
|
||
const EZApp = CreateApp({ | ||
envelop: { | ||
plugins: [useGraphQLModules(modulesApp)], | ||
}, | ||
ez: { | ||
plugins: [ezGraphiQLIDE()], | ||
}, | ||
}); | ||
|
||
const { fastifyPlugin } = EZApp.buildApp(); | ||
|
||
server.register(fastifyPlugin); | ||
|
||
server.listen(4000); |
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,50 @@ | ||
import globby from 'globby'; | ||
import { dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import chalk from 'chalk'; | ||
|
||
async function main() { | ||
const mjsFiles = await globby(['../packages/*/dist/*.mjs'], { | ||
cwd: dirname(fileURLToPath(import.meta.url)), | ||
}); | ||
|
||
const ok = []; | ||
const fail = []; | ||
|
||
let i = 0; | ||
await Promise.all( | ||
mjsFiles.map((mjsFile) => { | ||
const mjsPath = `./${mjsFile}`; | ||
return import(mjsPath) | ||
.then(() => { | ||
ok.push(mjsPath); | ||
}) | ||
.catch((err) => { | ||
const color = i++ % 2 === 0 ? chalk.magenta : chalk.red; | ||
console.error(color('\n\n-----\n' + i + '\n')); | ||
console.error(mjsPath, err); | ||
console.error(color('\n-----\n\n')); | ||
fail.push(mjsPath); | ||
}); | ||
}) | ||
); | ||
ok.length && console.log(chalk.blue(`${ok.length} OK: ${ok.join(' | ')}`)); | ||
fail.length && | ||
console.error(chalk.red(`${fail.length} Fail: ${fail.join(' | ')}`)); | ||
|
||
if (fail.length) { | ||
console.error('\nFAILED'); | ||
process.exit(1); | ||
} else if (ok.length) { | ||
console.error('\nOK'); | ||
process.exit(0); | ||
} else { | ||
console.error('No files analyzed!'); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.