Last active
November 7, 2019 12:09
-
-
Save KariHe/da55c1e436286c8b9f47dceabdccdc1d to your computer and use it in GitHub Desktop.
GraphQL modules blog post examples
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
import { FeatureOneModule } from './featureOne'; | |
import { FeatureTwoModule } from './featureTwo'; | |
export const AppModule = new GraphQLModule({ | |
imports: [ FeatureOneModule, FeatureTwoModule ] | |
}); | |
const server = new ApolloServer({ | |
schema: AppModule.schema, | |
context: AppModule.context | |
}); |
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
import { GraphQLModule } from '@graphql-modules/core'; | |
import { ApolloServer } from 'apollo-server'; | |
import gql from "graphql-tag"; | |
// Application module | |
const MyGraphQLModule = new GraphQLModule({ | |
typeDefs: gql` | |
type Query { | |
greeting: String | |
} | |
`, | |
resolvers: { | |
Query: { | |
greeting() { return 'Hello World'; } | |
} | |
} | |
}); | |
// Use Apollo server to serve the application | |
const server = new ApolloServer({ | |
schema: MyGraphQLModule.schema, | |
context: session => session | |
}); | |
server.listen().then(({ url }) => { | |
console.log(`Server running at ${url}`); | |
}); |
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
import { UserProvider } from './user.provider'; | |
import { MySecondModule } from './my-second-module'; | |
export const MyModule = new GraphQLModule({ | |
typeDefs, | |
resolvers, | |
imports: [ MySecondModule ], | |
providers: [ UserProvider ] | |
}); |
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
import 'reflect-metadata'; |
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
import { Injectable, ProviderScope } from '@graphql-modules/di'; | |
@Injectable({ | |
scope: ProviderScope.Application | |
}) | |
class MyProvider {} |
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
// MyProvider.js | |
import { Injectable, ProviderScope } from '@graphql-modules/di'; | |
@Injectable() | |
export class MyProvider { | |
getMessage() { return 'Hello World'; } | |
} | |
// MyModule.js | |
import { GraphQLModule } from '@graphql-modules/core'; | |
import * as typeDefs from './schema.graphql'; | |
import { MyProvider } from './MyProvider'; | |
export const MyModule = new GraphQLModule({ | |
typeDefs, | |
providers: [ | |
MyProvider, | |
], | |
resolvers: { | |
Query: { | |
test: (obj, args, {injector) => { | |
// Get service from injector | |
const provider = injector.get(MyProvider); | |
// Invoke service method | |
return provider.getMessage(); | |
} | |
} | |
} | |
}); |
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
// MyModule.test.js | |
import { MyModule } from 'MyModule'; | |
import { MyProvider } from 'MyProvider'; | |
import { execute } from 'graphql'; | |
describe('MyModule', () => { | |
it('FieldResolver of Query: test', async () => { | |
// Import schema and injector from the module | |
const { schema, injector } = MyModule; | |
// Mock MyProvider | |
injector.provide({ | |
provide: MyProvider, | |
overwrite: true, | |
useValue: { | |
getMessage: () => 'Test response' | |
} | |
}); | |
// Execute query against the module schema | |
const result = await execute({ | |
schema, | |
document: gql` | |
query { | |
test | |
} | |
`, | |
}); | |
expect(result.errors).toBeFalsy(); | |
expect(result.data[test]).toBe('Test response'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment